Mid SOLID

Can you give an example where you combined Repository and Unit of Work patterns?

The Unit of Work coordinates multiple Repositories and commits changes in a single

transaction. For example:

public interface IUnitOfWork : IDisposable

IProductRepository Products { get; }

IOrderRepository Orders { get; }

int Complete();

public class UnitOfWork : IUnitOfWork

private readonly DbContext _context;

public IProductRepository Products { get; }

public IOrderRepository Orders { get; }

public UnitOfWork(DbContext context)

_context = context;

Products = new ProductRepository(_context);

Orders = new OrderRepository(_context);

public int Complete() => _context.SaveChanges();

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

This ensures atomic commits across repositories.

More from Design Patterns in C#

All questions for this course