How to implement a MediatR INotificationHandler interface method - Asp.net Core

0

When installing MediatR 3.0.1 in my project, the INotificationHandler interface looks like this:

#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);
    }
}

In my CustomerCommandHandler class, I have 3 implementations:

 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();
        }

    }

Now, let's go to the problem: If I upgrade to MediatR 4.0.1 (newer), it looks like there was a change in the INotificationHandler interface:

#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);
    }
}

Then, in my CustomerCommandHandler class, it forces me to change the methods to:

 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();           
        }

How do I implement these methods above as what I already had? Example:

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));
            }
        }
    
asked by anonymous 16.01.2018 / 09:18

0 answers