没有从 'System.Collections.Generic.List<Create.Command>' 到 'MediatR.IRequest<MediatR.Unit>' + .NET Core + CQRS 的隐式引用转换

no implicit reference conversion from 'System.Collections.Generic.List<Create.Command>' to 'MediatR.IRequest<MediatR.Unit>' + .NET Core + CQRS

我在 API 控制器方法中获取一个列表并将其传递给处理程序,如下所示。 我打算做的是遍历列表并将列表中的所有项目保存到数据库中。

public class Create
{
    public class Command : IRequest
    {
        public Guid A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public bool D { get; set; }
    }

    public class Handler : IRequestHandler<List<Command>>
    {
        private readonly DataContext _context;
        private readonly IMapper _mapper;
        public Handler(DataContext context, IMapper mapper)
        {
            _mapper = mapper;
            _context = context;
        }

        public async Task<Unit> Handle(List<Command> request, CancellationToken cancellationToken)
        {
            // loop over the request list and save in the database
        }
    }
}

然而在代码行中'Handler'下有一条红线:public class Handler : IRequestHandler<List<Command>>.

将鼠标悬停在“Handler”上,它显示:

The type 'System.Collections.Generic.List' cannot be used as type parameter 'TRequest' in the generic type or method 'IRequestHandler'. There is no implicit reference conversion from 'System.Collections.Generic.List' to 'MediatR.IRequest'. [Application]csharp(CS0311)

我的 API 控制器方法是:

[HttpPost]
public async Task<ActionResult<Unit>> Create(List<Create.Command> commands) // not like this, it'll be a list
{
     return await Mediator.Send(commands);
}

return await Mediator.Send(commands); 下的红线表示:

Cannot implicitly convert type 'object' to 'Microsoft.AspNetCore.Mvc.ActionResult'. An explicit conversion exists (are you missing a cast?) [API]csharp(CS0266)

如果我在写问题时遗漏了一些信息,请放轻松,我会根据询问不断更新。

所以这就是我最终解决问题的方法:

第一步: 不要在命令 class 中使用道具,而是在相同的 Create.cs class 中使用嵌套的 class,其中命令 class 是:

    public class CreateDto
    {
        public Guid A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public bool D { get; set; }
    }

第 2 步:将是命令 class。命令 class 现在将是:

    public class Command : IRequest
    {
        public List<CreateDto> SomeObjects { get; set; }
    }

第 3 步:处理程序 class 将变为:

public class Handler : IRequestHandler<Command>
    {
        private readonly DataContext _context;
        private readonly IMapper _mapper;
        public Handler(DataContext context, IMapper mapper)
        {
            _mapper = mapper;
            _context = context;
        }

        public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
        {
            foreach (var obj in request.SomeObjectss)
            {
                // logic
            }
            return Unit.Value;
        }
    }

第 4 步:控制器方法将变为:

    [HttpPost]
    public async Task<ActionResult<Unit>> Create(List<CreateDto> createDtos)
    {
        return await Mediator.Send(new Create.Command{SomeObjects = createDtos});
    }