Ao instalar o MediatR 3.0.1 no meu projeto, a interface INotificationHandler fica assim:
#region Assembly MediatR, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null // C:\Users\jalbe\.nuget\packages\mediatr.0.1\lib\netstandard1.1\MediatR.dll #endregion namespace MediatR { public interface INotificationHandler<in TNotification> where TNotification : INotification { void Handle(TNotification notification); } }
Na minha classe CustomerCommandHandler, tenho 3 implementações:
INotificationHandler<RegisterNewCustomerCommand>, INotificationHandler<UpdateCustomerCommand>, INotificationHandler<RemoveCustomerCommand>
public class CustomerCommandHandler : CommandHandler, INotificationHandler<RegisterNewCustomerCommand>, INotificationHandler<UpdateCustomerCommand>, INotificationHandler<RemoveCustomerCommand> { private readonly ICustomerRepository _customerRepository; private readonly IMediatorHandler Bus; public CustomerCommandHandler(ICustomerRepository customerRepository, IUnitOfWork uow, IMediatorHandler bus, INotificationHandler<DomainNotification> notifications) : base(uow, bus, notifications) { _customerRepository = customerRepository; Bus = bus; } public void Handle(RegisterNewCustomerCommand message) { if (!message.IsValid()) { NotifyValidationErrors(message); return; } var customer = new Customer(Guid.NewGuid(), message.Name, message.Email, message.BirthDate); if (_customerRepository.GetByEmail(customer.Email) != null) { Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken.")); return; } _customerRepository.Add(customer); if (Commit()) { Bus.RaiseEvent(new CustomerRegisteredEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate)); } } public void Handle(UpdateCustomerCommand message) { if (!message.IsValid()) { NotifyValidationErrors(message); return; } var customer = new Customer(message.Id, message.Name, message.Email, message.BirthDate); var existingCustomer = _customerRepository.GetByEmail(customer.Email); if (existingCustomer != null && existingCustomer.Id != customer.Id) { if (!existingCustomer.Equals(customer)) { Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken.")); return; } } _customerRepository.Update(customer); if (Commit()) { Bus.RaiseEvent(new CustomerUpdatedEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate)); } } public void Handle(RemoveCustomerCommand message) { if (!message.IsValid()) { NotifyValidationErrors(message); return; } _customerRepository.Remove(message.Id); if (Commit()) { Bus.RaiseEvent(new CustomerRemovedEvent(message.Id)); } } public void Dispose() { _customerRepository.Dispose(); } }
Agora, vamos ao problema: Se eu atualizar para o MediatR 4.0.1 (Mais recente), parece que teve uma mudança na interface INotificationHandler:
#region Assembly MediatR, Version=4.0.1.0, Culture=neutral, PublicKeyToken=null // C:\Users\jalbe\.nuget\packages\mediatr.0.1\lib\netstandard1.1\MediatR.dll #endregion using System.Threading; using System.Threading.Tasks; namespace MediatR { public interface INotificationHandler<in TNotification> where TNotification : INotification { Task Handle(TNotification notification, CancellationToken cancellationToken); } }
Então, na minha classe CustomerCommandHandler, ele me obriga a alterar os métodos para:
public Task Handle(RegisterNewCustomerCommand notification, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task Handle(UpdateCustomerCommand notification, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task Handle(RemoveCustomerCommand notification, CancellationToken cancellationToken) { throw new NotImplementedException(); }
Como que eu faço para implementar nestes métodos citados acima como o que eu já tinha? Exemplo:
public Task Handle(RemoveCustomerCommand notification, CancellationToken cancellationToken) { if (!message.IsValid()) { NotifyValidationErrors(message); return; } _customerRepository.Remove(message.Id); if (Commit()) { Bus.RaiseEvent(new CustomerRemovedEvent(message.Id)); } }