Mastering EF Core: Unit of Work, Repository Patterns, and Transactions

In modern software development, managing database operations efficiently while maintaining code clarity is crucial for building robust, scalable applications. Two powerful design patterns that help achieve this are the Unit of Work and Repository patterns. Together, they provide a structured approach to handling data access, ensuring that your application remains modular, testable, and resilient. Additionally, by incorporating database transactions into the Unit of Work pattern, you can further enhance the integrity and reliability of your data operations.

The Repository Pattern: A Foundation for Data Access

The Repository pattern is a design pattern that provides a clean abstraction layer over the data access logic. It allows you to manage entities without exposing the complexities of the underlying data storage mechanism, thereby keeping your business logic clean and focused.

Key Benefits of the Repository Pattern:
  • Abstraction: It abstracts the data access layer, making your business logic independent of the data storage mechanism.
  • Separation of Concerns: By separating the data access logic from the business logic, it improves code readability and reduces duplication.
  • Testability: The abstraction facilitates unit testing by allowing easy mocking of the data layer.
Implementing the Repository Pattern

Let’s start by implementing a generic repository interface that handles basic CRUD (Create, Read, Update, Delete) operations:

public interface IRepository<T> where T : class
{
    Task<T> GetByIdAsync(int id);
    Task<IEnumerable<T>> GetAllAsync();
    Task AddAsync(T entity);
    void Update(T entity);
    void Delete(T entity);
}

This interface defines a set of methods that can be applied to any entity, providing consistent data access operations across your application.

Here’s the implementation of this interface for Entity Framework Core:

public class Repository<T> : IRepository<T> where T : class
{
    private readonly DbContext _context;
    private readonly DbSet<T> _dbSet;

    public Repository(DbContext context)
    {
        _context = context;
        _dbSet = _context.Set<T>();
    }

    public async Task<T> GetByIdAsync(int id)
    {
        return await _dbSet.FindAsync(id);
    }

    public async Task<IEnumerable<T>> GetAllAsync()
    {
        return await _dbSet.ToListAsync();
    }

    public async Task AddAsync(T entity)
    {
        await _dbSet.AddAsync(entity);
    }

    public void Update(T entity)
    {
        _dbSet.Update(entity);
    }

    public void Delete(T entity)
    {
        _dbSet.Remove(entity);
    }
}

This implementation provides a reusable, type-safe way to handle data access for any entity in your application.

The Unit of Work Pattern: Managing Transactions

While the Repository pattern is great for managing individual entities, many real-world scenarios involve operations that span multiple entities. The Unit of Work pattern ensures that all these operations are executed within a single, cohesive transaction, maintaining data integrity and consistency.

Key Benefits of the Unit of Work Pattern:
  • Transactional Integrity: All operations within a transaction are either committed together or not at all, ensuring consistency.
  • Centralized Control: It provides a single point of control over multiple repositories, making your data access code more organized.
  • Concurrency Handling: The Unit of Work pattern helps manage concurrency issues by coordinating updates to the data source.
Implementing the Unit of Work Pattern

Let’s consider a scenario where you need to manage both customer data and their associated orders in a single transaction. We can implement the Unit of Work pattern to ensure that all changes are committed together.

First, define a Unit of Work interface:

public interface IUnitOfWork : IDisposable
{
    IRepository<Customer> Customers { get; }
    IRepository<Order> Orders { get; }
    Task<int> SaveChangesAsync();
}

This interface exposes repositories for different entities (like Customers and Orders) and provides a SaveChangesAsync method to commit all operations as a single transaction.

Next, implement the Unit of Work:

public class UnitOfWork : IUnitOfWork
{
    private readonly DbContext _context;

    public UnitOfWork(DbContext context)
    {
        _context = context;
        Customers = new Repository<Customer>(_context);
        Orders = new Repository<Order>(_context);
    }

    public IRepository<Customer> Customers { get; private set; }
    public IRepository<Order> Orders { get; private set; }

    public async Task<int> SaveChangesAsync()
    {
        return await _context.SaveChangesAsync();
    }

    public void Dispose()
    {
        _context.Dispose();
    }
}

This implementation ensures that all the database operations are coordinated and committed together, maintaining the integrity of your data.

Adding Database Transactions to the Unit of Work

One of the most powerful aspects of the Unit of Work pattern is the ability to handle database transactions seamlessly. In scenarios where you need to ensure that a set of operations either all succeed or all fail, using transactions within the Unit of Work is essential.

Here’s how you can integrate transactions into the Unit of Work pattern:

public class UnitOfWork : IUnitOfWork
{
    private readonly DbContext _context;
    private IDbContextTransaction _transaction;

    public UnitOfWork(DbContext context)
    {
        _context = context;
        Customers = new Repository<Customer>(_context);
        Orders = new Repository<Order>(_context);
    }

    public IRepository<Customer> Customers { get; private set; }
    public IRepository<Order> Orders { get; private set; }

    public async Task BeginTransactionAsync()
    {
        _transaction = await _context.Database.BeginTransactionAsync();
    }

    public async Task CommitAsync()
    {
        try
        {
            await _context.SaveChangesAsync();
            await _transaction.CommitAsync();
        }
        catch
        {
            await _transaction.RollbackAsync();
            throw;
        }
    }

    public async Task RollbackAsync()
    {
        await _transaction.RollbackAsync();
    }

    public void Dispose()
    {
        _context.Dispose();
    }
}

In this implementation:

  • BeginTransactionAsync(): Starts a new database transaction.
  • CommitAsync(): Commits the transaction if all operations succeed. If an exception occurs, it rolls back the transaction to maintain data integrity.
  • RollbackAsync(): Explicitly rolls back the transaction if needed.

Using Unit of Work with Transactions in Practice

Let’s see how these patterns work together in a service class, handling multiple operations within a transaction:

public class PurchaseOrderService
{
    private readonly IUnitOfWork _unitOfWork;

    public PurchaseOrderService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task AddPurchaseOrderAsync(Customer customer, Order order)
    {
        await _unitOfWork.BeginTransactionAsync();
        try
        {
            await _unitOfWork.Customers.AddAsync(customer);
            await _unitOfWork.Orders.AddAsync(order);
            await _unitOfWork.CommitAsync();
        }
        catch
        {
            await _unitOfWork.RollbackAsync();
            throw;
        }
    }

    // Other business logic methods
}

In this example, the PurchaseOrderService class performs operations on both the Customer and Order entities within a single transaction. The use of BeginTransactionAsyncCommitAsync, and RollbackAsync ensures that these operations are executed as an atomic unit—either all succeed, or all fail, preserving data integrity.

Conclusion

By combining the Repository and Unit of Work patterns with database transactions, you can create a data access layer that is not only modular and testable but also robust in maintaining data integrity. The Repository pattern provides a clean abstraction for data operations, while the Unit of Work pattern ensures that multiple operations are managed as a cohesive transaction.

Incorporating transactions into the Unit of Work pattern further enhances your application’s reliability, ensuring that complex operations involving multiple entities are executed safely and consistently. As you build out your applications, leveraging these patterns will help you maintain clean, organized, and resilient code, making your software easier to maintain and extend over time.

And Keep Learning till the Next Time

Assalamualaikum - Allahuma Barik
Peace Out✌️

Post a Comment

0Comments
Post a Comment (0)