Implementing Use Cases
Use cases represent application-specific business rules. This architecture uses CQRS (Command Query Responsibility Segregation) to separate write operations (commands) from read operations (queries).Overview
Every use case consists of:- A Request (Command or Query)
- A Handler that processes the request
- Optional Application Services for cross-cutting concerns
Commands (Write Operations)
Commands modify system state and typically return an identifier or void.Create the Command
Create a command class that implements Example from codebase:
IRequestCommand<TResponse> or IRequestCommand (for void).Create Application/UseCases/YourEntity/Commands/CreateYourEntity/CreateYourEntityCommand.cs:Application/UseCases/DummyEntity/Commands/CreateDummyEntity/CreateDummyEntityCommand.cs:13-22Create the Command Handler
Create a handler that implements Example from codebase:
IRequestCommandHandler<TRequest, TResponse>.Create Application/UseCases/YourEntity/Commands/CreateYourEntity/CreateYourEntityHandler.cs:Application/UseCases/DummyEntity/Commands/CreateDummyEntity/CreateDummyEntityHandler.cs:17-43Queries (Read Operations)
Queries retrieve data without modifying state and return DTOs (Data Transfer Objects).Create the Query
Create a query class that implements Example from codebase:
IRequestQuery<TResponse>.Create Application/UseCases/YourEntity/Queries/GetYourEntityBy/GetYourEntityByQuery.cs:Application/UseCases/DummyEntity/Queries/GetDummyEntityBy/GetDummyEntityByQuery.cs:7-15Create the Query Handler
Create a handler that implements Example from codebase:
IRequestQueryHandler<TRequest, TResponse>.Create Application/UseCases/YourEntity/Queries/GetYourEntityBy/GetYourEntityByHandler.cs:Application/UseCases/DummyEntity/Queries/GetDummyEntityBy/GetDummyEntityByHandler.cs:8-17Application Services
Application services handle cross-cutting concerns that don’t belong in handlers.Key Concepts
Command vs Query
Command vs Query
Commands:
- Modify system state
- Can return an ID or void
- May publish domain events
- Validate before persisting
- Read-only operations
- Return DTOs, not domain entities
- No side effects
- Can use optimized read models
Handler Responsibilities
Handler Responsibilities
A handler should:
- Validate input
- Create/retrieve domain entities
- Execute business logic
- Persist changes (commands only)
- Publish events (commands only)
- Return results
- Contain complex business logic (use domain entities)
- Directly access external services (use application services)
- Return domain entities (use DTOs)
Error Handling
Error Handling
The architecture provides standard exceptions:
InvalidEntityDataException: Entity validation failedEntityDoesNotExistException: Entity not found (404)EntityDoesExistException: Duplicate entity (400)BussinessException: General business rule violation (400)
BaseExceptionFilter.Complete CRUD Example
Here’s how all use cases work together:Next Steps
Creating Controllers
Learn how to expose your use cases through API endpoints
Event Handling
Publish and subscribe to domain events