SOLID & Design Patterns Interview Question #81

What is the difference between a domain repository and a generic CRUD repository?

DDD Boundaries, Resilience, Caching & Practical ASP.NET Core Design Mid-Level Intermediate

Quick Interview Answer

A domain repository exposes operations meaningful to a particular aggregate or business use case. A generic CRUD repository exposes broad operations such as Add, Update, Delete, and Get for arbitrary entity types.

Detailed Explanation

A domain repository exposes operations meaningful to a particular aggregate or business use case. A generic CRUD repository exposes broad operations such as Add, Update, Delete, and Get for arbitrary entity types.

Domain repositories can preserve aggregate boundaries and express intent more clearly. Generic repositories may be sufficient for simple applications but can become restrictive or leak persistence concerns in complex domains.

Code Example

public interface IOrderRepository
{
    Task<Order?> GetPendingOrderAsync(Guid customerId);
    Task AddAsync(Order order);
}

// More expressive than only:
IRepository<Order>