1

I took Jimmy's course which used MediatR DI 1.0.1. I'm trying to upgrade a project based on that course's examples to 2.0.0 but I can't find any upgrade steps.

Here's a specific example from the course. I've changed IAsyncRequest to IRequest and that appears to be correct. The errors remaining are for:

public class EmployeeEditHandler : AsyncRequestHandler<EmployeeEditModel> 

and

protected override async Task HandleCore(EmployeeEditModel message)  

What should AsyncRequestHandler be changed to? does HandleCore need to be changed?

namespace EmployeeDirectory.ViewModels
{
    using MediatR;
    //Other using statements removed for this example

    public class EmployeeEditQuery : IAsyncRequest<EmployeeEditModel>
    {
        public Guid Id { get; set; }
    }

    public class EmployeeEditModel : IAsyncRequest
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }

    public class EmployeeEditQueryHandler 
        : IAsyncRequestHandler<EmployeeEditQuery, EmployeeEditModel>
    {
        private readonly DirectoryContext _dbContext;

        public EmployeeEditQueryHandler(DirectoryContext dbContext)
        {
            _dbContext = dbContext;
        }
        public async Task<EmployeeEditModel> Handle(EmployeeEditQuery message)
        {
            var model = await _dbContext
                .Employees
                .Where(e => e.Id == message.Id)
                .ProjectTo<EmployeeEditModel>()
                .SingleOrDefaultAsync();

            return model;
        }
    }

    public class EmployeeEditHandler : AsyncRequestHandler<EmployeeEditModel>
    {
        private readonly DirectoryContext _dbContext;

        public EmployeeEditHandler(DirectoryContext dbContext)
        {
            _dbContext = dbContext;
        }

        protected override async Task HandleCore(EmployeeEditModel message)
        {
            var employee = await _dbContext.Employees
                .SingleOrDefaultAsync(e => e.Id == message.Id);

            Mapper.Map(message, employee);
        }
    }
}
4

1 回答 1

2

我发现 Jimmy 使用 MediatR 2.0.0 重写了 Contoso,这给了我答案。以下是我从 1.0.1 到 2.0.0 必须做出的更改:

更新控制器动作:
来自:await _mediator.SendAsync(query);
收件人:等待 _mediator.Send(query);

更新 AsyncRequestHandler:
从:公共类 EmployeeEditHandler:AsyncRequestHandler
到:公共类 EmployeeEditHandler:IAsyncRequestHandler

更新 HandleCore:
从:受保护的覆盖异步任务 HandleCore(EmployeeEditModel 消息)
到:公共异步任务句柄(EmployeeEditModel 消息)

更新 IAsyncRequest:
从:公共类 EmployeeEditQuery:IAsyncRequest
到:公共类 EmployeeEditQuery:IRequest

于 2017-04-05T16:22:28.730 回答