Voitovich.Patterns.UnitOfWork.Dapper
1.0.0
See the version list below for details.
dotnet add package Voitovich.Patterns.UnitOfWork.Dapper --version 1.0.0
NuGet\Install-Package Voitovich.Patterns.UnitOfWork.Dapper -Version 1.0.0
<PackageReference Include="Voitovich.Patterns.UnitOfWork.Dapper" Version="1.0.0" />
paket add Voitovich.Patterns.UnitOfWork.Dapper --version 1.0.0
#r "nuget: Voitovich.Patterns.UnitOfWork.Dapper, 1.0.0"
// Install Voitovich.Patterns.UnitOfWork.Dapper as a Cake Addin #addin nuget:?package=Voitovich.Patterns.UnitOfWork.Dapper&version=1.0.0 // Install Voitovich.Patterns.UnitOfWork.Dapper as a Cake Tool #tool nuget:?package=Voitovich.Patterns.UnitOfWork.Dapper&version=1.0.0
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)
- Install this nuget package
- Create an IDbConnection inheritor class
public sealed class MyAppConnection : SqliteConnection
{
}
- 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));
- Create your own repository interfaces inherited from IUowRepository
public interface ICarRepository : IUowRepository
- 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;
//...
}
//...
- 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.
- 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 | 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
- 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.