Voitovich.Patterns.UnitOfWork.EF 1.0.1

dotnet add package Voitovich.Patterns.UnitOfWork.EF --version 1.0.1
NuGet\Install-Package Voitovich.Patterns.UnitOfWork.EF -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.EF" 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.EF --version 1.0.1
#r "nuget: Voitovich.Patterns.UnitOfWork.EF, 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.EF as a Cake Addin
#addin nuget:?package=Voitovich.Patterns.UnitOfWork.EF&version=1.0.1

// Install Voitovich.Patterns.UnitOfWork.EF as a Cake Tool
#tool nuget:?package=Voitovich.Patterns.UnitOfWork.EF&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 Entity Framework)

  1. Install this nuget package
  2. Create DbContext of your database
    public sealed class ApplicationContext : DbContext
    {
        //...
    }
  1. In the Program.cs file, add the IUnitOfWork singleton with DbContext implementation to the services
builder.Services.AddSingleton<IUnitOfWork, UnitOfWork<ApplicationContext>>();
  1. Create your own repository interfaces inherited from IUowRepository
public interface ICarRepository : IUowRepository
  1. When implementing repository interfaces, create a constructor with the DbContext parameter
/// <inheritdoc/>
public sealed class CarRepository : ICarRepository
{
    public CarRepository(DbContext context)
    {
        //...
    }
    //...
  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();
        }
    }

For what

  • Move DbContext from BLL to DAL
  • Hide large linq instructions in repositories

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.

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 103 3/16/2024
1.0.0 96 3/16/2024

Add project URL