SkillAgentSearch skills...

Narochno.Dapper

Unit of Work pattern with Dapper (deprecated)

Install / Use

/learn @Narochno/Narochno.Dapper
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

View on GitHub
GitHub Stars9
CategoryDevelopment
Updated1y ago
Forks0

Languages

C#

Security Score

55/100

Audited on Jun 23, 2024

No findings