确定以使用DataEventArgs< TDATA>而不是定制的事件数据类?而不是、事件、数据、LT

2023-09-03 07:51:37 作者:一只小肥羊

使用的是通用的 DataEventArgs< TDATA> 类,而不是声明和使用自定义的EventArgs继承类,在事件的声明,违反了。网络事件模式约定?或者认为不好的做法在某些情况下?

(将命名的命名事件参数的约定是使用事件名称和追加后缀EventArgs的使用 DataEventArgs< TDATA> 省略了事件名称,虽然它显示您发送的数据的类型。)

您也许可以认为,通用DataEventArgs类是一种封闭的扩展,如添加另一个属性,除非你可以修改您使用的TDATA类。

更详细的解释:

在声明一个标准委托的事件,其中包括一些数据据我所知,标准事件模式的约定是使用通用EventHandler委托正是如此声明它:

 公共事件的EventHandler< SomethingHappendEventArgs> SomethingHappend;
 
OPPO上半年电信市场贡献度第一 R11亮相天翼博览会

如特定 SomethingHappendEventArgs 声明沿线的东西

 公共类SomethingHappendEventArgs:EventArgs的
{
    公共SomeDataType值{获得;私定; }
    公共SomethingHappendEventArgs(SomeDataType数据)
    {
        THIS.VALUE =数据;
    }
}
 

在谷歌上搜索周围,我注意到,有几个微软命名空间提供了一个通用的DataEventArgs类(包括Microsoft.P​​ractices.Prism.Events)。不过,我到处找不着时使用自定义的事件数据类,如SomethingHappendEventArgs或反之亦然,与其任何建议或公约的指示。

所以,只要有一个的数据块,我要在事件数据包括,在那里,我应该使用自定义事件数据类,如SomethingHappendEventArgs,而不是宣布事件的任何原因这样吗?

 公共事件的EventHandler< D​​ataEventArgs< SomeDataType>> SomethingHappend;
 

其中,通用 DataEventArgs 可以声明是这样的:

 公共类DataEventArgs< TDATA> :EventArgs的
{
    公共TDATA值{获得;私定; }
    公共DataEventArgs(TDATA值)
    {
        THIS.VALUE =价值;
    }
}
 

解决方案

还有什么理由不使用通用的EventArgs子类的非公共事件。但是,对于那些真正的公共API的一部分事件,事情变得有点棘手,由于潜在的向后兼容性的担忧。对于公开使用的事件,创造一个事件特定的EventArgs的子类,会给你灵活地添加成员,而不影响到API的消费者。

有关的事件是不是公共API的一部分,仍然会有许多工作要做一点潜在的返工,如果EventArgs的子类被改变为一个特定的事件,因为通用的子类不再是一个不错的选择。然而,这通常应该是相当小的,而编译器应该抓住任何问题(显式的或匿名的处理方法是否被使用)。很显然,有一个折中的初始开发工作和潜在的变革努力之间必须作出有 - FWIW,我使用的是通用的EventArgs的内部比赛时,这是一个不错的选择,而且我很少需要经过初步改变一个人发布。

Is using a generic DataEventArgs<TData> class, rather than declaring and using a custom EventArgs inherited class, in an event declaration, a violation of the .Net Event "pattern" convention? Or considered bad practice in some circumstances?

(The naming convention of naming the event args is to use the event name and append the postfix "EventArgs". Using DataEventArgs<TData> omits the event name, although it shows you the type of the data transmitted.)

You could perhaps argue that the generic DataEventArgs class is kind of closed for extensions, such as adding another property, unless you can modify the class that you use for TData.

Longer explanation:

When declaring a standard delegate event that includes some data I understand that the standard event "pattern" convention is to declare it using the generic EventHandler delegate thusly:

public event EventHandler<SomethingHappendEventArgs> SomethingHappend;

Where the specific SomethingHappendEventArgs is declared something along the lines of

public class SomethingHappendEventArgs : EventArgs
{
    public SomeDataType Value { get; private set; }
    public SomethingHappendEventArgs(SomeDataType data)
    {
        this.Value = data;
    }
}

When googling around I noticed that there are several Microsoft namespaces that supplies a generic DataEventArgs class, (including Microsoft.Practices.Prism.Events). However I could nowhere find any recommendation or convention indication on when to use that instead of a customized event data class such as SomethingHappendEventArgs or vice versa.

So, provided that there is one piece of data that I want to include in the event data, are there any reasons that I should use customized event data classes, like SomethingHappendEventArgs , rather than declaring events like this?

public event EventHandler<DataEventArgs<SomeDataType>> SomethingHappend;

where the generic DataEventArgs could be declared something like this:

public class DataEventArgs<TData> : EventArgs
{
    public TData Value { get; private set; }
    public DataEventArgs(TData value)
    {
        this.Value = value;
    }
}

解决方案

There's little reason not to use a generic EventArgs subclass for non-public events. However, for events that are part of a truly public API, things get a bit trickier due to potential backward compatibility concerns. For a publicly consumed event, creating an event-specific EventArgs subclass would give you flexibility to add members without affect the API consumers.

For an event that is not part of a public API, there would still be a bit of potential rework to be done if the EventArgs subclass was changed for a particular event because the generic subclass was no longer a good fit. However, this should usually be fairly minimal, and the compiler should catch any problems (whether explicit or anonymous handler methods are used). Obviously, there's a trade-off to be made there between the initial development effort and the potential change effort -- fwiw, I use a generic EventArgs for internal events where it's a good fit, and I've rarely needed to change one after initial release.