Finch.Generators by Ivan Mazurenko
NuGet / site data
Details
Info
Name: Finch.Generators
Package Description
Author: Ivan Mazurenko
NuGet: https://www.nuget.org/packages/Finch.Generators/
You can find more details at https://github.com/ivmazurenko/finch
Author
Ivan Mazurenko
Original Readme
Finch - database mapping extension methods source generator
Introduction
Finch is c# source generator designed to simplify database interactions by generating concise extension methods for database queries mappings.
Getting Started
-
Install the Finch NuGet packages:
$ dotnet add package Finch.Abstractions
$ dotnet add package Finch.Generators -
Mark the required type with an attribute corresponding to your desired database:
[GenerateSqlserverConnectionExtensions]
public class TbUser
{
public int id \{ get; set; }
public string name \{ get; set; }
} -
Use the extension method for your type:
var connection = new SqlConnection(connectionString);
var items = await connection.QueryAsync<TbUser>("select * from tb_user");
Contribution
Contributions to Finch are welcome! If you encounter any issues or have ideas for improvement, feel free to open an issue or submit a pull request on GitHub.
License
Finch is licensed under the MIT License.
About
dapper style generator
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
This is the CSharp Project that references Finch.Generators
<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>
This is the use of Finch.Generators in Program.cs
// 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");
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- SqlserverConnectionExtensions.Query.g.cs
- SqlserverConnectionExtensions.QueryAsync.g.cs
- SqlserverConnectionExtensions.QueryAsyncWithParameter.g.cs
- SqlserverObjectMapper.g.cs
- SqlserverPropertyMapper.Person.g.cs
// <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"]);
}
}
Useful
Download Example (.NET C#)
Share Finch.Generators
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Finch.Generators
aaa
Category "Database" has the following generators:
1 Breezy
5 Gedaq