a few suggestions

Rune Funch Søltoft replied to Chris Falter at 24-Apr-08 05:23
First of all nice article. We've been using the named consts and enum solutions quite often but this might be our way or solving Magic String problems in the future.
Reading the article I came to think of a few things that i might change when we're using it.
I would make the construtor and the constants private and expose a few "named singletons" like:
 private static readonly MarriedStatus _married = new MarriedStatus(MarriedConst);
 public static MarriedStatus Married{ get {return _married;}}

I dont see any reason why it should be possible to call the constructor since we know already which calls would be appropriate and with this change we have now change a possible runtime error to a compile time error.

The above will of cause only work in a scenario as the one given in the example where all calls to the constructor will be of the format MarriedStatus(namedconstant). It will not work if calls like MarriedStatus(AMethodReturningStatusMagicString()) in scenarios where that's need the above change will of cause only complicate things.

second change i would make is to the part of code that exposes the dictionary i think i would write that with a yield return in stead (and exposing an IEnumerable<KeyValuePair<x,y>> instead) this way we'll only need to iterate the inner dictionary once (when the third party is using it) instead of at least twice; when created and when used.

as I started saying I like the idea and we might where well use it so thx for the heads up on this approach



Click here to sign in and reply. You could earn money via our $500 contest just for being helpful.
  Article Discussion: A Good Solution for "Magic String" Data - Chris Falter  06-Mar-08 01:51 1:51:03 PM
      Yo, Chris! - Peter Bromberg  06-Mar-08 07:32 7:32:13 PM
      a few suggestions - Rune Funch Søltoft  24-Apr-08 05:23 5:23:53 AM
          Re: a few suggestions - Chris Falter  24-Apr-08 01:05 1:05:57 PM
              IEnumerable<KeyValuePair<string, string>> implementation - Rune Funch Søltoft  25-Apr-08 04:02 4:02:16 AM
View Posts