凡订阅内部对象的事件? [英] Where to subscribe to events of the inner object?

查看:95
本文介绍了凡订阅内部对象的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常遇到这样一种情况,我必须决定在哪里订阅内部对象的事件?

I'm often encountering a situation where I must decide where to subscribe to events of the inner object?

例如,我有一个对象模型看起来是这样的:

For example, I have an object model looks like this:

class ClassA
{
    public event EventHandler SomeEvent1;
    public event EventHandler SomeEvent2;
    public event EventHandler SomeEvent3;
    public event EventHandler SomeEvent4;
}

class ClassB
{
    private ClassA a;
    public ClassA A 
    {
        get
        {
            return this.a;
        }
    }

    public ClassB()
    {
        this.a = new ClassA();
        // here subscribe to some events (for example, SomeEvent1 and SomeEvent2)
        // this.a.SomeEvent1 += OnSomeEvent1Raised;
        // this.a.SomeEvent2 += OnSomeEvent2Raised;
    }
}

class ClassC
{
    public ClassB B { get; }
}

class ClassD
{
    public ClassC C { get; }

    public void SomeMethod()
    {
        // Here subscribe to another ones events of object C.B.A. For example:
        C.B.A.SomeEvent3 += OnSomeEvent3Raised;
        C.B.A.SomeEvent4 += OnSomeEvent4Raised;
    }

    private void OnSomeEvent4Raised(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

    private void OnSomeEvent3Raised(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}



我试图创造一些像UML图:

I've tried to create something like a UML diagram:

的我的项目的现有的代码结构具有其中这样的对象模型存在(其具有的地方订阅作为在上面的例子实现的事件 - CBASomeEvent + =)的地方。
我不喜欢它,并希望改变它。
我想从你到这里对这种情况的最佳实践。

Structure of existing code of my project has places where such object model exist (its has a places where subscribing to events implemented as in the example above - C.B.A.SomeEvent+= ). I don't like it and want to change it. I want to here from you the best practices about this situation.

另一种方法是复制CLASSA的所有事件在CLASSB,classC,D类。
,然后更换所有订阅事件为一个地方(我的意思是,在CLASSB我们将订阅/退订ClassA的对象的所有事件。在classC我们将订阅/退订到ClassB的。等...)的对象,在这种情况下的所有事件的所有订阅或取消订阅会在一个地方。希望你明白我的意思在这里。

Alternative approach is to duplicate all events of classA in the classB, classC, classD. And then replace all subscriptions to events to ONE PLACE (I mean that in the classB we will subscribe/unsubscribe to all events of the object of ClassA. In the classC we will subscribe/unsubscribe to all events of the object of classB. And so on...) In this case all subscriptions and unsubscriptions will be in one place. Hope, you understand what I mean here.

再次请靠自己的知识和经验都告诉我们如何解决这种情况。

Again, please rely on your knowledge and experience tell we how to resolve this situation.

更新

你同不同意,订阅或取消订阅事件必须被放置在一个地方?
请,这方面的补充问题太答案。

Do you agree with me that subscriptions and unsubscriptions to events must be placed in ONE PLACE ? Please, answer on this additional question too.

在此先感谢。

推荐答案

您可能会感兴趣在的事件聚合

什么它基本上是由用户去耦出版商 - 这是怎样的一个事件的容器。你可以打通依赖注入(例如MEF)的每个类,你想订阅或发布事件聚合器。

What it basically does is decoupling the publishers from subscribers - it's kind of a event container. You could get the event aggregator through dependency injection (e.g. MEF) for each class you'd like to subscribe or publish from.

我个人使用的方式和喜欢它的最是罗布艾森伯格在实现微卡利事件聚合方式:

The way I personally use and like it the most, is the way Rob Eisenberg implemented the event aggregator in Caliburn Micro:

  • NuGet Gallery
  • Caliburn.Micro Event Aggregator Documentation

在您的情况下,物体A ,B和C将共享一个事件聚合,这只要事件对本次活动汇集出版,所有这些对象识别它意味着相同的实例。 A类,B和C都能够行为不同,造成不同的处理某些事件。

In your case object A, B and C would share the same instance of an event aggregator, which means as soon as events are published on this event aggregator, all these objects recognize it. Class A, B and C are able behave differently, caused by different handling of certain events.

修改

使用事件聚合的是,您订阅聚合器本身带班的的的实例。发布方和订阅类之间的连接发生,通过依靠事件聚合器的同一个实例。在Caliburn.Micro订阅某些事件的情况下,通过实施一个通用的接口发生( IHandle<> )。
例如:如果您想订阅 MyCustomEvent 您实施 IHandle< MyCustomEvent> 接口在类中被认购。
这就要求无效手柄(MyCustomEvent E)从 IHandle< 方法的实现; MyCustomEvent> 接口对于这种类型的事件。这种方法被称为每次一个(新) MyCustomEvent 上的共享事件聚合公布。

The use of an event aggregator is, that you subscribe to the aggregator itself with an instance of a class. The connection between publisher and subscriber class happens through relying to the same instance of the event aggregator. In case of Caliburn.Micro subscription to certain events happens through implementing a generic interface (IHandle<>). For example: if you'd like to subscribe to MyCustomEvent you implement the IHandle<MyCustomEvent> interface in the class to be subscribed. This requires an implementation of the void Handle(MyCustomEvent e) method from the IHandle<MyCustomEvent> interface for this type of event. This method gets called everytime a (new) MyCustomEvent is published on the shared event aggregator.

这篇关于凡订阅内部对象的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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