Skip to content

ZibStack.NET.Dapper

NuGet Source

Dapper integration for ZibStack.NET.Dto CRUD API. Provides a DapperCrudStore<TEntity, TKey> base class via source generation.

dotnet add package ZibStack.NET.Dto
dotnet add package ZibStack.NET.Dapper
dotnet add package Dapper
  1. Define your entity with [CrudApi]:
[CrudApi]
[CreateDto]
[UpdateDto]
[ResponseDto]
public class Player
{
[DtoIgnore(DtoTarget.Create | DtoTarget.Update | DtoTarget.Query)]
public int Id { get; set; }
public required string Name { get; set; }
public int Level { get; set; }
}
  1. Implement the store:
using ZibStack.NET.Dapper;
public class PlayerStore : DapperCrudStore<Player, int>
{
public PlayerStore(IDbConnection db) : base(db) { }
protected override string TableName => "Players";
}
  1. Register in DI:
builder.Services.AddScoped<IDbConnection>(_ =>
new SqliteConnection("Data Source=app.db"));
builder.Services.AddScoped<ICrudStore<Player, int>, PlayerStore>();

Base class implementing ICrudStore<TEntity, TKey> using Dapper:

MethodSQL
GetByIdAsyncSELECT * FROM {Table} WHERE {Key} = @Id
QuerySELECT * FROM {Table} (returns in-memory IQueryable)
CreateAsyncINSERT INTO {Table} (columns...) VALUES (@params...)
UpdateAsyncUPDATE {Table} SET col = @col, ... WHERE {Key} = @Key
DeleteAsyncDELETE FROM {Table} WHERE {Key} = @Key

Override virtual properties to customize mapping:

PropertyDefaultDescription
TableNameentity type name + “s”Table name used in SQL
KeyColumn"Id"Primary key column name

All methods are virtual — override any operation for custom SQL:

public class PlayerStore : DapperCrudStore<Player, int>
{
public PlayerStore(IDbConnection db) : base(db) { }
protected override string TableName => "Players";
public override async ValueTask<Player?> GetByIdAsync(int id, CancellationToken ct = default)
{
var sql = "SELECT * FROM Players WHERE Id = @Id AND IsDeleted = 0";
return await SqlMapper.QueryFirstOrDefaultAsync<Player>(Db,
new CommandDefinition(sql, new { Id = id }, cancellationToken: ct));
}
}

This package is a source generator. When your project references both ZibStack.NET.Dto (which provides ICrudStore) and Dapper, the generator emits the DapperCrudStore base class into your compilation. No runtime dependency on this package.

  • Query() loads all rows into memory and returns IQueryable over the in-memory collection. For large tables, override Query() with a custom implementation or use filtering at the SQL level.
  • CreateAsync skips the key column (assumes auto-increment). Override for composite keys or non-auto-increment scenarios.
  • Column names are derived from property names via reflection. Override individual methods if your column names differ from property names.