RA.Utilities.Data.EntityFramework
· 2 min read
This is the initial preview release of the RA.Utilities.Data.EntityFramework package.
It provides concrete implementations of the repository and unit of work patterns for Entity Framework Core, based on the abstractions from RA.Utilities.Data.Abstractions.
✨ Key Features
-
Generic Repository Implementations:
RepositoryBase<T>: A full implementation ofIRepositoryBase<T>for complete CRUD functionality.ReadRepositoryBase<T>: A read-only repository that usesAsNoTracking()by default for efficient querying, ideal for CQS patterns.WriteRepositoryBase<T>: A write-only repository for command operations (Add, Update, Delete).
-
Generic Unit of Work Implementation:
UnitOfWork<TContext>: A generic implementation ofIUnitOfWorkthat manages theDbContextlifecycle and ensures transactional integrity by saving all changes atomically.
-
Dependency Injection Extensions:
- Includes
AddRepositoryBase(),AddReadRepositoryBase(), andAddWriteRepositoryBase()extension methods to simplify the registration of generic repositories in your application's DI container.
- Includes
🚀 Getting Started
-
Define Your DbContext: Create your
ApplicationDbContextinheriting fromDbContext.public class ApplicationDbContext : DbContext, IDbContext
{
// ... DbSets
} -
Register Services: In
Program.cs, register yourDbContext, theUnitOfWork, and your repositories.// Register DbContext
builder.Services.AddDbContext<ApplicationDbContext>(...);
// Register UnitOfWork and generic repositories
builder.Services.AddScoped<IUnitOfWork, UnitOfWork<ApplicationDbContext>>();
builder.Services.AddRepositoryBase(); // Registers IRepositoryBase<> -
Use in Your Application: Inject
IUnitOfWorkorIRepositoryBase<T>into your services to interact with the database.public class ProductService(IRepositoryBase<Product> productRepo, IUnitOfWork uow)
{
// ... use repository methods and uow.SaveChangesAsync()
}