Nerv.Repository
1.0.2
See the version list below for details.
dotnet add package Nerv.Repository --version 1.0.2
NuGet\Install-Package Nerv.Repository -Version 1.0.2
<PackageReference Include="Nerv.Repository" Version="1.0.2" />
<PackageVersion Include="Nerv.Repository" Version="1.0.2" />
<PackageReference Include="Nerv.Repository" />
paket add Nerv.Repository --version 1.0.2
#r "nuget: Nerv.Repository, 1.0.2"
#:package Nerv.Repository@1.0.2
#addin nuget:?package=Nerv.Repository&version=1.0.2
#tool nuget:?package=Nerv.Repository&version=1.0.2
Nerv.Repository
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>andIReadOnlyRepository<T> IUnitOfWorkwith transaction support- Audit support (
CreatedOn,UpdatedOn,DeletedOn,CreatedBy, etc.) - Soft delete support
- Actor context integration for automatic user tracking
- Global
DbContextBasewith auto-audit and soft-delete filters - Pagination and filtering utilities
- Repository model configuration via
UnitOfWorkOptions(including table name pluralization using Humanizer) - 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>(
// DbContext configuration
options =>
{
// Retrieve connection string from configuration
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
// Configure EF Core provider (in this case, SQL Server)
options.UseSqlServer(connectionString);
},
// ActorContext factory configuration (used for audit tracking)
provider =>
{
// Resolve IHttpContextAccessor to access the current HTTP context
var httpContextAccessor = provider.GetRequiredService<IHttpContextAccessor>();
var httpContext = httpContextAccessor.HttpContext;
// Extract the user ID from claims (e.g. OpenID Connect "sub" claim)
var userIdClaim = httpContext?.User?.FindFirst("sub")?.Value;
// Parse the user ID or fallback to Guid.Empty if not authenticated
Guid userId = userIdClaim != null
? Guid.Parse(userIdClaim)
: Guid.Empty;
// Return ActorContext to be injected into DbContextBase
return new ActorContext<Guid> { UserId = userId };
},
// UnitOfWork options configuration (repository model customization)
uowOptions =>
{
// Enable pluralization of table names (e.g. User -> Users)
uowOptions.RepositoryModelOptions.UsePluralization = true;
}
);
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.Repository<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/ # Interfaces and contracts
Contexts/ # DbContext and actor context
Extensions/ # Extension methods
LogEntities/ # Entities for audit logging
Entities/ # Domain entities
Models/ # Supporting models (e.g. pagination)
Options/ # Configuration options (UnitOfWorkOptions, RepositoryModelOptions)
tests/
Nerv.Repository.Tests/
Context/ # Test DbContext definitions
Entities/ # Test entities
Fixtures/ # Test setup utilities
Helpers/ # Shared test helpers (e.g. UnitOfWorkOptions factory)
Repositories/ # Repository-specific tests
UnitOfWork/ # UnitOfWork-specific tests
scripts/
clean-empty-lines.sh # Empty line cleanup script
Nerv.Repository.sln # Solution file at repository root
.editorconfig # Consistent formatting rules
.vscode/settings.json # VSCode editor configuration
.git/hooks/pre-commit # Pre-commit hook for automatic cleanup
๐ License
This project is licensed under the MIT License.
๐ค Contributing
Feel free to open issues or pull requests. Contributions are welcome!
๐ Related Projects
- Entity Framework Core
- Humanizer โ Used for automatic pluralization of table names
| Product | Versions 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. |
-
net9.0
- Humanizer (>= 2.14.1)
- Microsoft.EntityFrameworkCore (>= 9.0.6)
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.