Core Interface
IRepository<TEntity>
Base repository interface providing standard CRUD operations.Core.Application.Repositories/IRepository.cs:5
Type Parameters:
TEntity- The domain entity type this repository manages
Repository Methods
Add Operations
Add
Adds a new entity synchronously.entity- The entity to add
object)
Example:
AddAsync
Adds a new entity asynchronously.entity- The entity to add
Application/UseCases/DummyEntity/Commands/CreateDummyEntity/CreateDummyEntityHandler.cs:32
Query Operations
FindAll
Retrieves all entities synchronously.FindAllAsync
Retrieves all entities asynchronously.Application/UseCases/DummyEntity/Queries/GetAllDummyEntities/GetAllDummyEntitiesHandler.cs:13
FindOne
Retrieves a single entity by its key(s) synchronously.keyValues- The primary key value(s)
FindOneAsync
Retrieves a single entity by its key(s) asynchronously.keyValues- The primary key value(s)
Application/UseCases/DummyEntity/Commands/UpdateDummyEntity/UpdateDummyEntityHandler.cs:17
Count
Counts entities matching a filter synchronously.filter- Lambda expression to filter entities
CountAsync
Counts entities matching a filter asynchronously.filter- Lambda expression to filter entities
Update Operations
Update
Updates an existing entity.id- The entity IDentity- The updated entity
Application/UseCases/DummyEntity/Commands/UpdateDummyEntity/UpdateDummyEntityHandler.cs:23
Delete Operations
Remove
Deletes an entity by its key(s).keyValues- The primary key value(s)
Application/UseCases/DummyEntity/Commands/DeleteDummyEntity/DeleteDummyEntityHandler.cs:20
Custom Repository Example
IDummyEntityRepository
Application-specific repository extending the base interface.Application/Repositories/IDummyEntityRepository.cs:11
Custom Repository Methods
Repositories can extendIRepository<T> with domain-specific methods:
Repository Pattern Benefits
Abstraction
Abstraction
- Application layer doesn’t know about EF Core, Dapper, or other data access technologies
- Easy to swap implementations (SQL Server → PostgreSQL, EF → Dapper)
- Enables testing with in-memory or mock implementations
Domain Focus
Domain Focus
- Works with domain entities, not database models
- Encapsulates data access logic
- Maintains clean separation of concerns
Testability
Testability
- Easy to create fake/mock implementations
- No database required for unit tests
- Predictable behavior in tests
Flexibility
Flexibility
- Add custom methods per entity type
- Implement complex queries in infrastructure
- Support multiple data sources
Usage Patterns
In Command Handlers
Commands use repositories for write operations:In Query Handlers
Queries use repositories for read operations:IRepository vs IReadRepository
While not explicitly shown in the codebase, many DDD implementations separate read and write repositories:| Interface | Purpose | Methods | Use In |
|---|---|---|---|
IRepository<T> | Full CRUD access | Add, Update, Remove, Find | Command handlers |
IReadRepository<T> | Read-only access | Find methods only | Query handlers |
Example IReadRepository
- Enforces CQRS separation at compile time
- Prevents accidental modifications in query handlers
- Clearer intent in code
Best Practices
Repository Scope
Repository Scope
- One repository per aggregate root
- Don’t create repositories for value objects or entities within aggregates
- Access child entities through the aggregate root
Method Design
Method Design
- Prefer async methods for all I/O operations
- Use
Expression<Func<T, bool>>for flexible filtering - Return domain entities, not DTOs
- Use nullable returns (
TEntity?) for “find” operations in C# 11+
Custom Methods
Custom Methods
- Add methods that express domain concepts:
FindOverdueOrders(),GetActiveUsers() - Encapsulate complex queries in repository methods
- Keep Application layer free of query logic
Dependency Injection
Dependency Injection
- Register repositories in DI container
- Inject repository interfaces, never concrete implementations
- Use constructor injection in handlers
Error Handling
Error Handling
- Let infrastructure layer throw exceptions
- Return null for “not found” scenarios
- Don’t catch and swallow exceptions in repositories
Related Concepts
- Commands - Use repositories for persistence
- Queries - Use repositories for data retrieval
- Infrastructure Layer - Repository implementations
- Unit of Work - Transaction management pattern