如果使用DI + MEF,在容器中的类之间传递事件的好方法是什么?

Con*_*ngo 5 .net c# events mef reactive-programming

我有一个包含数百个类的MEF容器.在不同类之间传递消息的好方法是什么?

我更喜欢适用于任何依赖注入(DI)容器的解决方案,包括Unity,Castle Windsor等.

Con*_*ngo 3

注意:这是一个“分享您的知识,问答式”的条目。

事件发布者简介

该事件发布者允许 MEF 容器中的任何类向 MEF 容器中的任何其他类发送消息。

该代码已经经过多年的实际验证,并且已被证明在使用 WPF / MVVM 时特别有用。

它是一对多订阅,因此一旦消息发出,任何正在观察该自定义类型消息的侦听器都会收到该消息。

此示例适用于 MEF,但它也适用于任何其他依赖注入 (DI) 容器,例如 Unity、Castle Windsor 等。如果转换EventPublisher为单例,则可以将其与普通 C# 一起使用(即不使用 DI 容器) )。如果您想让我发布代码,请告诉我。

这段代码并不是什么新鲜事:开源社区中有数百种其他事件发布者的实现,例如 MVVM Light。但是,此示例使用的代码量非常少,因此可以通过在调试器中单步执行来了解其幕后工作原理。

C# 用法

将样板代码添加到您的项目中(见下文)。

创建您的自定义事件类型。这可以是一个类、一个结构,甚至是一个枚举,例如:

public enum NavigationType
{
    Unknown = 0,
    MyOption1,
    MyOption2
}
Run Code Online (Sandbox Code Playgroud)

...然后,我可以将其导入eventPublisher到任何类中,如下所示:

[ImportingConstructor]
public BrokerOrderSearchResultViewModel(
    IEventPublisher<NavigationType> eventPublisher,
    )
{
    _eventPublisher = eventPublisher;
    ...
Run Code Online (Sandbox Code Playgroud)

...在构造函数中,我可以订阅类型的事件NavigationType

_eventPublisher.GetEvent<NavigationType>().Subscribe(o => 
{
    Console.Write(o);
});
Run Code Online (Sandbox Code Playgroud)

...以及其他任何地方,我可以推送事件,这些事件将在订阅中接收:

_eventPublisher.Publish(NavigationType.MyOption1);
Run Code Online (Sandbox Code Playgroud)

C# 样板代码

将反应式扩展 (RX) NuGet 包添加到您的项目中。

创建这个接口:

public interface IEventPublisher
{
    IObservable<TEvent> GetEvent<TEvent>();
    void Publish<TEvent>(TEvent sampleEvent);
}

public interface IEventPublisher<in T>
{
    IObservable<TEvent> GetEvent<TEvent>() where TEvent : T;
    void Publish<TEvent>(TEvent sampleEvent) where TEvent : T;
}
Run Code Online (Sandbox Code Playgroud)

...通过这个实现:

// NOTE: This class must be a singleton (there should only ever
// be one copy; this happens automatically in any dependency injection
// container). This class is the central dictionary that routes events
// of any incoming type, to all listeners for that same type.
[Export(typeof (IEventPublisher))]
public class EventPublisher : IEventPublisher
{
    private readonly ConcurrentDictionary<Type, object> _subjects;


    public EventPublisher()
    {
        _subjects = new ConcurrentDictionary<Type, object>();
    }

    public IObservable<TEvent> GetEvent<TEvent>()
    {
        return (ISubject<TEvent>)_subjects.GetOrAdd(typeof(TEvent), t => new Subject<TEvent>());
    }

    public void Publish<TEvent>(TEvent sampleEvent)
    {
        object subject;
        if (_subjects.TryGetValue(typeof (TEvent), out subject))
        {
            ((ISubject<TEvent>)subject).OnNext(sampleEvent);
        }
        // Could add a lock here to make it thread safe, but in practice,
        // the Dependency Injection container sets everything up once on
        // startup and it doesn't change from that point on, so it just
        // works.
    }
}

// NOTE: There can be many copies of this class, one for
// each type of message. This happens automatically in any
// dependency injection container because its a <T> class. 
[Export(typeof (IEventPublisher<>))]
public class EventPublisher<T> : IEventPublisher<T>
{
    private readonly IEventPublisher _eventPublisher;

    [ImportingConstructor]
    public EventPublisher(IEventPublisher eventPublisher)
    {
        _eventPublisher = eventPublisher;
    }

    public IObservable<TEvent> GetEvent<TEvent>() where TEvent : T
    {
        return _eventPublisher.GetEvent<TEvent>();
    }

    public void Publish<TEvent>(TEvent sampleEvent) where TEvent : T
    {
        _eventPublisher.Publish(sampleEvent);
    }
}
Run Code Online (Sandbox Code Playgroud)

讨论

此代码显示了将事件从任何类发送到任何其他类是多么简单。

如图所示,您需要创建一个新的自定义类型才能发送消息。类型可以是枚举、结构或类。如果类型是类或结构,则它可以包含任意数量的属性。如果使用特定的自定义类型发送消息,则侦听该类型消息的所有订阅者都将收到该消息。您可以创建许多自定义类型,每种类型对应您需要通信的每种类型的事件。

在幕后,所有代码所做的就是保存自定义类型的字典。发送时,它会在字典中查找适当的订阅者,然后使用反应性扩展 (RX) 发送消息。所有收听该类型的订阅者都将收到该消息。

有时,如果到处都有太多事件,则很难看出哪些类正在与哪些其他类进行通信。在这种情况下,很简单:您可以使用“在文件中查找”来查找包含字符串 的所有类IEventPublisher<NavigationType>,最终列出正在发送或侦听自定义类型事件的所有类NavigationType

请注意:此代码不是灵丹妙药。过度依赖事件是一种不好的代码味道,因为类层次结构应该以这样的方式组成,即类不应该依赖于它们的父级。有关更多信息,请研究 SOLID 原则,特别是 LSP。然而,有时事件的使用是不可避免的,因为我们别无选择,只能跨越类层次结构。

未来的增强功能

目前,此事件发布器未实现 IDisposable。它应该。