具有两个键和一个对象的最佳 C# 集合是什么?

发布于 2024-07-29 02:49:48 字数 1442 浏览 2 评论 0 原文

我有一个 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云仙小弟 2024-08-05 02:49:48

严格满足您的标准,请使用 Dictionary>

var dict = new Dictionary<string, Dictionary<string, object>>();
...
object view = dict["customer"]["help"];
Dictionary<string, object>.ValueCollection views = dict["customer"].Values;

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;
红玫瑰 2024-08-05 02:49:48

正如类似线程中提到的,在 .NET 4 中表示 2 键字典的一个好方法是使用 元组 类:

IDictionary<Tuple<K1, K2>, V>

As mentioned in a similar thread, a good way to represent a 2-key dictionary in .NET 4 is to use the Tuple class:

IDictionary<Tuple<K1, K2>, V>
赠意 2024-08-05 02:49:48

您所描述的内容听起来像是字典中的复合键,而不是两个键。 我建议设置一个简单的结构来表示这个键:

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

   // override ToHashCode, Equals and implement IComparable.
}

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文