问题
Now I use MediatR notifications like this:
private readonly IMediator _requestsRouter; // from constructor injection
OrderCreatedEvent orderCreatedEvent = new OrderCreatedEvent(x,y,z);
await _requestRouter.Publish(orderCreatedEvent);
I would like to change default PublishStrategy to ParallelNoWait = 3,
Question: How to extend MediatR functionality from nuget with MediatR PublishStrategies from sample ?
I understand that I can download MediatR source code, add to it code from MediatR.Examples.PublishStrategies and build my own library but maybe there is another "fast" way?
回答1:
You could extend Publish
creating a subclass defining your own DefaultStrategy
:
public class MyPublisher : Publisher
{
public MyPublisher(ServiceFactory serviceFactory) : base(serviceFactory)
{
DefaultStrategy = PublishStrategy.ParallelNoWait;
}
}
So you keep the source code intact from Mediatr by calling the base constructor and overriding the value with which the DefaultStrategy
member was initialized.
And, of course, register your custom MyPublisher
in your DI Container instead of Publisher
:
services.AddSingleton<MyPublisher>();
来源:https://stackoverflow.com/questions/59320296/how-to-add-mediatr-publishstrategy-to-existing-project