Nerv.Repository 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Nerv.Repository --version 1.0.1
                    
NuGet\Install-Package Nerv.Repository -Version 1.0.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="Nerv.Repository" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nerv.Repository" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Nerv.Repository" />
                    
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 Nerv.Repository --version 1.0.1
                    
#r "nuget: Nerv.Repository, 1.0.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 Nerv.Repository@1.0.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=Nerv.Repository&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Nerv.Repository&version=1.0.1
                    
Install as a Cake Tool

Nerv.Repository

Repository NuGet License

A lightweight, flexible and extensible implementation of the Repository and Unit of Work patterns using Entity Framework Core.
Designed for clean architecture, testability, and ease of integration across any .NET application.


โœจ Features

  • Generic IRepository<T> and IReadOnlyRepository<T>
  • IUnitOfWork with transaction support
  • Audit support (CreatedOn, UpdatedOn, DeletedOn, CreatedBy, etc.)
  • Soft delete support
  • Actor context integration for automatic user tracking
  • Global DbContextBase with auto-audit and soft-delete filters
  • Pagination and filtering utilities
  • Ready-to-use in-memory tests

๐Ÿ“ฆ Installation

dotnet add package Nerv.Repository

๐Ÿ› ๏ธ Usage

1. Define your entity

public class User : Entity<Guid>
{
    public string Name { get; set; }
}

2. Configure your DbContext

public class AppDbContext : DbContextBase<Guid>
{
    public AppDbContext(DbContextOptions<AppDbContext> options, ActorContext<Guid> actor)
        : base(options, actor) { }
}

3. Register dependencies

var builder = WebApplication.CreateBuilder(args);

// Register IHttpContextAccessor (needed to access HTTP context)
builder.Services.AddHttpContextAccessor();

// Register everything using the AddRepositoryPattern extension
builder.Services.AddRepositoryPattern<AppDbContext, Guid>(
    options => 
    {
        var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
        options.UseSqlServer(connectionString);
    },
    provider =>
    {
        // This is the actor factory, resolved per request
        var httpContextAccessor = provider.GetRequiredService<IHttpContextAccessor>();
        var httpContext = httpContextAccessor.HttpContext;

        var userIdClaim = httpContext?.User?.FindFirst("sub")?.Value;

        Guid userId = userIdClaim != null 
            ? Guid.Parse(userIdClaim)
            : Guid.Empty; // fallback if not authenticated

        return new ActorContext<Guid> { UserId = userId };
    }
);

4. Use in your services

You only need to inject IUnitOfWork in your services. All repositories are accessed through it:

public class UserService
{
    private readonly IUnitOfWork _unitOfWork;

    public UserService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task CreateUserAsync(string name)
    {
        var user = new User { Id = Guid.NewGuid(), Name = name };

        var userRepo = _unitOfWork.GetRepository<User>();
        await userRepo.AddAsync(user);
        await _unitOfWork.SaveChangesAsync();
    }
}

๐Ÿ” ActorContext for audit

All changes are tracked by ActorContext<TUserId>:

public class ActorContext<TUserId>
{
    public TUserId UserId { get; set; }
}

๐Ÿงช Testing

You can test your repositories and unit of work using the in-memory SQLite provider.

public class UserTests
{
    [Fact]
    public async Task Add_Should_Persist_User()
    {
        using var fixture = RepositoryFixture.Create();
        var user = new User { Id = Guid.NewGuid(), Name = "Test" };
        await fixture.Repository.AddAsync(user);
        await fixture.UnitOfWork.SaveChangesAsync();

        var result = await fixture.Repository.GetByIdAsync(user.Id);
        Assert.NotNull(result);
    }
}

๐Ÿงฑ Project Structure

src/
  Nerv.Repository/
    Abstractions/
    Contexts/
    Extensions/
    LogEntities/
tests/
  Nerv.Repository.Tests/
    Context/
    Entities/
    Fixtures/
    Repositories/
    UnitOfWork/

๐Ÿ“„ License

This project is licensed under the MIT License.


๐Ÿค Contributing

Feel free to open issues or pull requests. Contributions are welcome!


Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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
2.0.0 101 6/27/2025
1.0.2 109 6/21/2025
1.0.1 103 6/21/2025
1.0.0 145 6/18/2025