我的班级应该参加自己的公开活动吗? [英] Should my class subscribe to its own public events?

查看:82
本文介绍了我的班级应该参加自己的公开活动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#3.0.遵循标准的事件模式,我有:

I am using C# 3.0. Following the standard event pattern I have:

    public event EventHandler<EventArgs> SomeEventHappens;

    protected virtual void OnSomeEventHappens(EventArgs e)
    {
        if (SomeEventHappens != null)
        {
            SomeEventHappens(this, e);
        }
    }

    private object _someProperty;

    public object SomeProperty
    {
        get
        {
            return _someProperty;
        }
        private set
        {
            if (_someProperty == value)
            {
                return;
            }
            OnSomeEventHappens(EventArgs.Empty);
            _someProperty = value;
        }
    }

在同一个班级内SomeProperty更改时,我想采取一些措施.我的看法有3种选择:

Within my same class I would like to take some actions when SomeProperty changes. The way I see it I have 3 alternatives:

1)在我的SomeProperty设置器中进行操作.这样做使我误入歧途,因为我尝试遵循所有事物的哲学,那就是一件事做得好.将东西塞进二传手似乎与此相反,或者至少有这样的倾向.

1) Do stuff within my SomeProperty setter. Something rubs me the wrong way about doing this since I try to subscribe to the philosophy of everything should do one thing and do it well. Cramming stuff into a setter seems to go against that, or at least has the propensity to.

2)在OnSomeEventHappens中做事.再次,似乎有点反对将其保持在简单的片断中.另外,如果重写此方法,则如果实现者不调用基本方法,则可能会失去功能.

2) Do stuff in OnSomeEventHappens. Again, seems to go a little against keeping this in simple pieces. Also, if this method gets overridden, could potentially lose functionality if the implementer does not call the base method.

3)让班级订阅SomeEventHappens.对我而言,就封装而言,这似乎是正确的选择,而且看起来很干净.同样,如果OnSomeEventHappens被覆盖,可能会产生影响.

3) Have the class subscribe to SomeEventHappens. To me this seems to be the proper choice as far as encapsulation is concerned, and seems pretty clean. Again, possible repercussions if OnSomeEventHappens is overridden.

也许还有一些更优雅的东西?我无法在选项2和3之间做出选择,而且我对最佳实践是很好奇的.毕竟,最安全的地方可能是在属性设置器中.

Maybe there is something more elegant? I cannot decide between option 2 and 3, and I am curious as to what the Best Practice is. Maybe the safest place is in the property setter after all.

有想法吗?

更新: 感谢您在下面的出色评论和回答.我知道让一个类订阅自己的事件是好的",尽管在我看来,由于开销我倾向于不这样做.我已经考虑了潜在的虚拟方法重写器的行为以及我到底想发生什么.

Update: Thanks for the great comments and answers below. I have learned that it is "OK" to have a class subscribe to its own events, although in my case I am leaning to not do due to overhead. I have put thought into the behavior of potential overriders of my virtual methods and what exactly I want to happen.

在我的实际情况下,我真的不希望在不设置属性的情况下引发事件.正如下面的答案指导了我的思考过程一样,我认为我可以选择第一种方法,因为这种方法的开销较低,继承者进行不当行为的风险有所降低,这对我来说通常更有意义.再次感谢!

In my real-world case, I do not really want the events to be raised without the property being set. As the answers below have guided my thought process, I think I may go with option 1, due to the lower overhead, the reduced risk of improper behavior from inheritors, and it just generally makes better sense to me. Thanks again!

推荐答案

如果您从某个公共位置(属性过程或其他函数)调用SomeEventHappens和OnSomeEventHappens,则不必担心重写器会忽略提高事件.我宁愿重写一个函数而不是处理事件,因为这样可以减少开销.

If you call SomeEventHappens and OnSomeEventHappens from some common location (the property procedure or another function), then you don't have to worry about overriders neglecting to raise the event. I would prefer to override a function rather than handle events because there's less overhead.

这篇关于我的班级应该参加自己的公开活动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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