mapping constants to resource strings for lists population and lookup

!!!Albert-0-

New Member
After an extensive search here I still need an expert advice:RequirementsRequirements are to map int consts to strings from resource files so localization will work fine and we can easily create a list of value pairs for dropdown lists as well as doing backward lookup on int value. Const integers are defined as static in different classes.So, here is how it's implemented currently and something tells me it can be better and deserves critics:Dictionary class:\[code\]public class TLiteralDic : Dictionary<int, string>{ //Lookup method public string getLiteral(int key) { if (ContainsKey(key)) { return this[key]; } return string.Empty; }}\[/code\]Somewhere close to UI layer an extension method to fill dropdown controls is defined like this:\[code\]public static void fill(this TLiteralDic dic, RNDropDownList ddlst){ ddlst.Items.Clear(); foreach (KeyValuePair<int, string> v in dic) { ddlst.Items.Add(new ListItem(v.Value, v.Key.ToString())); }}\[/code\]Adding pairs (static const int to string from a resource file):\[code\]public static class TLiterals{private static TLiteralDic _fileStatus;public static TLiteralDic FileStatus{ get { if (_fileStatus == null) { _fileStatus = new TLiteralDic() { {TFiles.Status.Cancelled, Lists.FileStatus_Cancelled}, {TFiles.Status.Closed, Lists.FileStatus_Closed}, {TFiles.Status.OnHold, Lists.FileStatus_OnHold}, {TFiles.Status.Open, Lists.FileStatus_Open}, {TFiles.Status.Pending, Lists.FileStatus_Pending}, {TFiles.Status.Portal, Lists.FileStatus_Portal} }; } return _fileStatus; }}//... hundred of lists like that, short and long (up to 15 entries)}\[/code\]Using in codeLookup:\[code\]textStatus.Text = TLiterals.FileStatus.getLiteral(row.FileStatus);\[/code\]List fill:\[code\]TLiterals.FileStatus.fill(ddlstFileStatus);\[/code\]ConsiderationsThe idea is to have only one place where the mapping is defined and be able to create a list out of it or do a lookup by int value. Ideally with good performance, minimal memory footprint and coding hassle.Already considered the following alternatives:
  • using switch (was used in the past for lookup - a lot of redundant code)
  • using reflection
  • closures
  • JSON, XML
  • using T4 class generation in VS2010
No simple or obviously better solution was found so far.Issues in the current implementation
  • a lot of repetitive code around the actual list of pairs, ideally must be all hidden and reused
  • need to define static private property; the idea was to use "lazy" initialization for lists only when they are retrieved the first time.
  • keeping literals in memory after the first use maybe too high price for such simple operation
Advantages with the current implementation
  • list defined in one place (vs switch lookup and manual list population in 2 different places using the same value pairs)
  • reused code for lookup and list filling.
  • simplicity to maintain and compile time checks.
Any ideas for a better "beautiful code"? :)Ideally, I'd like to see something like this when defining the list but it should not initialize until really needed/used:\[code\]public static TLiteralDic FileStatus = new TLiteralDic () { {TFiles.Status.Cancelled, Lists.FileStatus_Cancelled}, {TFiles.Status.Closed, Lists.FileStatus_Closed}, {TFiles.Status.OnHold, Lists.FileStatus_OnHold}, {TFiles.Status.Open, Lists.FileStatus_Open}, {TFiles.Status.Pending, Lists.FileStatus_Pending}, {TFiles.Status.Portal, Lists.FileStatus_Portal}}\[/code\]
 
Back
Top