Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best C# collection with two keys and an object?

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)
like image 982
Edward Tanguay Avatar asked Jul 23 '09 13:07

Edward Tanguay


People also ask

Which is best C++ or C?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.

What is C language best used for?

The C programming language is the recommended language for creating embedded system drivers and applications. The availability of machine-level hardware APIs, as well as the presence of C compilers, dynamic memory allocation, and deterministic resource consumption, make this language the most popular.

What is the most current version of C?

Published in June 2018 as ISO/IEC 9899:2018, C17 is the current standard for the C programming language.


1 Answers

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;
like image 113
Steve Gilham Avatar answered Oct 14 '22 18:10

Steve Gilham