This generic Repository implementation for Entity Framework abstracts completely all EF-References from your business layer in a domain driven design manner.
- Ready to use in seconds
- Abstracts all EF specific Code out of your business layer
- Easy to mock and test
- On .NET 9.0
- 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
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()