Pavas.Patterns.UnitOfWork
1.0.4
See the version list below for details.
dotnet add package Pavas.Patterns.UnitOfWork --version 1.0.4
NuGet\Install-Package Pavas.Patterns.UnitOfWork -Version 1.0.4
<PackageReference Include="Pavas.Patterns.UnitOfWork" Version="1.0.4" />
paket add Pavas.Patterns.UnitOfWork --version 1.0.4
#r "nuget: Pavas.Patterns.UnitOfWork, 1.0.4"
// Install Pavas.Patterns.UnitOfWork as a Cake Addin #addin nuget:?package=Pavas.Patterns.UnitOfWork&version=1.0.4 // Install Pavas.Patterns.UnitOfWork as a Cake Tool #tool nuget:?package=Pavas.Patterns.UnitOfWork&version=1.0.4
Pavas.Patterns.UnitOfWork Library
Overview
This library provides a Unit of Work and Repository pattern implementation for managing database interactions using Entity Framework Core. It includes support for soft delete, tenant management, correlation IDs, and configurable repository options. Below is the detailed documentation for each part of the library.
Table of Contents
DatabaseContext
Summary
The DatabaseContext
class is an abstract base class for configuring a database context in EF Core. It includes
mechanisms for handling soft delete, multi-tenancy, and auditing (timestamps).
Example Usage
public class MyDbContext : DatabaseContext
{
protected override void GetProvider(DbContextOptionsBuilder optionsBuilder, string connectionString)
{
optionsBuilder.UseSqlServer(connectionString);
}
}
Repository
Summary
The Repository<TEntity>
class is a generic repository for managing data access for entities. It includes methods for
retrieving, adding, updating, and removing entities asynchronously. It is automatically provided through the
UnitOfWork
, and you do not need to implement it manually.
Example Usage
var repository = await unitOfWork.GetRepositoryAsync<MyEntity>();
var entity = await repository.GetByIdAsync(1);
await repository.AddAsync(new MyEntity { Name = "New Entity" });
UnitOfWork
Summary
The UnitOfWork
class provides a mechanism for managing transactions and repositories within a database context. It
allows for retrieving repositories, managing database transactions, and saving changes. You do not need to manually
implement repositories; UnitOfWork
provides them.
Example Usage
var unitOfWork = serviceProvider.GetRequiredService<IUnitOfWork>();
var repository = await unitOfWork.GetRepositoryAsync<MyEntity>();
await unitOfWork.SaveChangesAsync();
Contracts
ICorrelated
Defines an entity that is associated with a correlation ID for tracking related actions.
Example
public class MyEntity : ICorrelated
{
public string CorrelationId { get; set; }
}
IDatabaseOptions
Defines the configuration options for the database, including the connection string and soft delete settings.
Example
public class MyDatabaseOptions : IDatabaseOptions
{
public string ConnectionString { get; set; } = "your-connection-string";
public string TenantId { get; set; } = "your-connection-string";
public string CorrelationId { get; set; } = "6e343h3f3434h3jj3434v3ggg34"; // use in scope or transient injection
public bool SoftDelete { get; set; } = true;
public bool EnsureCreated { get; set; } = true;
}
ISoftDelete
Represents an entity that supports soft delete functionality by maintaining a deletion timestamp.
Example
public class MyEntity : ISoftDelete
{
public bool IsDeleted { get; set; }
public DateTime? DeletedAt { get; set; }
}
ITenancy
Represents an entity associated with a tenant in a multi-tenant system.
Example
public class MyEntity : ITenancy
{
public string TenantId { get; set; }
}
ITimestamps
Represents an entity with common properties for auditing and tenant management.
Example
public class MyEntity : ITimestamps
{
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
}
IUnitOfWork
Defines a contract for the UnitOfWork pattern, including methods for retrieving repositories and managing transactions.
Example
var unitOfWork = serviceProvider.GetRequiredService<IUnitOfWork>();
var repository = await unitOfWork.GetRepositoryAsync<MyEntity>();
await unitOfWork.SaveChangesAsync();
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.