Core Interfaces
IRequestCommand
Base interface for commands that don’t return a value.Core.Application.ComandQueryBus/Commands/IRequestCommand.cs:5
IRequestCommand<TResponse>
Base interface for commands that return a value.Core.Application.ComandQueryBus/Commands/IRequestCommand.cs:9
Type Parameters:
TResponse- The type of value returned by the command (e.g.,string,Guid,int)
IRequestCommandHandler<TRequest, TResponse>
Interface for command handlers that process commands and return results.Core.Application.ComandQueryBus/Commands/IRequestCommandHandler.cs:10
Type Parameters:
TRequest- The command typeTResponse- The response type
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken)- Processes the command
IRequestCommandHandler<TRequest>
Interface for command handlers that don’t return a value.Core.Application.ComandQueryBus/Commands/IRequestCommandHandler.cs:5
Command Examples
CreateDummyEntityCommand
Command to create a new domain entity.Application/UseCases/DummyEntity/Commands/CreateDummyEntity/CreateDummyEntityCommand.cs:13
Returns: The ID of the created entity as a string
UpdateDummyEntityCommand
Command to update an existing entity.Application/UseCases/DummyEntity/Commands/UpdateDummyEntity/UpdateDummyEntityCommand.cs:8
Returns: No value (uses IRequestCommand)
DeleteDummyEntityCommand
Command to delete an entity.Application/UseCases/DummyEntity/Commands/DeleteDummyEntity/DeleteDummyEntityCommand.cs:7
Returns: MediatR.Unit (represents void)
Handler Implementation Patterns
Basic Handler Structure
Command handlers follow a consistent pattern:Application/UseCases/DummyEntity/Commands/CreateDummyEntity/CreateDummyEntityHandler.cs:17
Handler Workflow
- Entity Creation - Instantiate domain entity from command data
- Validation - Validate domain invariants and business rules
- Persistence - Use repository to save changes
- Event Publishing - Publish domain events via
ICommandQueryBus - Error Handling - Wrap exceptions in application-specific types
Update Handler Pattern
Application/UseCases/DummyEntity/Commands/UpdateDummyEntity/UpdateDummyEntityHandler.cs:10
Delete Handler Pattern
Application/UseCases/DummyEntity/Commands/DeleteDummyEntity/DeleteDummyEntityHandler.cs:10
Best Practices
Command Naming
Command Naming
- Use verb-noun pattern:
CreateEntity,UpdateEntity,DeleteEntity - Be specific:
ActivateUservsUpdateUser - Append
Commandsuffix for clarity
Validation Strategy
Validation Strategy
- Use DataAnnotations for basic validation (
[Required], etc.) - Validate domain invariants in the entity itself
- Perform business rule validation in the handler
- Throw specific exceptions for different failure types
Handler Dependencies
Handler Dependencies
- Inject repositories for data access
- Inject
ICommandQueryBusfor publishing domain events - Inject application services for complex business logic
- Use primary constructor syntax (C# 12) for conciseness
Event Publishing
Event Publishing
- Always publish domain events after successful persistence
- Use
entity.To<TEvent>()for entity-to-event mapping - Pass
cancellationTokento support operation cancellation - Publish events before returning from the handler
Related Concepts
- Queries - Read operations in CQRS
- Events - Domain and integration events
- Repositories - Data access interfaces