除了大而混乱的属性之外,还有其他选择吗?
我经常发现属性可能太大。 有时感觉属性比代码占据了更多的屏幕。 它可能会导致很难识别方法名称。
此外,它们不可重复使用,因此您最终可能会多次重复您的值。
为了解决这个问题,我考虑创建自己的属性类,它继承自所需的 属性,只需将所有属性设置为我需要的默认值。
然而,在大多数情况下,属性是被密封的,这阻止了我的计划。
除了大属性之外还有其他选择吗?
作为我正在谈论的内容的随机示例:
[SoapDocumentMethod(
"http://services.acme.co.uk/account/Web/GetCustomerDetails/GetCustomerDetails",
RequestNamespace = "http://services.acme.co.uk/account/Web",
ResponseNamespace = "http://services.acme.co.uk/account/Web",
Use = SoapBindingUse.Literal,
ParameterStyle = SoapParameterStyle.Wrapped)]
public Response GetCustomerDetails(Request request)
{
//...
}
I often find that attributes can be too large.
Sometimes it feels like the attributes take up more of the screen than the code.
It can make it hard to spot the method names.
Also, they are not reusable, so you can end up repeating your values a lot.
To counter this I considered creating my own attribute class, which inherits from the required
attribute, and just sets all the properties to the defaults I need.
However, in most cases attributes are sealed, putting a stop to my schemes.
Is there any alternative to large attributes?
As a random example of what I'm talking about:
[SoapDocumentMethod(
"http://services.acme.co.uk/account/Web/GetCustomerDetails/GetCustomerDetails",
RequestNamespace = "http://services.acme.co.uk/account/Web",
ResponseNamespace = "http://services.acme.co.uk/account/Web",
Use = SoapBindingUse.Literal,
ParameterStyle = SoapParameterStyle.Wrapped)]
public Response GetCustomerDetails(Request request)
{
//...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然它不能解决您的所有问题,但您应该对重复值(尤其是字符串)使用常量。
While it doesn't solve all your problems, you should be using constants for your repeated values, especially strings.
从我的角度来看,有很多选择。
其中之一是代码生成。您可以使用 T4 引擎来读取某些配置文件并将某些属性应用于任意成员。
了解有关 T4 的更多信息 http://msdn.microsoft .com/en-us/library/ff697195.aspx
有时,某些类是某些层次结构的一部分,您可以在抽象或虚拟成员中使用某些属性,因此重写这些属性的派生类不需要该属性,因为它是已在基本成员中声明。
关于可读性,您可以使用区域来隐藏成员的属性。
无论如何,我会建议您使用代码生成方法,因为它是最干净、最简单的解决方案。是的,这不是一个替代方案,因为您将拥有确切的代码,但您将避免手动完成。
最后,大多数 .NET API 和第三方 API 允许您使用属性或某些对象模型来配置事物,因此,也许,当您发现代码充满属性时,可以通过创建您的自己的配置模式并使用库的对象模型来配置您的环境。
编辑
我想补充一点,如果您喜欢代码生成方法,您可以使用自定义属性,当在某些文件上执行某些代码模板时,这些属性将被正确的属性替换。
其中的一个示例可能是:
From my point of view there're many options.
One would be with code generation. You can use T4 engine in order to read some configuration file and apply some attributes to an arbitrary member.
Learn more about T4 on http://msdn.microsoft.com/en-us/library/ff697195.aspx
Sometimes some class is part of some hierarchy and you can use some attribute in an abstract or virtual member, so derived classes overriding these wouldn't need that attribute since it's declared already in the base member.
About readability, you can use regions in order to hide members' attributes.
Anyways, I'll suggest you code generation approach, because it's the cleanest and simpler solution. Right, this isn't an alternative, because you'll have the exact code, but you'll avoid doing it by hand.
Finally, most of .NET APIs and third-party ones allows you to configure things with attributes or with some object model, so, maybe, when you find that you've your code full of attributes, some things could be made by creating your own configuration schema and use library's object model in order to configure your environment.
EDIT
I want to add that if you like code generation approach, you can use custom attributes that will be replaced by right ones when some code template is executed over some file.
A sample of that could be:
将 URL 声明为在其他地方定义的 const 字符串可能是一个开始
另外,谁说 X 个字符后需要换行?只要有很长的一行代码,关心的人就可以滚动阅读参数。
Declaring the URLs as const strings defined elsewhere might be a start
Also, who says you need a new line after X characters? Just have a really long line of code, and people who care can scroll across to read the parameters.