开发者

Is there an alternative to large messy attributes?

开发者 https://www.devze.com 2023-03-06 19:51 出处:网络
I often find that attributes can be too large. Sometimes it feels like the attributes take up more of the screen than the code.

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)
{
    //...
}


While it doesn't solve all your problems, you should be using constants for your repeated values, especially strings.

[SoapDocumentMethod(
    URL, RequestNamespace = NAMESPACE, ResponseNamespace = NAMESPACE, 
    Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
public Response GetCustomerDetails(Request request)
{
    //...
}


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:

[SomeFake]
public void A() {}

... and after code generation

[SomeActual(Allow = true, Loggable = true)]
public void A() {}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消