Giter Club home page Giter Club logo

ezdev.genericrepository's Introduction

Build Status Azure DevOps coverage NuGet latest version Medium Badge

Easy Generic Repository

EzDev.GenericRepository is a very simplistic, lightweight generic repository based on EntityFramework Core, that doesn't lock you into a certain way of working. You're provided a single base class with simple CRUD-based operations, that you may override if you have other requirements.

Installation

Install with NuGet

or use .NET Core CLI
dotnet add package EzDev.GenericRepository.

Consider using --prelease for preview versions.

How do I get started?

Create a class that inherits from EntityRepository<T> and implement its constructor.
In its simplest form, you can have a repository such as below.

public class SimpleEmployeeRepository : EntityRepository<Employee> {
    public SimpleEmployeeRepository(DbContext context) : base(context) { }
}

That's honestly it.

The SimpleEmployeeRepository now has default implementations for getting, adding, updating, and deleting Employee entities.

More advanced options

Say you have a Company type acting as an aggregate root with a list of employees, and you want to retrieve all employees whenever you query a company.

In this case, you may want to override the default Entities property on the EntityRepository, as demonstrated below.

public class CompanyRepository : EntityRepository<Company> {
    public CompanyRepository(DbContext context) : base(context) {
        Entities = context.Set<Company>().Include(c => c.Employees).AsNoTracking();
    }
}

Extension and listening points

Take advantage of events to plug in your own code without having to override methods. This is great for implementing cross-cutting concerns such as logging.

You can listen to repository events in two ways: implement the methods directly in the repository, or, register them with the dependency injection framework.

public class EmployeeRepositoryWithEvents : EntityRepository<Employee> {
    public EmployeeRepositoryWithEvents(DbContext context, ILogger logger) : base(context) {
        Events = new RepositoryEvents<Employee> {
            OnBeforeSaving = async employee => logger.LogInformation("Before saving employee {Id}", employee.Id),
            OnSaved = async employee => logger.LogInformation("Saved employee {Id}", employee.Id),
            OnSavingFailed = async (employee, exception) => logger.LogError("Saving employee {Id} failed with message {Message}", employee.Id, exception.Message)
        };
    }
}

If you don't want to pollute your repository with logging statements, then you can register the RepositoryEvents<T> with your dependency container framework, such as below.

public class EmployeeRepositoryWithEvents : EntityRepository<Employee> {
    public EmployeeRepositoryWithEvents(DbContext context, RepositoryEvents<Employee> events) :
        base(context, events) { }
}

// In Startup.cs (or elsewhere)
services.AddRepository<Employee, EmployeeTestRepository>()
  .WithEvents<Employee>(_ => {
    OnBeforeSaving = async employee => logger.LogInformation("Before saving employee {Id}", employee.Id),
    OnSaved = async employee => logger.LogInformation("Saved employee {Id}", employee.Id),
    OnSavingFailed = async (employee, exception) => logger.LogError("Saving employee {Id} failed with message {Message}", employee.Id, exception.Message)
  });

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.