我正在尝试实现Mediatr
和CQRS
模式。我得到它的工作为Create
命令和Get
查询。但是当我试图运行带有参数的Delete
命令时,我得到了这个错误
Int32没有实现IRequest (Parameter 'request')
这是我的代码。
public class DeleteMarketingEventCommand : IRequest<int>
{
public int Id{ get; }
public DeleteMarketingEventCommand(int id)
{
Id = id;
}
}
public class DeleteMarketingEventHandler : IRequestHandler<DeleteMarketingEventCommand, int>
{
private readonly IMarketingEventService _marketingEventService;
private readonly IMapper _mapper;
public DeleteMarketingEventHandler(IMapper mapper, IMarketingEventService marketingEventService)
{
_mapper = mapper;
_marketingEventService = marketingEventService;
}
public async Task<int> Handle(DeleteMarketingEventCommand request, CancellationToken cancellationToken)
{
var result = await _marketingEventService.DeleteMarketingEvent(request.Id);
return result;
}
}
[HttpDelete]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult> DeleteMarketingEventById(int id)
{
try
{
var result = _mediator.Send(id);
return Ok(result);
}
catch (Exception ex)
{
return Problem(ex.Message);
}
}
您需要传递DeleteMarketingEventCommand:
public async Task<ActionResult> DeleteMarketingEventById(int id)
{
try
{
var result = _mediator.Send(new DeleteMarketingEventCommand(id));
return Ok(result);
}
catch (Exception ex)
{
return Problem(ex.Message);
}
}
或
public async Task<ActionResult> DeleteMarketingEventById(DeleteMarketingEventCommand command)
{
try
{
var result = _mediator.Send(command);
return Ok(result);
}
catch (Exception ex)
{
return Problem(ex.Message);
}
}