After the project was completed we discussed ways we could have done it better and we agreed that using enumerables would have been a better choice. Outside of making sure you're using the right one there is no real possibility of spelling or capitalization mistakes. MY main problem with this approach was the need to completely define our Enum in a centralized location. If we ever added additional functionality to our project we would have to go and expand that Enum as needed. I want the ability to expand out Enum as needed while maintaining the main purpose of an Enum. So I came up with the following:
public class EnumBase { // I don't know why we would want to count the number of objects // genereated from our EnumBase but it's here if we ever want it. private static int _Count = 0; // Class constructor. Basic stuff. Post increments Count, // and thus _Count, every time it's called. public EnumBase() { Count++; } // Our public interface to _Count. Provides a public 'get' // and a private 'set' for access to our _Count. public static int Count { get { return _Count; } private set { _Count = value; } } }
Pretty straight forward, and most of the guts of this class are unneeded, but it allows you to do the following:
public class EnumFoo { public static readonly EnumBase One = new EnumBase(); public static readonly EnumBase Two = new EnumBase(); } public class EnumBar { public static readonly EnumBase Red = new EnumBase(); public static readonly EnumBase Blue = new EnumBase(); }
Which in turn allows you to the following:
Dictionary<EnumBase, List<Action<Object>>> Diction = new Dictionary<EnumBase, List<Action<object>>>(); Diction.Add(EnumFoo.One, Func); Diction.Add(EnumFoo.Two, Func2); Diction.Add(EnumBar.Red, Func3);
I'm using the Dictionary from my Mediator class as an example, assuming we passed EnumBase to the template. Using this method allows us to expand our list of unique "enumerables" as needed without having to continually expand a centralized Enum.
As always comments and suggestions are welcome!
No comments:
Post a Comment