Aller au contenu principal
Nicolas Cousin Tech SolutionsNicolas Cousin Tech Solutions
Module 3 of 6

Details of the Unit of Work Pattern

Unit of Work Pattern: Managing Transactions

The Unit of Work Pattern is designed to group multiple operations into a single coherent transaction. It ensures that all modifications are either successfully executed or rolled back in case of failure.

Unit of Work Interface Example

public interface IUnitOfWork : IDisposable
{
    IRepository<T> Repository<T>() where T : class;
    Task<int> SaveChangesAsync();
}

Unit of Work Implementation

Here is a basic implementation:

public class UnitOfWork : IUnitOfWork
{
    private readonly DbContext _context;
    private readonly Dictionary<string, object> _repositories = new();

    public UnitOfWork(DbContext context)
    {
        _context = context;
    }

    public IRepository<T> Repository<T>() where T : class
    {
        var typeName = typeof(T).Name;

        if (!_repositories.ContainsKey(typeName))
        {
            var repositoryInstance = new Repository<T>(_context);
            _repositories[typeName] = repositoryInstance;
        }

        return (IRepository<T>)_repositories[typeName];
    }

    public async Task<int> SaveChangesAsync() => await _context.SaveChangesAsync();

    public void Dispose() => _context.Dispose();
}

The result: transaction management stays isolated, and every repository is coordinated from a single entry point.

Check your understanding

What is the main role of the Unit of Work pattern?

In the module's implementation, what is the _repositories dictionary used for?