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

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

问题描述

现在我像这样使用 MediatR 通知:

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

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

I would like to change default PublishStrategy to ParallelNoWait = 3,

问题:如何使用来自 样品 ?

Question: How to extend MediatR functionality from nuget with MediatR PublishStrategies from sample ?

我知道我可以下载 MediatR 源代码,从 MediatR.Examples.PublishStrategies 添加代码并构建我自己的库,但也许还有另一种快速"方式?

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?

推荐答案

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

You could extend Publish creating a subclass defining your own DefaultStrategy:

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

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

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.

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

And, of course, register your custom MyPublisher in your DI Container instead of Publisher:

services.AddSingleton<MyPublisher>();

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

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