An example of using CQRS pattern together with minimal api from .NET 7.
CQRS means Command and Query Responsibility Segregation and it sounds more complex than it actually is. The main outcome of using CQRS is cleaner and simpler code that is structured better and is easy to scale.
I tried to make this example as simple as possible to show the core of CQRS advantages, but I also included here some other most common use cases, which is for example validation or exception handling.
Here is the result:
Command declaration:
public record AddProduct(string Name) : ICommand<string>
{
public class Validator : RequestValidator<AddProduct>
{
public Validator() => RuleFor(c => c.Name).MaximumLength(255).NotEmpty();
}
}
Command's handler:
public class AddProductHandler : ICommandHandler<AddProduct>
{
public async Task<Result<string>> Handle(AddProduct command, CancellationToken ct)
{
var product = Product.Create(
name: command.Name
);
// ...
return Result.Ok(product.Id);
}
}
Mapping command to the API's endpoint:
endpoints.MapGroup("products")
.MapCommand<AddProduct>();
Which results in a new route:
POST /products/addProduct
It's a live project which I will update with the most common use cases.