我有一个 MenuManager 类,每个模块都可以向其中添加一个键和要加载到主要内容中的元素:
private Dictionary<string,object> _mainContentItems = new Dictionary<string,object>();
public Dictionary<string,object> MainContentItems
{
get { return _mainContentItems; }
set { _mainContentItems = value; }
}
因此,客户模块会像这样注册其视图:
layoutManager.MainContentViews.Add("customer-help", this.container.Resolve<HelpView>());
layoutManager.MainContentViews.Add("customer-main", this.container.Resolve<MainView>());
所以稍后会带来一个特定的视图到前面我说:
layoutManager.ShowMainContentView("customer-help");
为了获得默认视图(注册的第一个视图),我说:
layoutManager.ShowDefaultView("customer");
这工作正常。
但是,我想用分隔模块名称和视图名称的连字符消除“代码味道”,因此我想使用此命令进行注册:
layoutManager.MainContentViews.Add("customer","help", this.container.Resolve<HelpView>());
但是替换我当前词典的最佳方法是什么,例如会出现什么要记住的是这些:
-
Dictionary (不存在)
-
Dictionary, object>
-
Dictionary
新集合需要能够做到this:
- 获取带有模块和视图键的视图(例如“customer”、“help”返回 1 个视图)
- 按模块键获取所有视图的集合(例如“customer”返回 5 个视图)
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)
发布评论
评论(3)
严格满足您的标准,请使用
Dictionary>
;Strictly meeting your criteria, use a
Dictionary<string, Dictionary<string, object>>
;正如类似线程中提到的,在 .NET 4 中表示 2 键字典的一个好方法是使用 元组 类:
As mentioned in a similar thread, a good way to represent a 2-key dictionary in .NET 4 is to use the Tuple class:
您所描述的内容听起来像是字典中的复合键,而不是两个键。 我建议设置一个简单的结构来表示这个键:
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: