Voitovich.Patterns.UnitOfWork.Dapper 1.0.1

dotnet add package Voitovich.Patterns.UnitOfWork.Dapper --version 1.0.1
NuGet\Install-Package Voitovich.Patterns.UnitOfWork.Dapper -Version 1.0.1
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="Voitovich.Patterns.UnitOfWork.Dapper" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Voitovich.Patterns.UnitOfWork.Dapper --version 1.0.1
#r "nuget: Voitovich.Patterns.UnitOfWork.Dapper, 1.0.1"
#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 Voitovich.Patterns.UnitOfWork.Dapper as a Cake Addin
#addin nuget:?package=Voitovich.Patterns.UnitOfWork.Dapper&version=1.0.1

// Install Voitovich.Patterns.UnitOfWork.Dapper as a Cake Tool
#tool nuget:?package=Voitovich.Patterns.UnitOfWork.Dapper&version=1.0.1

Unit of Work

Theory

Unit of Work is a pattern that defines a logical transaction, i.e. atomic synchronization of changes in objects placed in a UoW object with a repository (database). If we turn to the original description of this pattern by Martin Fowler, it can be seen that the object implementing this pattern is responsible for accumulating information about which objects are included in the transaction and what their changes are relative to the original values in the repository. The main work is done in the commit() method, which is responsible for calculating changes in objects stored in UoW and synchronizing these changes with the repository (database).

This implementation (with Dapper)

  1. Install this nuget package
  2. Create an IDbConnection inheritor class
    public sealed class MyAppConnection : SqliteConnection
    {
    }
  1. In the Program.cs file, add the IUnitOfWork singleton to the services with the new connection class from the previous paragraph
    const string ConnectionString = "Data Source=AppDatabase.db";
    builder.Services.AddSingleton<IUnitOfWork>(new UnitOfWork<MyAppConnection>(ConnectionString));
  1. Create your own repository interfaces inherited from IUowRepository
public interface ICarRepository : IUowRepository
  1. When implementing repository interfaces, create a constructor with the IDbConnection parameter
/// <inheritdoc/>
public sealed class CarRepository : ICarRepository
{
    private readonly MyAppConnection _connection;

    public CarRepository(IDbConnection connection)
    {
        _connection = (MyAppConnection)connection;
        //...
    }

    //...
  1. At the BLL, use StartTransaction() and CommitAsync() to ensure atomicity
public class DriverService : IDriverService
{
    private readonly IUnitOfWork _unitOfWork;
    //...

    public async Task DeleteAsync(long[] ids)
    {
        using (var uow = _unitOfWork.StartTransaction())
        {
            var cars = await uow.Repository<ICarRepository>().GetCarsByDriverIdsAsync(ids);
            await uow.Repository<ICarRepository>().DeleteAsync(cars.Select(x => x.Id));
            await uow.Repository<IDriverRepository>().DeleteAsync(ids);
            await uow.CommitAsync();
        }
    }

If execution reaches CommitAsync(), then all changes will be saved, otherwise all changes will be rolledback.

  1. If you don't need to change the data (don't need transactions), you can use the Selector()
public class DriverService : IDriverService
{
    private readonly IMapper _mapper;
    private readonly IUnitOfWork _unitOfWork;
    //...

    /// <inheritdoc/>
    public async Task<DriverDto[]> GetAsync(long[] ids)
    {
        using (var uow = _unitOfWork.Selector())
        {
            var cars = await uow.Repository<ICarRepository>().GetCarsByDriverIdsAsync(ids);
            var drivers = await uow.Repository<IDriverRepository>().GetAsync(ids);

            return drivers
                .Select(x =>
                {
                    var driver = _mapper.Map<DriverDto>(x);
                    driver.Cars = cars
                        .Where(z => z.DriverId == driver.Id)
                        .Select(z => _mapper.Map<CarDto>(z))
                        .ToArray();
                    return driver;
                })
                .ToArray();
        }
    }

License

MIT

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.
  • net8.0

    • No dependencies.

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.1 98 4/9/2024
1.0.0 83 4/6/2024

Correct namespaces