如何将MediatR PublishStrategy添加到现有项目 [英] How to add MediatR PublishStrategy to existing project

查看:125
本文介绍了如何将MediatR PublishStrategy添加到现有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我使用这样的MediatR通知:

private readonly IMediator _requestsRouter; // from constructor injection 

 OrderCreatedEvent orderCreatedEvent = new OrderCreatedEvent(x,y,z);
 await _requestRouter.Publish(orderCreatedEvent); 

我想将默认 PublishStrategy 更改为ParallelNoWait = 3,

问题:如何从

您可以扩展Publish来创建定义您自己的DefaultStrategy的子类:

public class MyPublisher : Publisher
{
    public MyPublisher(ServiceFactory serviceFactory) : base(serviceFactory)
    {
        DefaultStrategy = PublishStrategy.ParallelNoWait;
    }
}

因此,您可以通过调用基本构造函数并覆盖初始化DefaultStrategy成员的值来保持Mediatr的源代码完整.

并且,当然,在您的DI容器中注册您的自定义MyPublisher而不是Publisher:

services.AddSingleton<MyPublisher>();

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?

解决方案

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

这篇关于如何将MediatR PublishStrategy添加到现有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆