Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
Can I call QueryHandler/CommandHandler in another CommandHandler in CQRS

I am working an ASP.NET Core 6.0 Web API project. I use the CQRS design pattern.

I want to update an airlines table (code-first EF Core). So first I need to find the ID of airline.

I have GetAirlineByIdQueryHandler.cs

public record GetAirlineByIdQuery(int Id, bool LoadOverview = true) : IRequest<Airline>;

public class GetAirlineByIdQueryHandler : IRequestHandler<GetAirlineByIdQuery, Airline>
{
    public async Task<Airline> Handle(GetAirlineByIdQuery request, CancellationToken cancellationToken)
    {
        var query = _techneDbContext.Airline
                .Include(d => d.X)
                .Include(d => d.Y)
                .Include(d => d.Z)
               
                .AsQueryable();

        if (request.LoadOverview)
        {
            query = query.Include(d => d.Overview);
        }

      var airline = await query.FirstOrDefaultAsync(d => d.Id == request.Id);


        if (airline == null)
        {
            throw new NotFoundException(nameof(Airline), request.Id);
        }

         return airline;
    }
}

UpdateAirlineCommand.cs

public class UpdateAirlineCommand : AirlineUpdateDto, IRequest<Airline>
{
    public int Id { get; set; }
}

public class UpdateAirlineCommanddHandler : IRequestHandler<UpdateAirlineCommand, Airline>
{
    // removed constructor

    public async Task<Airline> Handle(UpdateAirlineCommand request, CancellationToken cancellationToken)
    {
        // To update I have find the the id is there or not?
        // Can I call GetAirlineByIdQueryHandler here or do I need to copy the query and paste it here
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Generally Command and Queries operates on separate models. This gives you optimized data schemas for reads and writes. In your examples query returns the same model that will by updated by the command. Another drawback of using query inside command is making them tighly coupled. One of CQRS huge adventage is keeping read and write side separetly. In your example any change in GetAirlineByIdQueryHandler can affect UpdateAirlineCommanddHandler.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda