DreamBig.SourceGen.Dapper.SqlServer
1.0.0-beta.10
dotnet add package DreamBig.SourceGen.Dapper.SqlServer --version 1.0.0-beta.10
NuGet\Install-Package DreamBig.SourceGen.Dapper.SqlServer -Version 1.0.0-beta.10
<PackageReference Include="DreamBig.SourceGen.Dapper.SqlServer" Version="1.0.0-beta.10" />
<PackageVersion Include="DreamBig.SourceGen.Dapper.SqlServer" Version="1.0.0-beta.10" />
<PackageReference Include="DreamBig.SourceGen.Dapper.SqlServer" />
paket add DreamBig.SourceGen.Dapper.SqlServer --version 1.0.0-beta.10
#r "nuget: DreamBig.SourceGen.Dapper.SqlServer, 1.0.0-beta.10"
#:package DreamBig.SourceGen.Dapper.SqlServer@1.0.0-beta.10
#addin nuget:?package=DreamBig.SourceGen.Dapper.SqlServer&version=1.0.0-beta.10&prerelease
#tool nuget:?package=DreamBig.SourceGen.Dapper.SqlServer&version=1.0.0-beta.10&prerelease
DreamBig.SourceGen.Dapper
DreamBig.SourceGen.Dapper provides compile-time SQL generation for Dapper users who want to avoid raw SQL in application code.
Features
- Attribute-driven repository generation (
[DbRepository]) - Entity mapping attributes (
[DbTable],[DbColumn],[DbKey],[DbIgnore]) - Generated CRUD methods
- Generated stored procedure execution with output parameter support
- Generated INNER/OUTER joins from method-level attributes
- Provider-specific SQL generation (SQL Server and PostgreSQL)
Target Frameworks
Runtime library supports:
net10.0
Install
- SQL Server:
DreamBig.SourceGen.Dapper - PostgreSQL:
DreamBig.SourceGen.Dapper.PostgreSql
DI Setup
SQL Server:
using DreamBig.SourceGen.Dapper.SqlServer;
services.AddDreamBigDapperSqlServer(configuration);
services.AddDreamBigDapperGenerated();
PostgreSQL:
using DreamBig.SourceGen.Dapper.PostgreSql;
services.AddDreamBigDapperPostgreSql(configuration);
services.AddDreamBigDapperGenerated();
Configuration sections:
- SQL Server:
DreamBig:Dapper:SqlServer - PostgreSQL:
DreamBig:Dapper:PostgreSql
Overloads are available for raw connection strings and custom connection string factories.
Quick Start
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DreamBig.SourceGen.Dapper.Attributes;
[DbTable("Customers", Schema = "dbo")]
public sealed class Customer
{
[DbKey]
public int Id { get; set; }
[DbColumn("full_name")]
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
[DbRepository]
public interface ICustomerRepository
{
Task<int> InsertCustomer(Customer entity, CancellationToken cancellationToken);
Task<int> UpdateCustomer(Customer entity, CancellationToken cancellationToken);
Task<int> DeleteCustomer(int id, CancellationToken cancellationToken);
Task<Customer?> GetByIdCustomer(int id, CancellationToken cancellationToken);
Task<IEnumerable<Customer>> GetAllCustomers(CancellationToken cancellationToken);
}
The source generator emits CustomerRepositoryGenerated, with SQL and Dapper calls already implemented.
Primary Key Without [DbKey]
You can define the primary key directly on DbTableAttribute using PrimaryKey.
This is useful when the entity class comes from another team/package and you do not want to modify the entity source.
using System.Threading;
using System.Threading.Tasks;
using DreamBig.SourceGen.Dapper.Attributes;
[DbTable("Customers", Schema = "dbo", PrimaryKey = "CustomerId")]
public sealed class ExternalCustomer
{
public int CustomerId { get; set; }
public string Name { get; set; } = string.Empty;
}
[DbRepository]
public interface IExternalCustomerRepository
{
Task<int> UpdateCustomer(ExternalCustomer entity, CancellationToken cancellationToken);
Task<int> DeleteCustomer(int customerId, CancellationToken cancellationToken);
Task<ExternalCustomer?> GetByIdCustomer(int customerId, CancellationToken cancellationToken);
}
PrimaryKey can match either:
- the CLR property name (
CustomerId), or - the mapped SQL column name when
DbColumnAttributeis used.
Join Example
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DreamBig.SourceGen.Dapper.Attributes;
[DbQuery(From = "Customers", Schema = "dbo", Where = "[IsActive] = @isActive", OrderBy = "Id", OrderByDirection = OrderByDirection.Desc)]
[DbJoin(JoinType = JoinType.Left, JoinTable = typeof(Order), JoinColumnA = "Id", JoinColumnB = "CustomerId", Schema = "sales")]
Task<IEnumerable<CustomerSummary>> QueryActive(bool isActive, CancellationToken cancellationToken);
Notes:
- The generator assigns table aliases (
t0,t1, ...) automatically. OrderByexpects a column/property name only; useOrderByDirectionto control sort direction.JoinColumnAandJoinColumnBmust match CLR property names on the base entity and join entity (validated at compile time).
Stored Procedure Example
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using DreamBig.SourceGen.Dapper.Attributes;
using DreamBig.SourceGen.Dapper.Internal;
[DbStoredProcedure("usp_customer_summary", Schema = "dbo")]
Task<GeneratedProcedureResult<CustomerSummary>> GetSummary(
[DbParam("@customerId", DbType = DbType.Int32)] int customerId,
[DbParam("@total", Direction = DbParamDirection.Output)] int total,
CancellationToken cancellationToken);
Unit Of Work Example
using System;
using System.Threading;
using System.Threading.Tasks;
using DreamBig.SourceGen.Dapper.Attributes;
[DbUnitOfWork]
public interface IAppUnitOfWork
{
ICustomerRepository Customers { get; }
IOrderRepository Orders { get; }
}
// generated type: AppUnitOfWorkGenerated
var uow = new AppUnitOfWorkGenerated(() => new SqlConnection(connectionString));
await uow.BeginTransactionAsync(cancellationToken: cancellationToken);
try
{
await uow.Customers.UpdateCustomer(customer, cancellationToken);
await uow.Orders.DeleteOrder(orderId, cancellationToken);
await uow.CommitAsync(cancellationToken);
}
catch
{
await uow.RollbackAsync(cancellationToken);
throw;
}
Notes:
Insert*,Update*,Delete*, and stored procedure methods require an active transaction and throwInvalidOperationExceptionotherwise.- Read/query methods can execute with or without an active transaction.
Known Limitations (v1)
- SQL dialect support is limited to SQL Server and PostgreSQL.
- Stored procedures support one mapped result set plus output parameter capture.
- Complex projection/multi-mapping scenarios are not yet generated.
Dialect Packages
The generator emits SQL based on the provider-specific package you install (DreamBig.SourceGen.Dapper for SQL Server, DreamBig.SourceGen.Dapper.PostgreSql for PostgreSQL) while preserving the same attribute contracts.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Azure.Identity (>= 1.19.0)
- Dapper (>= 2.1.72)
- Microsoft.Data.SqlClient (>= 6.1.4)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Options (>= 10.0.5)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.5)
- Microsoft.Extensions.Options.DataAnnotations (>= 10.0.5)
- Microsoft.Identity.Client (>= 4.83.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-beta.10 | 30 | 3/15/2026 |
| 1.0.0-beta.9 | 27 | 3/15/2026 |
| 1.0.0-beta.8 | 44 | 3/14/2026 |