GithubHelp home page GithubHelp logo

abuzaforfagun / genericunitofwork Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 17 KB

A .NETStandard package to plug and play with unit of work pattern in EntityFrameworkCore.

C# 100.00%
unitofwork unitofworkpattern unitofwork-pattern design-patterns entity-framework-core dotnet-core dotnet-standard

genericunitofwork's Introduction

Generic Unit Of Work

A .NETStandard package to plug and play with unit of work pattern in EntityFrameworkCore.

Uses

  • Add the reference of the library or install nuget package following by Install-Package EntityFrameworkCore.GenericUnitOfWork command from visual studio package manager console or dotnet add package EntityFrameworkCore.GenericUnitOfWork from command line.
  • Register the library in ConfigureServices services.AddUnitOfWork<AppDbContext>();. AddUnitOfWork is a generic method. You need to pass your database context.
  • Use IUnitOfWork using constractor dependency injection.
public EmployeesController(IUnitOfWork unitOfWork)
{
    _unitOfWork = unitOfWork;
}
  • Access your entity repository using _unitOfWork.Repository<Employee>().
  • Do your operation using the available methods.

Sample code

Check out the complete sample project inside sample folder.

Basic uses of GenericUnitOfWork in controlelr

[Route("api/[controller]")]
[ApiController]
public class EmployeesController : ControllerBase
{
    private readonly IUnitOfWork _unitOfWork;

    public EmployeesController(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    [HttpPost]
    public async Task<IActionResult> Create(Employee employee)
    {
        _unitOfWork.Repository<Employee>().Add(employee);
        await _unitOfWork.SaveChangesAsync();
        return Ok();
    }

    [HttpGet]
    public async Task<IActionResult> GetAll() => Ok(await _unitOfWork.Repository<Employee>().GetAllAsync());

    [HttpGet("{id}")]
    public async Task<IActionResult> Get(int id) => Ok(await _unitOfWork.Repository<Employee>().GetQuery().Include(e => e.Department).SingleAsync(e => e.Id == id));


    [HttpPut]
    public async Task<IActionResult> Update(Employee employee)
    {
        _unitOfWork.Repository<Employee>().Update(employee);
        await _unitOfWork.SaveChangesAsync();
        return Ok();
    }

    [HttpDelete]
    public async Task<IActionResult> Delete(Employee employee)
    {
        _unitOfWork.Repository<Employee>().Remove(employee);
        await _unitOfWork.SaveChangesAsync();
        return Ok();
    }
}

genericunitofwork's People

Contributors

abuzaforfagun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

vanasis toantv13

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.