Skip to content

A Generic Repository Implementation for Entity Framework. The generic repository completely abstracts Entity Framework out of your business layer.

License

Notifications You must be signed in to change notification settings

decius999/CleanCodeJNGenericRepository

Repository files navigation

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(x => x.Invoices) // Use to Include dependent tables, e.g: Invoices
                         .Where(x => x.Invoice.IsPayed)
                         .ToList()

Sample Code

GitHub Full Sample

About

A Generic Repository Implementation for Entity Framework. The generic repository completely abstracts Entity Framework out of your business layer.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages