Queries - Read Operations
What is it about?
Queries are CQRS read operations that allow the application to optimize and scale its read operations independently, often through dedicated read models that are designed and optimized for specific query requirements. It creates a query handler that contains logic for the operation.
Examples
Creating a Query
frontier dotnet add query [name-of-query]
This command generates two files located ProjectName/src/ProjectNameApi.Application/NameOfQuery/Queries/NameOfQuery/
. The files care to modified for user's purpose.
NameOfQueryQuery.cs
using Mediator;
// using ProjectName.Application.Features.NameOfQuery.Interfaces;
namespace ProjectName.Application.Features.NameOfQuery.Queries.NameOfQuery
{
public record NameOfQueryQuery : IRequest<string?>>
{
}
public class NameOfQueryQueryHandler : IRequestHandler<NameOfQueryQuery, string?>>
{
// private readonly IAuthenticationService _authenticationService;
public NameOfQueryQueryHandler(/*IAuthenticationService authenticationService*/)
{
// _authenticationService = authenticationService;
}
public async ValueTask<string?>> Handle(NameOfQueryQuery request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}
NameOfQueryQueryValidator.cs
using FluentValidation;
namespace ProjectName.Application.Features.NameOfQuery.Queries.NameOfQuery
{
public class NameOfQueryQueryValidator : AbstractValidator<NameOfQueryQuery>
{
public NameOfQueryQueryValidator()
{
RuleFor(v => v.Property1).Cascade(CascadeMode.Stop)
.NotEmpty().WithMessage("Property1 field is required.");
}
}
}
Table of Contents