Thursday, May 07, 2009 9:45 PM Sean Feldman

CategoryAttribute

Anyone who's doing TDD is familiar with the CategoryAttribute coming with the most of frameworks. Today (I am surprised it took us so long!) we got read of

[Category("Integration")]

and started to use the right approach

[Category(Categories.Integration)]

where Categories is a sealed class with constants

public sealed class Categories
{
    public const string Integration = "Integration";
}

Why is it good? First - it's DRY (rather that finding strings such as "Integration" and "Integrations" all over the place. Second - it's easy to refactor.

Filed under:

Comments

# re: CategoryAttribute

Friday, May 08, 2009 4:35 AM by chrissie1

Aha, cool. I knew there was better way.

# re: CategoryAttribute

Friday, May 08, 2009 4:44 AM by chrissie1

Do you mind if I post the VB.Net version on my blog. Of course I will mention your post in my post.

# re: CategoryAttribute

Friday, May 08, 2009 10:10 AM by Sean Feldman

@chrissie1,

Go ahead, the code isn't "mine" - it "belongs to the masses". More people do better code, better it will be for all of us.

Sean

# re: CategoryAttribute

Friday, May 08, 2009 10:11 AM by Mike

Hey Sean,

My only question is why use sealed? Not that I see it making a difference one way or the other. It's just that I have a strong and unpleasant visceral reaction when I see that keyword :-)

# re: CategoryAttribute

Friday, May 08, 2009 10:23 AM by Sean Feldman

@Mike,

Funny when you mention it. It it somewhat a singleton, but using the word 'static' with all constants felt weird. But I wanted to make sure that the class is not inheritable, so I marked it as sealed. According to C# design, this is what you should use on classes and methods to prevent extensibility (msdn.microsoft.com/.../88c54tsw(VS.71).aspx).

Bottom line, it doesn't matter what are the details of implementation, as long as no "magical strings" are around :)

# re: CategoryAttribute

Friday, May 08, 2009 11:16 AM by Mike

@Sean

My understanding is that when you define a var using const that the compiler replaces its usages at build time. So [Category(Categories.Integration)] becomes [Category("Integration")] if you look at it using reflector. If you use static readonly then its stays a reference var. So in this case making the class sealed is redundant. An inheriting class would not be able to override the value anyway.

Not trying to start a holy war here or anything I was just wondering if you had some reason for doing this which I was missing the point for. :-)

# re: CategoryAttribute

Friday, May 08, 2009 12:49 PM by Sean Feldman

@Mike,

you are absolutely right - const value will be replaced at compile time with the value itself. But during development, I don't want to be able to touch the Categories class. Therefore I used sealed. Static has a different meaning to myself. No holly wars :) The point is that no longer my team has to wonder what string to put in :) And that's what matters most.