The previous section discussed the IRequest operation of the MediatR Package.
The same as the INotification of the MediatR Package implemented in .Net Core, the Package will be registered first:
The registration process of “Service.AddMediateR” was also introduced in the first half of the previous section, so I won’t repeat it here. Then observe the operation of Notification.
[INotification]
Suppose we execute the following code as an example:
await _mediator.Publish(new MessageNotice(customer), new System.Threading.CancellationToken());
And the class inherits the interface INotification:
public class MessageNotice : INotification
{
public DateTime SubmittedAt { get; set; }
public string FullName { get; set; }
public string EmailAddress { get; set; }
public string Message { get; set; }
public MessageNotice(Customer Customer)
{
FullName = Customer.FirstName + " " + Customer.LastName;
SubmittedAt = DateTime.Now;
EmailAddress = Customer.FirstName + "_" + Customer.LastName + "@mail.com";
Message = " Happy Holiday! ";
}
public MessageNotice()
{
}
}
Executed Handler class:
public class MailNoticeHandler : INotificationHandler<MessageNotice>
{
public Task Handle(MessageNotice notification, CancellationToken cancellationToken)
{
MailCollection.Instance[MailCollection.Instance.Count] = notification;
return Task.CompletedTask;
}
}
public class QueueNoticeHandler : INotificationHandler<MessageNotice>
{
public Task Handle(MessageNotice notification, CancellationToken cancellationToken)
{
QueueCollection.Instance[QueueCollection.Instance.Count] = notification;
return Task.CompletedTask;
}
}
The operation process is as follows:
Its MailCollection and QueueCollection simplify the storage space for its own design, which can be replaced by database operations related to Mail information and Queue information in the future.
Through the DI mechanism of .net Core, the Mediator and its field ServiceFactory can be obtained from the controller constructor.
Later, some processing will be used to deal with the corresponding entities of Type and Instance.
Then there are several steps in the figure:
First, directly add an instance of NotificationHandlerWrapperImpl , the example TNotification here is INotificationHandler.
Second, delegate ServiceFactory to generate instances via registered services via IServiceProvider.GetService in the Dot Net Core framework.
Then pass ServiceFactory and Mediator.PublishCore function to NotificationHandlerWrapperImpl.Handle.
Third, implement the entity from the registered service INotificationHandler using ServiceFactory, which is also a class created by the developer.
The above is the description of the operation process of INotification.