Coder Social home page Coder Social logo

dapper-unitofwork's Introduction

Dapper.UnitOfWork

This library is intended to be used asynchronously only. Non-async methods are not exposed.

Unit of Work implementation for Dapper. This project relies on sequel for entity mapping and CRUD templating and lunch-pail for establishing the database context.

The intention of this project is to provide an abstract repository to be implemented using an opt-in model. Whereby only the desired functionality is exposed to consumers. The dependency on IDbContext provides the context to establish the unit of work, and ISqlMapper<TEntity> yields the required information about the entity. The opt-in model was chosen as I feel it's favorable to the generic repository approach, insofar that it exposes all functionality to all models, which is rarely desireable.

Getting Started

  1. Implement the desired facets of AbstractRepository
//Post.cs
public class Post 
{
  public int Id { get; set; } //ISqlMapper assumes a key named "Id"
  public string Author { get; set; }
  public string Body { get; set; }
}

//IPostRepository.cs
public interface IPostRepository 
{
  Task<Post> Read (int id);
}

//PostRepository.cs
public class PostRepository : AbstractRepository<Post>, IPostRepository
{
  public PostRepositry(
    IDbContext dbContext,
    ISqlMapper<Post> sqlMapper
  ) : base(dbContext, sqlMapper)
  { }

  public async Task<Post>Read(int id)
  {
    return await ReadEntity(id);
  }
}

Custom Entity Key

Working with the example above, let's assume that your entity's key field is called "PostId" and not "Id". Since ISqlMapper will assume a key named "Id" we need to override this property.

//Post.cs
public class Post 
{
  public int PostId { get; set; }
  ///...
}

//PostSqlMapper.cs
public class PostSqlMapper : ISqlMapper<Post>
{
  public override string Key =>
    "PostId";
}

You will of course need to ensure your IoC is creating this when request, as opposed to the generic SqlMapper<Post>. But otherwise, no other changes will be necessary.

Built with โ™ฅ by Pim Brouwers in Toronto, ON.

dapper-unitofwork's People

Contributors

pimbrouwers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.