CleanArch.Repository.EntityFramework 0.3.1

dotnet add package CleanArch.Repository.EntityFramework --version 0.3.1
                    
NuGet\Install-Package CleanArch.Repository.EntityFramework -Version 0.3.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CleanArch.Repository.EntityFramework" Version="0.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CleanArch.Repository.EntityFramework" Version="0.3.1" />
                    
Directory.Packages.props
<PackageReference Include="CleanArch.Repository.EntityFramework" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CleanArch.Repository.EntityFramework --version 0.3.1
                    
#r "nuget: CleanArch.Repository.EntityFramework, 0.3.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CleanArch.Repository.EntityFramework@0.3.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CleanArch.Repository.EntityFramework&version=0.3.1
                    
Install as a Cake Addin
#tool nuget:?package=CleanArch.Repository.EntityFramework&version=0.3.1
                    
Install as a Cake Tool

CleanArchitecture Repository EntityFramework

NuGet

CleanArch.Repository.EntityFramework provides a clean, extensible, and EF Core–optimized implementation of the Repository & Unit of Work patterns for Clean Architecture solutions.
It integrates seamlessly with:

  • CleanArch.Repository
  • CleanArch.Specification
  • Entity Framework Core DbContext

This package allows you to keep your domain/application layers persistence-agnostic while enabling powerful data access backed by EF Core.


✨ Features

  • Production-ready EF Core repository implementation
  • Specification pattern support for advanced querying
  • Full Unit of Work support with transaction management
  • Async-first APIs
  • Plug‑and‑play dependency injection configuration
  • Clean Architecture friendly — zero EF Core references in your Domain or Application layers

📦 Installation

Install via NuGet:

dotnet add package CleanArch.Repository.EntityFramework

⚙️ Setup

1. Register EF Core Repository + Unit of Work

Add the following to your Startup.cs or Program.cs:

services.AddDbContext<AppDbContext>(options =>
{
    options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
});

services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IReadRepository<>), typeof(EfRepository<>));
services.AddScoped<IUnitOfWork, EfUnitOfWork>();

Now your services can request IRepository<T> or IUnitOfWork without referencing EF Core directly.


🧱 EfRepository Overview

Key responsibilities:

  • Execute CRUD operations using EF Core
  • Apply Specifications via ApplySpecification
  • Wrap DbSet<T> access
  • Persist changes via IUnitOfWork

Example constructor:

public class EfRepository<T> : IRepository<T> where T : class
{
    private readonly AppDbContext _dbContext;

    public EfRepository(AppDbContext dbContext)
    {
        _dbContext = dbContext;
    }
}

🚀 Usage Examples


1. Basic CRUD with Repository

public class ProductService
{
    private readonly IRepository<Product> _repo;
    private readonly IUnitOfWork _uow;

    public ProductService(IRepository<Product> repo, IUnitOfWork uow)
    {
        _repo = repo;
        _uow = uow;
    }

    public async Task<Guid> CreateProductAsync(string name, decimal price)
    {
        var product = new Product(name, price);

        await _repo.AddAsync(product);
        await _uow.CommitAsync();

        return product.Id;
    }
}

2. Using Specification with EF Repository

Specification:

public class ExpensiveProductsSpecification : Specification<Product>
{
    public ExpensiveProductsSpecification(decimal minPrice)
    {
        Query.Where(p => p.Price >= minPrice);
    }
}

Usage:

var spec = new ExpensiveProductsSpecification(1000);

var results = await _repo.ListAsync(spec);

EfRepository will automatically apply .Where(), .Include(), sorting, paging, etc.


3. Transactional Operations via Unit of Work

public async Task ProcessOrder(Guid orderId)
{
    var order = await _repo.GetByIdAsync(orderId);
    order.MarkAsProcessed();

    await _uow.BeginTransactionAsync();

    try
    {
        await _repo.UpdateAsync(order);
        await _uow.CommitTransactionAsync();
    }
    catch
    {
        await _uow.RollbackTransactionAsync();
        throw;
    }
}

4. Using EF Core Includes via Specification

public class OrderWithItemsSpecification : Specification<Order>
{
    public OrderWithItemsSpecification(Guid id)
    {
        Query.Where(o => o.Id == id)
             .Include(o => o.Items);
    }
}

Usage:

var order = await _repo.FirstOrDefaultAsync(new OrderWithItemsSpecification(orderId));

🧪 Unit Testing

You can test your Application layer using:

  • In-memory EF Core
  • Fake repository
  • Mock repository (Moq, NSubstitute, etc.)

Example with EF InMemory:

var options = new DbContextOptionsBuilder<AppDbContext>()
    .UseInMemoryDatabase("TestDb")
    .Options;

var dbContext = new AppDbContext(options);
var repo = new EfRepository<Product>(dbContext);

🤝 Contributing

Contributions are welcome.
Please ensure code is tested, documented, and follows Clean Architecture principles.


📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.1 486 12/11/2025