Coder Social home page Coder Social logo

rizwanuppal / ef-unit-of-work Goto Github PK

View Code? Open in Web Editor NEW

This project forked from daniel127/ef-unit-of-work

0.0 0.0 0.0 144 KB

Repository and Unit of Work pattern implementation for Entity Framework Core.

License: MIT License

Shell 0.40% C# 99.60%

ef-unit-of-work's Introduction

Status

Quality Gate Lines of Code Bugs Coverage Maintainability Rating

Branch Build Deployment
master Build Build Status Nuget package
develop Build Build Status N/A

What is it?

Repository and Unit of Work pattern implementation for Entity Framework Core 3.

How to use?

Installation

Install the Nuget packages. Use Abstractions for logic layers and the other with insfrastructure layer or monolithic project.

dotnet add package QD.EntityFrameworkCore.UnitOfWork
dotnet add package QD.EntityFrameworkCore.UnitOfWork.Abstractions

Register Services

public class AppDbContext : DbContext, IDbContext
{
    public DbSet<Product> Products { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

    ...
    ...
}
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AppDbContext>(builder =>
    {
        ...
    });

#region Register UnitOfWorks
    // Register a IUnitOfWork<AppDbContext>
    _services.AddUnitOfWork<AppDbContext>();
    // Register a IUnitOfWork<AppDbContext> and IUnitOfWork
    _services.AddUnitOfWork<AppDbContext>(onlyGeneric: false);
#endregion

    // Optional, register custom repositories
    _services.AddRepository<Product, ProductRepository>();
    _services.AddReadOnlyRepository<Product, ProductReadOnlyRepository>();
}

Use the services

public class FancyService : IFancyService
{
    private readonly IUnitOfWork<AppDbContext> _unitOfWork;

    public FancyService(IUnitOfWork<AppDbContext> unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public void DoFancyThing()
    {
        var productsRepository = _unitOfWork.GetRepository<Product>();

        var products = productsRepository.GetAll();

        ...
        // Use inserts, updates, deletes.
        ...

        _unitOfWork.SaveChanges();
    }
}

Paged Collections (v2.0)

public void FancyFunction()
{
    // These are some examples

    // Array
    IPagedCollection<Product> page1 = _productsRepository.GetPagedArray(20, 0, product => product.Price > 500);
    IPagedCollection<Product> page2 = await _productsRepository.GetPagedArrayAsync(
        pageSize: 10,
        orderBy: query => query.OrderBy(product => product.Price).ThenBy(product => product.Name)
    );
    // Indexers
    var a = page1[1].Price;
    var b = ((PagedArray<Product>)page1)[2].Price;

    ...

    // List
    IPagedCollection<Product> page1 = _productsRepository.GetPagedList(20, 0, product => product.Price > 500);
    IPagedCollection<Product> page2 = await _productsRepository.GetPagedListAsync(
        pageSize: 10,
        orderBy: query => query.OrderBy(product => product.Price).ThenBy(product => product.Name)
    );
    // Indexers
    var a = page1[1].Price;
    var b = ((PagedList<Product>)page1)[2].Price;

    ...

    // Dictionary
    IPagedCollection<KeyValuePair<Guid, Product>> page1 = _productsRepository.GetPagedDictionary(product => product.Id, 20, 0, product => product.Price > 500);
    IPagedCollection<KeyValuePair<Guid, Product>> page2 = await _productsRepository.GetPagedDictionaryAsync(
        keySelector: product => product.Id,
        pageSize: 10,
        orderBy: query => query.OrderBy(product => product.Price).ThenBy(product => product.Name)
    );
    // Indexers
    var a = page1[1].Value.Price;
    var b = ((PagedDictionary<Guid, Product>)page1)[Guid.Parse("5926d548-c0b3-4c40-b37f-e89be7741024")].Price;
}

The before methods are same that

    GetAll(predicate, orderBy).ToPagedArray(pageSize, pageNumber);
    GetAll(predicate, orderBy).ToPagedListAsync(pageSize, pageNumber);
    GetAll(predicate, orderBy).ToPagedDictionary(keySelector, pageSize, pageNumber);

ef-unit-of-work's People

Contributors

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