A cohesive set of infrastructure libraries for dotnet that utilizes abstractions for event handling, persistence, unit of work, mediator, distributed messaging, event bus, CQRS, email, and more
$ dotnet add package RCommon.DapperDapper implementation of the RCommon persistence abstractions. Provides a lightweight SQL mapper repository using Dapper and Dommel for CRUD operations with expression-based querying, while integrating with the RCommon data store factory and domain event tracking.
DapperRepository<TEntity> implementing ISqlMapperRepository<T>, IReadOnlyRepository<T>, and IWriteOnlyRepository<T>SelectAsync, CountAsync, and AnyAsync extension methodsInsertAsync, UpdateAsync, and DeleteAsyncDeleteMultipleAsync with expression predicatesGetAsyncIDataStoreFactory and RDbConnectionISoftDelete are automatically filtered on reads and logically deleted on writesIMultiTenant are automatically filtered by tenant on reads and stamped with TenantId on writesdotnet add package RCommon.Dapper
// Configure in Program.cs or Startup
builder.Services.AddRCommon()
.WithPersistence<DapperPersistenceBuilder>(dapper =>
{
dapper.AddDbConnection<ApplicationDbConnection>("ApplicationDb", options =>
{
options.DbFactory = SqlClientFactory.Instance;
options.ConnectionString = builder.Configuration.GetConnectionString("ApplicationDb");
});
dapper.SetDefaultDataStore(defaults =>
defaults.DefaultDataStoreName = "ApplicationDb");
});
Your database connection must inherit from RDbConnection:
public class ApplicationDbConnection : RDbConnection
{
public ApplicationDbConnection(IOptions<RDbConnectionOptions> options)
: base(options) { }
}Then inject and use the repository abstractions:
public class ProductService
{
private readonly ISqlMapperRepository<Product> _productRepo;
public ProductService(ISqlMapperRepository<Product> productRepo)
{
_productRepo = productRepo;
}
public async Task<ICollection<Product>> GetActiveProductsAsync()
{
return await _productRepo.FindAsync(p => p.IsActive);
}
public async Task<Product> GetByIdAsync(int id)
{
return await _productRepo.FindAsync(id);
}
}DapperRepository<TEntity> automatically supports soft delete and multitenancy when your entities implement the opt-in interfaces:
using RCommon.Entities;
public class Product : BusinessEntity<int>, ISoftDelete, IMultiTenant
{
public string Name { get; set; }
public bool IsDeleted { get; set; }
public string? TenantId { get; set; }
}Reads automatically exclude soft-deleted records and scope to the current tenant:
// Both filters applied transparently
var products = await _productRepo.FindAsync(p => p.IsActive);Writes automatically stamp the tenant and support logical deletion:
// TenantId stamped automatically from ITenantIdAccessor
await _productRepo.AddAsync(new Product { Name = "Widget" });
// Soft delete — sets IsDeleted = true, performs UPDATE via Dapper
await _productRepo.DeleteAsync(product, isSoftDelete: true);| Type | Description |
|---|---|
DapperRepository<TEntity> | Concrete repository using Dapper/Dommel with expression-based CRUD operations |
DapperPersistenceBuilder | Fluent builder for registering Dapper database connections and repository services in DI |
IDapperBuilder | Builder interface exposing AddDbConnection<T>() for registering named database connections |
For full documentation, visit rcommon.com.
Licensed under the Apache License, Version 2.0.