Skip to main content

RA.Utilities.Data.EntityFramework

· 2 min read
Redon Alla
.NET, React, Angular Developer

NuGet version

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 of IRepositoryBase<T> for complete CRUD functionality.
    • ReadRepositoryBase<T>: A read-only repository that uses AsNoTracking() 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 of IUnitOfWork that manages the DbContext lifecycle and ensures transactional integrity by saving all changes atomically.
  • Dependency Injection Extensions:

    • Includes AddRepositoryBase(), AddReadRepositoryBase(), and AddWriteRepositoryBase() extension methods to simplify the registration of generic repositories in your application's DI container.

🚀 Getting Started

  1. Define Your DbContext: Create your ApplicationDbContext inheriting from DbContext.

    public class ApplicationDbContext : DbContext, IDbContext
    {
    // ... DbSets
    }
  2. Register Services: In Program.cs, register your DbContext, the UnitOfWork, 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<>
  3. Use in Your Application: Inject IUnitOfWork or IRepositoryBase<T> into your services to interact with the database.

    public class ProductService(IRepositoryBase<Product> productRepo, IUnitOfWork uow)
    {
    // ... use repository methods and uow.SaveChangesAsync()
    }