Tolitech.Infrastructure.Persistence.EntityFrameworkCore
1.0.0-preview.9
dotnet add package Tolitech.Infrastructure.Persistence.EntityFrameworkCore --version 1.0.0-preview.9
NuGet\Install-Package Tolitech.Infrastructure.Persistence.EntityFrameworkCore -Version 1.0.0-preview.9
<PackageReference Include="Tolitech.Infrastructure.Persistence.EntityFrameworkCore" Version="1.0.0-preview.9" />
<PackageVersion Include="Tolitech.Infrastructure.Persistence.EntityFrameworkCore" Version="1.0.0-preview.9" />
<PackageReference Include="Tolitech.Infrastructure.Persistence.EntityFrameworkCore" />
paket add Tolitech.Infrastructure.Persistence.EntityFrameworkCore --version 1.0.0-preview.9
#r "nuget: Tolitech.Infrastructure.Persistence.EntityFrameworkCore, 1.0.0-preview.9"
#:package Tolitech.Infrastructure.Persistence.EntityFrameworkCore@1.0.0-preview.9
#addin nuget:?package=Tolitech.Infrastructure.Persistence.EntityFrameworkCore&version=1.0.0-preview.9&prerelease
#tool nuget:?package=Tolitech.Infrastructure.Persistence.EntityFrameworkCore&version=1.0.0-preview.9&prerelease
Tolitech.Infrastructure.Persistence.EntityFrameworkCore
A robust .NET library that provides foundational infrastructure for implementing the Repository and Unit of Work patterns with Entity Framework Core. It enables transactional consistency, domain event publishing, and integration event outbox support for modern, scalable applications.
Features
- Generic Repository Base: Abstract base for repositories, simplifying CRUD operations.
- Unit of Work Pattern: Transaction management with support for nested transactions.
- Domain Events: Automatic collection and publishing of domain events after persistence.
- Integration Events (Outbox Pattern): Reliable persistence of integration events for eventual consistency.
- Exception Handling: Centralized constraint violation handling.
- Logging: Transaction and event lifecycle logging.
Installation
Add the NuGet package to your project:
dotnet add package Tolitech.Infrastructure.Persistence.EntityFrameworkCore
Getting Started
1. Implement a Repository
Create a repository by inheriting from Repository<TEntity>
:
using Tolitech.Infrastructure.Persistence.EntityFrameworkCore;
public class CustomerRepository : Repository<Customer>
{
public CustomerRepository(MyDbContext dbContext) : base(dbContext) { }
}
2. Implement a Unit of Work
Create a Unit of Work by inheriting from UnitOfWorkBase
:
using Tolitech.Infrastructure.Persistence.EntityFrameworkCore;
using MediatR;
using Microsoft.Extensions.Logging;
public class MyUnitOfWork : UnitOfWorkBase
{
public MyUnitOfWork(MyDbContext dbContext, IPublisher publisher, ILogger<MyUnitOfWork> logger)
: base(dbContext, publisher, logger) { }
}
3. Using Transactions and Saving Changes
using (var uow = new MyUnitOfWork(dbContext, publisher, logger))
{
await uow.BeginTransactionAsync(cancellationToken);
// ... perform repository operations ...
await uow.SaveChangesAsync(cancellationToken);
await uow.CommitAsync(cancellationToken);
}
Or atomically:
await uow.SaveChangesInTransactionAsync(cancellationToken);
4. Domain Events
Entities implementing IAggregate
can raise domain events. These are automatically published after SaveChangesAsync
if a transaction is open.
5. Integration Events (Outbox)
If using the outbox pattern, pass an IOutbox
to the Unit of Work. Integration events are persisted and can be processed later for reliable messaging.
Exception Handling
Constraint violations are handled and wrapped using the ConstraintValidatorManager
for consistent error management.
Example: Custom Exception
try
{
await uow.SaveChangesAsync(cancellationToken);
}
catch (MissingOpenTransactionException ex)
{
// Handle missing transaction scenario
}
Advanced
- Isolation Levels: Use
BeginTransactionAsync(IsolationLevel, CancellationToken)
for custom isolation. - Commit or Rollback by Result: Use
CommitOrRollbackAsync(Result, CancellationToken)
to commit or rollback based on operation result.
Tolitech.Infrastructure.Persistence.EntityFrameworkCore brings transactional consistency, event-driven architecture, and repository abstraction to your .NET applications with Entity Framework Core.
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
- MediatR (>= 12.5.0)
- Microsoft.EntityFrameworkCore (>= 9.0.6)
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.6)
- Tolitech.Application (>= 1.0.0-preview.10)
- Tolitech.Application.Messaging (>= 1.0.0-preview.3)
- Tolitech.Domain (>= 1.0.0-preview.8)
- Tolitech.Infrastructure.Messaging.EntityFrameworkCore (>= 1.0.0-preview.4)
- Tolitech.Infrastructure.Persistence.ConstraintValidator (>= 1.0.0-preview.5)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Tolitech.Infrastructure.Persistence.EntityFrameworkCore:
Package | Downloads |
---|---|
Tolitech.Infrastructure.Persistence.EntityFrameworkCore.Specifications
The Tolitech.Infrastructure.Persistence.EntityFrameworkCore.Specifications repository provides a foundational implementation for the Specifications pattern. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0-preview.9 | 11 | a day ago |
1.0.0-preview.8 | 105 | 6 days ago |
1.0.0-preview.7 | 111 | 19 days ago |
1.0.0-preview.6 | 109 | 19 days ago |
1.0.0-preview.5 | 157 | 3 months ago |
1.0.0-preview.4 | 133 | 3 months ago |
1.0.0-preview.3 | 80 | 6 months ago |
1.0.0-preview.2 | 71 | 7 months ago |
1.0.0-preview.1 | 78 | 7 months ago |