7

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
    }
}
hanushi
  • 1,169
  • 2
  • 12
  • 27

3 Answers3

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.

geek
  • 596
  • 8
  • 26
0

The answer to the question is no and don't do that. You can refer to the following links.

In my case, I love DRY principle so I solve duplicate code with extract class from GetAirlineByIdQueryHandler and create GetAirlineByIdHandler Handler or Airline Service to do that.

// Extract class
public record GetAirlineById(int Id, bool LoadOverview = true);

publc interface IGetAirlineByIdHandler {
    public Task<Airline> Handle(GetAirlineById dto, CancellationToken cancellationToken);
}

publc class GetAirlineByIdHandler : IGetAirlineByIdHandler 
{
    // ...
    public async Task<Airline> Handle(GetAirlineById dto, CancellationToken cancellationToken)
    {
        // same logic with GetAirlineByIdQueryHandler 
    }
}

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

public class GetAirlineByIdQueryHandler : IRequestHandler<GetAirlineByIdQuery, Airline>
{
    private readonly IGetAirlineByIdHandler _getAirlineByIdHandler;
    // removed constructor
    
    public async Task<Airline> Handle(GetAirlineByIdQuery request, CancellationToken cancellationToken)
    {
        return await _getAirlineByIdHandler.Handle(request, cancellationToken);
    }
}

public class UpdateAirlineCommanddHandler : IRequestHandler<UpdateAirlineCommand, Airline>
{
    private readonly IGetAirlineByIdHandler _getAirlineByIdHandler;
    // 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
        _getAirlineByIdHandler.Handle(..., cancellationToken);
    }
}
Changemyminds
  • 1,147
  • 9
  • 25
0

In the MediatR pattern it is not recommended to call one handler inside another, MediatR provides alternative solutions, some like:

  • Domain services
  • Events

In my opinion, create a Domain service inside Infrastructure for getting the airline tickets from DB and return the appropriate entity to both of your handlers i.e UpdateAirlineCommanddHandler and GetAirlineByIdQueryHandler. this is the more reasonable and recommended way of handling the duplicate code.

For more detail https://aspnetboilerplate.com/Pages/Documents/Domain-Services

If need information for Events see https://www.rahulpnath.com/blog/avoid-commands-calling-commands/#comment-5444567877

Hamza Iftikhar
  • 584
  • 6
  • 18