RSCG – Finch.Generators

RSCG – Finch.Generators
 
 

name Finch.Generators
nuget https://www.nuget.org/packages/Finch.Generators/
link https://github.com/ivmazurenko/finch
author Ivan Mazurenko

dapper style generator

 

This is how you can use Finch.Generators .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Finch.Abstractions" Version="0.0.107" />
    <PackageReference Include="Finch.Generators" Version="0.0.107" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.1" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using DB;
using Microsoft.Data.SqlClient;

Console.WriteLine("Hello, World!");
string connectionString="not set";
var connection = new SqlConnection(connectionString);
var items = await connection.QueryAsync<Person>("select * from Person");

 

The code that is generated is

// <auto-generated/>

namespace DB;

public static partial class SqlserverConnectionExtensions
{
    public static global::System.Collections.Generic.List<T> Query<T>(
        this global::Microsoft.Data.SqlClient.SqlConnection connection,
        string sql)
        where T : new()
    {
        connection.Open();

        var items = new global::System.Collections.Generic.List<T>();
        using var command = new global::Microsoft.Data.SqlClient.SqlCommand(sql, connection);
        using var reader = command.ExecuteReader();
        while (reader.Read())
        {
            T item = new T();

            SqlserverObjectMapper.Map(item, reader);
            items.Add(item);
        }
        
        return items;
    }
}
// <auto-generated/>

namespace DB;

public static partial class SqlserverConnectionExtensions
{
    public static async global::System.Threading.Tasks.Task<global::System.Collections.Generic.List<T>> QueryAsync<T>(
        this global::Microsoft.Data.SqlClient.SqlConnection connection,
        string sql,
        CancellationToken cancellationToken = default)
        where T : new()
    {
        await connection.OpenAsync(cancellationToken);

        var items = new global::System.Collections.Generic.List<T>();
        await using var command = new global::Microsoft.Data.SqlClient.SqlCommand(sql, connection);
        await using var reader = await command.ExecuteReaderAsync(cancellationToken);
        while (await reader.ReadAsync(cancellationToken))
        {
            T item = new T();

            SqlserverObjectMapper.Map(item, reader);
            items.Add(item);
        }
        
        return items;
    }
}
// <auto-generated/>

namespace DB;

public static partial class SqlserverConnectionExtensions
{
    public static async global::System.Threading.Tasks.Task<global::System.Collections.Generic.List<T>> QueryAsync<T>(
        this global::Microsoft.Data.SqlClient.SqlConnection connection,
        string sql,
        CancellationToken cancellationToken,
        params global::Microsoft.Data.SqlClient.SqlParameter[] parameters)
        where T : new()
    {
        await connection.OpenAsync(cancellationToken);

        var items = new global::System.Collections.Generic.List<T>();
        await using var command = new global::Microsoft.Data.SqlClient.SqlCommand(sql, connection);
        command.Parameters.AddRange(parameters);
        await using var reader = await command.ExecuteReaderAsync(cancellationToken);
        while (await reader.ReadAsync(cancellationToken))
        {
            T item = new T();

            SqlserverObjectMapper.Map(item, reader);
            items.Add(item);
        }
        
        return items;
    }
}
// <auto-generated/>

using System;
using System.Collections.Generic;

namespace DB;

internal class SqlserverObjectMapper
{
    public static void Map<T>(T item, global::Microsoft.Data.SqlClient.SqlDataReader reader)
    {
        if(typeof(T) == typeof(DB.Person))
        {
            SqlserverPropertyMapper.Map(item as DB.Person, reader);
            return;
        }
    }
}

// <auto-generated/>

using System;

namespace DB;

internal partial class SqlserverPropertyMapper
{
    public static void Map(
        DB.Person item,
        global::Microsoft.Data.SqlClient.SqlDataReader reader)
    {
         item.Id = Convert.ToInt32(reader["Id"]);

         item.Name = reader["Name"].ToString();

         item.Age = Convert.ToInt32(reader["Age"]);
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Finch.Generators


Posted

in

, ,

by

Tags: