1

I've noticed that it's been bothering me lately that when I'm advancing into a project, I always end up with a bunch of attributes that decorate the method declarations in the interface but also a different set of decorating the method implementations.

The latest instance is this WCF I'm working with and there, I'm only going to have a single implementing class. Still, some attributes populate the interface, while others do the class. Is that considered bad coding style?

More specifically - can/should all the attributes be moved into the implementing class definition when working with WCF?

4

1 回答 1

2

特定于 WCF:

您不能将所有属性单独移动到实现类,因为WCF Contract它是由接口定义的,并且它是向 WCF 客户端公开的接口,用于发现 WCF 服务和元数据。

此外,Attributes.Net 运行时使用反射使用它们,它们用于发现元数据并将其应用于各种AttributeTargets包括interfaceclass

因此,我认为Attributes在接口和类中都可以使用,并且如果.Net runtime 要求取决于使用它们的目标,则不被视为不好的做法。

于 2013-06-17T12:51:05.893 回答