Entity Framework Core Interview Question #173

Should the repository pattern always be placed on top of EF Core?

Advanced Mapping, Interceptors, Value Conversions & Architecture Senior Advanced

Quick Interview Answer

Not necessarily. DbContext already provides unit-of-work behavior and DbSet provides repository-like access. A custom repository can be useful when it expresses meaningful domain queries, hides multiple data sources, or creates a deliberate application boundary. A generic repository that merely duplicates every EF Core method can add abstraction without adding value.

Detailed Explanation

Not necessarily. DbContext already provides unit-of-work behavior and DbSet provides repository-like access. A custom repository can be useful when it expresses meaningful domain queries, hides multiple data sources, or creates a deliberate application boundary. A generic repository that merely duplicates every EF Core method can add abstraction without adding value.

Code Example

public interface IOrderRepository
{
    Task<Order?> GetForCheckoutAsync(
        int id, CancellationToken ct);
}