Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Personally I prefer the first solution, possibly with an additional property which returns <em>all</em> the values.</p> <p>It's much more OO - enums are basically "named numbers", that's all. Everywhere else in code, state is stored in properties - so why use attributes instead? As Eric Lippert wrote in a <a href="http://blogs.msdn.com/b/ericlippert/archive/2009/02/02/properties-vs-attributes.aspx" rel="nofollow">blog post comparing the two</a>, "Attributes are facts about the mechanisms". You're basically using them as a way of providing data about values instead, and that just feels wrong to me.</p> <p>Your first two objections to using POCOs in terms of the potential mismatch between code and the database doesn't ring true either - because they're exactly the same for enums as well, aren't they? Besides, it's very easy to write a test which validates the data - you could even do so on startup if you wanted.</p> <p>It's not clear what the rest of your <code>AssetClass</code> does, but if it only has a <em>private</em> constructor, then you get a lot of the benefits of enums in terms of a limited, well-known set of values, but without the problem that enums are basically just numbers.</p> <p>In fact, the POCO solution is <em>better</em> than the enum in terms of value limitation - because the only "invalid" POCO value is null, whereas it's easy to come up with an invalid enum value:</p> <pre><code>FooEnum invalid = (FooEnum) 12345; </code></pre> <p>Are you going to check for that everywhere? Generally null references go bang considerably earlier than invalid enum values, and are also easier to check for.</p> <p>One downside I can think of in the POCO approach is that you can't switch on it. There are ways around that, but they're not terribly pleasant - you'd basically have to <em>also</em> have a set of well-known numbers (which could be an enum, of course) so that some property would return that and you could switch on it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload