Pavas.Patterns.UnitOfWork 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pavas.Patterns.UnitOfWork --version 1.0.6                
NuGet\Install-Package Pavas.Patterns.UnitOfWork -Version 1.0.6                
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="Pavas.Patterns.UnitOfWork" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pavas.Patterns.UnitOfWork --version 1.0.6                
#r "nuget: Pavas.Patterns.UnitOfWork, 1.0.6"                
#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.
// Install Pavas.Patterns.UnitOfWork as a Cake Addin
#addin nuget:?package=Pavas.Patterns.UnitOfWork&version=1.0.6

// Install Pavas.Patterns.UnitOfWork as a Cake Tool
#tool nuget:?package=Pavas.Patterns.UnitOfWork&version=1.0.6                

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 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 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. 
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
1.0.8 93 9/30/2024
1.0.7 83 9/30/2024
1.0.6 87 9/27/2024
1.0.5 81 9/27/2024
1.0.4 87 9/26/2024
1.0.3 82 9/24/2024
1.0.2 81 9/24/2024
1.0.1 92 9/24/2024
1.0.0 87 9/24/2024