无法通过将对象传递给 MediatR 查询
Cannot get past passing an object to MediatR query
我正在尝试使用 Mediator(使用 MediatR)查询模式。我需要传入我的用户对象。
这是我的查询:
public class GetAllInvoicesQuery : IRequest<IEnumerable<OrderDto>>
{
public class GetAllInvoicesQueryHandler : IRequestHandler<GetAllInvoicesQuery, IEnumerable<OrderDto>>
{
private readonly IEntityContext _context;
private readonly IMapper _mapper;
public User User { get; set; }
public GetAllInvoicesQueryHandler(IEntityContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<IEnumerable<OrderDto>> Handle(GetAllInvoicesQuery query, CancellationToken cancellationToken)
{
var invoices = await _context.Orders
.Include(d => d.Details)
.Include(r => r.ReservedDocument)
.Include(b => b.Billto)
.Include(s => s.Shipto)
.OrderByDescending(d => d.Docdate)
.Where(x => x.Soptype == OrderType.Invoice && x.Custnmbr == User.Custnmbr)
.ProjectTo<OrderDto>(_mapper.ConfigurationProvider)
.AsNoTracking()
.ToListAsync(cancellationToken: cancellationToken);
if (invoices.Count == 0)
return null;
return invoices.AsReadOnly();
}
}
}
这是我的控制器操作:
[HttpGet("GetAllInvoices")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesDefaultResponseType]
public async Task<IActionResult> GetAllInvoicesAsync()
{
HttpContext.Items.TryGetValue("User", out var user);
if (user is null)
{
_logger.ForContext<UsersController>().Debug("User is null");
return BadRequest(new { message = "Can't find user." });
}
return Ok(await Mediator.Send(new GetAllInvoicesQuery { User = (User)user } ));
^^^^
}
Visual Studio 抱怨我的 User
,它在控制器操作的 return 行中说它“无法解析符号用户”。任何帮助将不胜感激。
您的 User
属性 需要在 GetAllInvoicesQuery
class 上,而不是 GetAllInvoicesQueryHandler
class.
我正在尝试使用 Mediator(使用 MediatR)查询模式。我需要传入我的用户对象。
这是我的查询:
public class GetAllInvoicesQuery : IRequest<IEnumerable<OrderDto>>
{
public class GetAllInvoicesQueryHandler : IRequestHandler<GetAllInvoicesQuery, IEnumerable<OrderDto>>
{
private readonly IEntityContext _context;
private readonly IMapper _mapper;
public User User { get; set; }
public GetAllInvoicesQueryHandler(IEntityContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<IEnumerable<OrderDto>> Handle(GetAllInvoicesQuery query, CancellationToken cancellationToken)
{
var invoices = await _context.Orders
.Include(d => d.Details)
.Include(r => r.ReservedDocument)
.Include(b => b.Billto)
.Include(s => s.Shipto)
.OrderByDescending(d => d.Docdate)
.Where(x => x.Soptype == OrderType.Invoice && x.Custnmbr == User.Custnmbr)
.ProjectTo<OrderDto>(_mapper.ConfigurationProvider)
.AsNoTracking()
.ToListAsync(cancellationToken: cancellationToken);
if (invoices.Count == 0)
return null;
return invoices.AsReadOnly();
}
}
}
这是我的控制器操作:
[HttpGet("GetAllInvoices")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesDefaultResponseType]
public async Task<IActionResult> GetAllInvoicesAsync()
{
HttpContext.Items.TryGetValue("User", out var user);
if (user is null)
{
_logger.ForContext<UsersController>().Debug("User is null");
return BadRequest(new { message = "Can't find user." });
}
return Ok(await Mediator.Send(new GetAllInvoicesQuery { User = (User)user } ));
^^^^
}
Visual Studio 抱怨我的 User
,它在控制器操作的 return 行中说它“无法解析符号用户”。任何帮助将不胜感激。
您的 User
属性 需要在 GetAllInvoicesQuery
class 上,而不是 GetAllInvoicesQueryHandler
class.