CQRS Pattern
Command Query Responsibility Segregation implementation.
Commands
Commands modify state and return Result.
Interfaces
| Interface | Description |
|---|---|
| ICommand<TResponse> | Command with response |
| ICommand | Command returning Result |
| ICommandHandler<TCommand, TResponse> | Handler interface |
| ICommandHandler<TCommand> | Handler for Result commands |
Usage
csharp
public record CreateUserCommand(string Email, string Name) : ICommand<Guid>;public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand, Guid> { public async Task<Result<Guid>> Handle(CreateUserCommand request, CancellationToken ct) { var user = new User(request.Email, request.Name); await _repo.AddAsync(user, ct); return Result<Guid>.Success(user.Id); } }
Queries
Queries read state without side effects.
Interfaces
| Interface | Description |
|---|---|
| IQuery<TResponse> | Query with response |
| IQueryHandler<TQuery, TResponse> | Handler interface |
Usage
csharp
public record GetUserByIdQuery(Guid Id) : IQuery<UserDto?>;public class GetUserByIdQueryHandler : IQueryHandler<GetUserByIdQuery, UserDto?> { public async Task<UserDto?> Handle(GetUserByIdQuery request, CancellationToken ct) { return await _repo.GetByIdAsync(request.Id, ct); } }