Pavas.Patterns.UnitOfWork 1.0.3

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.3                
NuGet\Install-Package Pavas.Patterns.UnitOfWork -Version 1.0.3                
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.3" />                
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.3                
#r "nuget: Pavas.Patterns.UnitOfWork, 1.0.3"                
#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.3

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

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 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 95 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