MediatR Dependency Injection 的升级步骤是什么
What are the upgrade steps for MediatR Dependency Injection
我参加了使用 MediatR DI 1.0.1 的 Jimmy 的课程。我正在尝试将基于该课程示例的项目升级到 2.0.0,但找不到任何升级步骤。
这是课程中的一个具体示例。我已将 IAsyncRequest 更改为 IRequest,这似乎是正确的。剩余的错误是:
public class EmployeeEditHandler : AsyncRequestHandler<EmployeeEditModel>
和
protected override async Task HandleCore(EmployeeEditModel message)
AsyncRequestHandler应该改成什么? HandleCore 是否需要更改?
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);
}
}
}
我发现 Jimmy 使用 MediatR 2.0.0 重写了 Contoso,这给了我答案。以下是我从 1.0.1 到 2.0.0 所做的更改:
更新控制器操作:
来自:await _mediator.SendAsync(query);
至:等待_mediator.Send(查询);
更新 AsyncRequestHandler:
来自:public class EmployeeEditHandler : AsyncRequestHandler
收件人:public class EmployeeEditHandler : IAsyncRequestHandler
更新 HandleCore:
来自:protected override async Task HandleCore(EmployeeEditModel message)
至:public 异步任务句柄(EmployeeEditModel 消息)
更新 IAsyncRequest:
来自:public class EmployeeEditQuery:IAsyncRequest
收件人:public class EmployeeEditQuery : IRequest
我参加了使用 MediatR DI 1.0.1 的 Jimmy 的课程。我正在尝试将基于该课程示例的项目升级到 2.0.0,但找不到任何升级步骤。
这是课程中的一个具体示例。我已将 IAsyncRequest 更改为 IRequest,这似乎是正确的。剩余的错误是:
public class EmployeeEditHandler : AsyncRequestHandler<EmployeeEditModel>
和
protected override async Task HandleCore(EmployeeEditModel message)
AsyncRequestHandler应该改成什么? HandleCore 是否需要更改?
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);
}
}
}
我发现 Jimmy 使用 MediatR 2.0.0 重写了 Contoso,这给了我答案。以下是我从 1.0.1 到 2.0.0 所做的更改:
更新控制器操作:
来自:await _mediator.SendAsync(query);
至:等待_mediator.Send(查询);
更新 AsyncRequestHandler:
来自:public class EmployeeEditHandler : AsyncRequestHandler
收件人:public class EmployeeEditHandler : IAsyncRequestHandler
更新 HandleCore:
来自:protected override async Task HandleCore(EmployeeEditModel message)
至:public 异步任务句柄(EmployeeEditModel 消息)
更新 IAsyncRequest:
来自:public class EmployeeEditQuery:IAsyncRequest
收件人:public class EmployeeEditQuery : IRequest