【发布时间】:2022-01-29 23:52:01
【问题描述】:
我正在开发一个 ASP.NET Core 6.0 Web API 项目。我使用 CQRS 设计模式。
我想更新airlines 表(代码优先的 EF Core)。所以首先我需要找到航空公司的 ID。
我有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
}
}
【问题讨论】:
标签: c# asp.net-core asp.net-core-webapi cqrs