A Repository provides an abstraction over data access, making the domain layer independent of infrastructure concerns. It encapsulates the logic needed to access data sources.
Key principles:
One repository per aggregate root
Interface defined in domain layer, implementation in infrastructure
Provides collection-like interface for accessing domain objects
Hides database-specific details from the domain
// Domain layer interface
public interface IOrderRepository
{
Order GetById(OrderId id);
void Save(Order order);
IEnumerable<Order> GetOrdersByCustomer(CustomerId customerId);
}
// Infrastructure layer implementation
public class SqlOrderRepository : IOrderRepository
{
public Order GetById(OrderId id)
{
// Database-specific implementation
}
}
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.