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
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> Integration = <span style="color: #006080">&quot;Integration&quot;</span>;</pre>

<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>

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.

7 Comments

  • Aha, cool. I knew there was better way.

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

  • @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

  • 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 :-)

  • @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 (http://msdn.microsoft.com/en-us/library/88c54tsw(VS.71).aspx).
    Bottom line, it doesn't matter what are the details of implementation, as long as no "magical strings" are around :)

  • @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. :-)

  • @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.

Comments have been disabled for this content.