views:

713

answers:

2

I've got a MenuManager class to which each module can add a key and and element to be loaded into the main content:

private Dictionary<string,object> _mainContentItems = new Dictionary<string,object>();
public Dictionary<string,object> MainContentItems
{
    get { return _mainContentItems; }
    set { _mainContentItems = value; }
}

So the customer module registers its views like this:

layoutManager.MainContentViews.Add("customer-help", this.container.Resolve<HelpView>());
layoutManager.MainContentViews.Add("customer-main", this.container.Resolve<MainView>());

So then later to bring a specific view to the front I say:

layoutManager.ShowMainContentView("customer-help");

And to get the default view (first view registered), I say:

layoutManager.ShowDefaultView("customer");

And this works fine.

However, I want to eliminate the "code smell" with the hyphen which separates module name and view name, so I would like to register with this command:

layoutManager.MainContentViews.Add("customer","help", this.container.Resolve<HelpView>());

But what is the best way to replace my current Dictionary, e.g. what comes to mind are these:

  • Dictionary<string, string, object> (doesn't exist)
  • Dictionary<KeyValuePair<string,string>, object>
  • Dictionary<CUSTOM_STRUCT, object>

The new collection needs to be able to do this:

  • get a view with module and view key (e.g. "customer", "help" returns 1 view)
  • get a collection of all views by module key (e.g. "customer" returns 5 views)
A: 

What you're describing sounds like a composite key into the dictionary, rather than two keys. I would recommend setting up a simple structure to represent this key:

struct Section {
   string Area { get; set; } 
   string Area2 { get; set; }

   // override ToHashCode, Equals and implement IComparable.
}
Ryan Brunner
+6  A: 

Strictly meeting your criteria, use a Dictionary<string, Dictionary<string, object>> ;

var dict = new Dictionary<string, Dictionary<string, object>>();
...
object view = dict["customer"]["help"];
Dictionary<string, object>.ValueCollection views = dict["customer"].Values;
Steve Gilham
Elegant in use.
Will
worked well, thanks.
Edward Tanguay