Narochno.Dapper
Unit of Work pattern with Dapper (deprecated)
Install / Use
/learn @Narochno/Narochno.DapperREADME
Narochno.Dapper
A Unit of Work pattern for Dapper. Use database driver that supports ADO.NET.
Philosophy
- Avoid Heavy ORMs like Entity Framework and NHibernate!
- Do the leg work of making entities against your tables and care about the data.
- Avoid heavy joins. Cache reference data and denormalize.
- Cache heavily
Example Usage
Implement IDbConnectionFactory
serviceCollection.AddNarochnoDapper(new NpgsqlConnectionFactory(connectionString));
Create Queries or Commands
public class GetAllAddresses : IQuery<IList<Address>>
{
public IList<Address> Execute(ISession session)
{
//using Dapper here!
return session.Query<Address>("select * from address").ToList();
}
}
Create Repositories
public class AddressRepository : IAddressRepository
{
private readonly IDatabase database;
private readonly ICacheManager cacheManager;
public AddressRepository(IDatabase database, ICacheManager cacheManager)
{
this.database = database;
this.cacheManager = cacheManager;
}
public IList<Address> GetAllAddresses()
{
//TODO use cacheManager!
return database.Query(new GetAllAddresses());
}
public Address GetAddressById(long id)
{
return GetAllAddresses().FirstOrDefault(x => x.Id == id);
}
}
Transactions
I try to avoid transactions by just making inserts/updates not dependant on other inserts/updates but it's unavoidable.
This supports nesting BeginTransaction by using a stack so your calls to other repositories don't need to know or care about transactions. A commit only occurs on the final commit call. The using block will auto rollback if commit isn't called.
using (var transaction = session.BeginTransaction())
{
...
transaction.Commit();
}
Related Skills
node-connect
341.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
341.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.6kCommit, push, and open a PR
