Code Monkey home page Code Monkey logo

daprepo's Introduction

DapRepo

Dapper Generic Repository And Unit Of Work

Instalation

Last Release: Version 1.1.0 (September 27, 2017)

PM> Install-Package Hurdad.DataAccess.DapRepo

Usage

Create models:

public class Person
{
    [Key]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    [Ignore]
    public string FullName => $"{FirstName} {LastName}";
}

Create repository:

class PersonRepository : GenericRepository<Person>
{
    public PersonRepository(IDbConnection dbConnection) : base(dbConnection)
    {
    }
}

Create unit-of-work class:

class AppUnitOfWork : UnitOfWork
{
    public UnitOfWork(bool isTransactional = false,
        IsolationLevel isolationLevel = IsolationLevel.ReadCommitted,
        int transactionTimeoutInSecond = 300,
        TransactionScopeOption transactionScopeOption = TransactionScopeOption.Required) 
        :base(isTransactional, isolationLevel, transactionTimeoutInSecond, transactionScopeOption)
    {
    }

    protected override IDbConnection CreateConnection()
    {
        var connection = new System.Data.SqlClient.SqlConnection(connectionString: ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
        return connection;
    }

    private PersonRepository _personRepository;
    public PersonRepository PersonRepository => _personRepository ?? (_personRepository = new PersonRepository(this.DbConnection));
}

Then you can use instance of Unit-of-work class in your project:

using (var uow = new UnitOfWork(isTransactional: true))
{
    uow.PersonRepository.Insert(person);
    uow.CommitTransaction();

    persons.AddRange(uow.PersonRepository.GetAll());
}

You can find full example project in sample directory

daprepo's People

Contributors

mehrdadbahrainy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.