How to add MediatR PublishStrategy to existing project

|▌冷眼眸甩不掉的悲伤 提交于 2020-03-02 20:02:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!