Pavas.Patterns.UnitOfWork
1.0.1
See the version list below for details.
dotnet add package Pavas.Patterns.UnitOfWork --version 1.0.1
NuGet\Install-Package Pavas.Patterns.UnitOfWork -Version 1.0.1
<PackageReference Include="Pavas.Patterns.UnitOfWork" Version="1.0.1" />
paket add Pavas.Patterns.UnitOfWork --version 1.0.1
#r "nuget: Pavas.Patterns.UnitOfWork, 1.0.1"
// Install Pavas.Patterns.UnitOfWork as a Cake Addin #addin nuget:?package=Pavas.Patterns.UnitOfWork&version=1.0.1 // Install Pavas.Patterns.UnitOfWork as a Cake Tool #tool nuget:?package=Pavas.Patterns.UnitOfWork&version=1.0.1
Pavas Patterns - Unit of Work Library
This repository contains an implementation of the Unit of Work design pattern along with a repository pattern, soft delete functionality, multi-tenancy, and timestamp auditing for Entity Framework Core (EF Core). The library is designed to help manage data access and context lifecycle more efficiently, by handling common database operations such as creation, modification, and soft deletion of entities.
Features
- Unit of Work pattern: Centralized management of database transactions and repositories.
- Repository pattern: Encapsulates data access logic and simplifies querying and manipulation of entities.
- Soft Delete: Allows entities to be "soft deleted", marking them as deleted without removing them from the database.
- Multi-tenancy: Provides support for managing entities across different tenants.
- Timestamp Auditing: Automatically tracks entity creation and update times.
Usage
Database Context
The DatabaseContext
class is an abstract base class that configures the EF Core context with support for:
- Soft delete (
ISoftDelete
) - Multi-tenancy (
ITenancy
) - Timestamps (
ITimestamps
)
Example implementation:
public class MyDbContext : DatabaseContext
{
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public DbSet<MyEntity> MyEntities { get; set; }
}
Unit of Work
The IUnitOfWork
interface provides methods to manage transactions and retrieve repositories. It ensures that all
operations are managed within a single transaction scope.
Example usage:
public class MyService
{
private readonly IUnitOfWork _unitOfWork;
public MyService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<MyEntity> GetEntityAsync(int id)
{
var repository = _unitOfWork.GetRepository<MyEntity>();
return await repository.GetByIdAsync(id);
}
public async Task SaveChangesAsync()
{
await _unitOfWork.SaveChangesAsync();
}
}
Repository Pattern
The IRepository<TEntity>
interface defines the repository pattern for CRUD operations on entities.
public interface IRepository<TEntity> where TEntity : class
{
Task<TEntity?> GetByIdAsync(int id);
Task<IEnumerable<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> filter);
Task AddAsync(TEntity entity);
Task UpdateAsync(TEntity entity);
Task RemoveAsync(TEntity entity);
}
Soft Delete
Entities implementing ISoftDelete
will automatically support soft delete operations.
public class MyEntity : ISoftDelete
{
public int Id { get; set; }
public bool IsDeleted { get; set; }
public DateTime? DeletedAt { get; set; }
}
Multi-tenancy
Entities implementing ITenancy
will have a TenantId
that can be used to manage data across different tenants.
public class MyEntity : ITenancy
{
public string TenantId { get; set; }
}
Dependency Injection
To register the UnitOfWork
and DbContext
, you can use the provided AddUnitOfWork
extension methods.
services.AddUnitOfWork<MyDbContext>(options =>
{
options.ConnectionString = "your-connection-string";
options.SoftDelete = true;
options.TenantId = "default-tenant";
}, ServiceLifetime.Scoped);
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.8)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.