Skip to content

Latest commit

 

History

History
65 lines (48 loc) · 1.76 KB

README.md

File metadata and controls

65 lines (48 loc) · 1.76 KB

Generic Repository Implementation

Repository Abstraction for Entity Framework

This generic Repository implementation for Entity Framework abstracts completely all EF-References from your business layer in a domain driven design manner.

Features

  • Ready to use in seconds
  • Abstracts all EF specific Code out of your business layer
  • Easy to mock and test
  • On latest .NET 8.0

How to use

  • Add IEntity interfaces to your domain classes
  • Add IDataContext Interface to your DBContext class
  • Use Extension RegisterRepositories() in your startup class or program.cs
  • Inject IRepository in your business layer
  • Just use It

Step by step explanation

Add IEntity interfaces to your domain classes:

public class Customer : IEntity<int>
{
    public int Id { get; set; }
}

Add IDataContext Interface to your DBContext class:

public partial class MyDbContext : DbContext, IDataContext
{   
}

Use Extension RegisterDbContextAndRepositories() in your startup class or program.cs:

// Just register generic repositories and your dbContext which has the IDataContext marker interface
builder.Services.RegisterDbContextAndRepositories<MyDbContext>();

Inject IRepository<TEntity, TKey> (or IIntRepository, IStringRepository, IGuidRepository, ILongRepository) in your business layer:

public class MyService(IRepository<Customer, int> repository)
{   
}

Just use it:

List<customer> customerWhoHavePayed = repository
                         .Query(asNoTracking: true, asSplitQuery: true, includes: x => x.Invoices)
                         .Where(x => x.Invoice.IsPayed)
                         .ToList()

Sample Code

GitHub Full Sample