2017-04-05 102 views
0

我參加了Jimmy的MediatR DI 1.0.1課程。我試圖將基於該課程示例的項目升級到2.0.0,但我找不到任何升級步驟。MediatR依賴注入的升級步驟

下面是該課程的一個具體示例。我已經將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); 
     } 
    } 
} 

回答

1

我發現吉米用MediatR 2.0.0重寫了Contoso,給了我答案。這裏是我必須做的變化從1.0.1到2.0.0:

更新控制器操作:
從:await _mediator.SendAsync(query);
收件人:await _mediator.Send(query);

更新AsyncRequestHandler:
來源:公共類EmployeeEditHandler:AsyncRequestHandler
爲:公共類EmployeeEditHandler:IAsyncRequestHandler

更新HandleCore:
來源:保護覆蓋異步任務HandleCore(EmployeeEditModel消息)
:公開async Task Handle(EmployeeEditModel message)

更新IAsyncRequest:
From:pub lic類EmployeeEditQuery:IAsyncRequest
致:public class EmployeeEditQuery:IRequest