它是一种欠佳的C#类来订阅它自己发布的事件? [英] Is it poor form for a C# class to subscribe to its own published events?

查看:67
本文介绍了它是一种欠佳的C#类来订阅它自己发布的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能只是被神经质,但我经常发现自己在这我有类,发布一个事件的情况下,我觉得是很方便的从类本身(例如在构造函数)内订阅此事件,而不是从外部类只预订。



这听起来很合理的给我,但我不能帮助唠叨的感觉,这是一个很差的做法,原因很简单,我M总是面临着这样的问题:为什么不呢?执行你会在其中触发事件的代码事件处理程序提供行动

 公共类按钮
{
公共按钮()
{
this.Click + = someHandler; //不好的做法?
}

公共事件的EventHandler点击;

公共无效HandleInput()
{
如果(someInputCondition)
{
//此处执行必要的行动,而不是
//订阅在构造函数?
this.Click(此,...);
}
}
}



是否有任何缺点订阅以自己的事件?


解决方案

这听起来很合理的给我,但我不能帮唠叨的感觉,这是一个很差的做法,对于我总是面临的问题,原因很简单:为什么不执行你会在其中触发事件的代码事件处理程序提供行动




要回答这个问题,可以考虑部分类的场景。假设你有一个基本类型B.你运行一个自动化工具,通过它扩展到派生类D.你的工具生成一个分部类,使开发人员耗时D可进一步自定义它为自己的目的。

$ B装饰乙
$ b

在这种情况下,似乎完全合理的D的用户创作侧想注册被调用时通过b或D的机器产生的侧声明事件由凸起机器生成的D面。



这是我们发现自己在很多年前VSTO设计时的情景。事实证明,这不是很难做到这一点在C#中,但它是相当棘手得到这一切在VB中工作。我相信VB做出了一些调整它们的事件订阅模式来简化这一过程。



这是说:如果你能避免这种情况,我会的。如果你只是做了内部认购,似乎像一个坏的代码味道的事件。在C#3助阵分部方法大大这里,因为他们可以很容易和低成本的机器生成方调用用户生成的侧小的通知功能,而不必去发布事件的麻烦。


I'm probably just being neurotic, but I regularly find myself in situations in which I have class that publishes an event, and I find it convenient to subscribe to this event from within the class itself (e.g. in the constructor), rather than only subscribing from external classes.

This sounds reasonable to me, but I can't help the nagging feeling that it's a poor practice, for the simple reason that I'm always faced with the question: "Why not perform the actions that you'd provide in the event handler in the code which fires the event?"

public class Button
{
   public Button()
   {
      this.Click += someHandler; // bad practice?
   }

   public event EventHandler Click;

   public void HandleInput()
   {
      if (someInputCondition)
      {
         // Perform necessary actions here rather than 
         // subscribing in the constructor?
         this.Click(this, ...);
      }
   }
}

Are there any drawbacks to subscribing to your own events?

解决方案

This sounds reasonable to me, but I can't help the nagging feeling that it's a poor practice, for the simple reason that I'm always faced with the question: "Why not perform the actions that you'd provide in the event handler in the code which fires the event?"

To answer that question, consider partial class scenarios. Suppose you have a base type B. You run an automated tool that decorates B by extending it to derived class D. Your tool generates a partial class so that developers consuming D can further customize it for their own purposes.

In that case, it seems perfectly reasonable that the user-authored side of D would want to sign up to be called when events declared by B or the machine-generated side of D are raised by the machine-generated side of D.

That was the scenario we found ourselves in when designing VSTO many years ago. As it turns out, it was not difficult to do this in C# but it was quite tricky to get it all working in VB. I believe VB has made some tweaks to their event subscription model to make this easier.

That said: if you can avoid this, I would. If you're just making an event for internal subscription that seems like a bad code smell. Partial methods in C# 3 help out greatly here, since they make it easy and low-cost for the machine-generated side to call little notification functions in the user-generated side, without having to go to the trouble of publishing an event.

这篇关于它是一种欠佳的C#类来订阅它自己发布的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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