如何将MediatR PublishStrategy添加到现有项目 [英] How to add MediatR PublishStrategy to existing project
问题描述
现在,我使用这样的MediatR通知:
private readonly IMediator _requestsRouter; // from constructor injection
OrderCreatedEvent orderCreatedEvent = new OrderCreatedEvent(x,y,z);
await _requestRouter.Publish(orderCreatedEvent);
我想将默认 PublishStrategy 更改为ParallelNoWait = 3,
>
问题:如何从样本?
我了解我可以下载MediatR源代码,可以从MediatR.Examples.PublishStrategies中添加代码并构建自己的库,但是也许还有另一种快速"方法吗?
您可以扩展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屋!