深层嵌套字典是反模式吗? [英] Is a deep nested Dictionary an antipattern?
问题描述
我有一个结构,可以很容易地用一个三层嵌套的字典来表示,就像这样
private static Dictionary>>预渲染模板;
结构可能会用在这样的地方
PrerenderedTemplates[instanceID][templategroup][templatepart]
现在,我意识到这段代码很难阅读,因为从查看定义语句中,您无法知道它的用途.将其更改为 Dictionary
的唯一优势是可读性.将每个嵌套转换为它自己的类(例如 class PrerenderedTemplate{} class TemplateGroup{} class TemplatePart{}
)会添加更多的代码行,以获得很少的(如果有的话)计算优势.据我所知.
- 那么,我的方法可以"还是应该加倍努力并创建单独的课程?
- 是否可以在文档/评论中介绍嵌套的
Dictionary
的工作原理 - 是否有处理此类嵌套的最佳做法?
- 请记住,这是一个私有成员,对于使用该类的人来说,它不需要简单明了.
更新
因此,受到 Reza 的启发,但无法使用元组,我决定创建自己的密钥生成器并像这样实现他的模式:
private Dictionary预渲染模板;私有字符串 GetPrerenderedTemplateKey(字符串 InstanceId,字符串 FeatureId,字符串 OptionId){返回新的 StringBuilder(instanceId).Append(FormatTools.LIST_ENTRY_DELIMITER).Append(模板组).Append(FormatTools.LIST_ENTRY_DELIMITER).Append(templatepart).ToString();}
其中 FormatTools.LIST_ENTRY_DELIMITER
是 Unicode 专用字符 0xe04d
.
我提供另一种选择:
Dictionary, string>点;
访问字典:
pt[Tuple.Create("id","group","part")]
<块引用>
更新:
值元组在C# 7中引入的最引人注目:
Dictionary<(string id, string group, string part), string>点;
访问字典:
pt[("id", "group", "part")]
I have a structure that can be very easily represented using a three-deep nested dictionary, like so
private static Dictionary<string, Dictionary<string, Dictionary<string,string>>> PrerenderedTemplates;
Where the structure might be used something like this
PrerenderedTemplates[instanceID][templategroup][templatepart]
Now, I realise that this code is hard to read, because from looking at the definition statement, you can't tell what it's being used for. The only advantage I can really see in changing it to Dictionary<string, PrerenderedTemplate>
is readability. Converting each nesting into its own class (e.g class PrerenderedTemplate{} class TemplateGroup{} class TemplatePart{}
) would add many more lines of code for little (if any) computational advantage. As far as I can see.
- So, is my approach "ok" or should I go the extra mile and create seperate classes?
- Is it okay to cover how the nested
Dictionary
works in the documentation/comments - Is there a best practice for handling this sort of nesting?
- Bear in mind, this is a private member, it doesn't need to be straightforward for people using the class.
Update
So, inspired by Reza, but unable to use Tuples, I decided to create my own key generator and implement his pattern like this:
private Dictionary<string, string> PrerenderedTemplates;
private string GetPrerenderedTemplateKey(string InstanceId, string FeatureId, string OptionId)
{
return new StringBuilder(instanceId)
.Append(FormatTools.LIST_ENTRY_DELIMITER)
.Append(templategroup)
.Append(FormatTools.LIST_ENTRY_DELIMITER)
.Append(templatepart).ToString();
}
Where FormatTools.LIST_ENTRY_DELIMITER
is the Unicode Private Use Character 0xe04d
.
I offer another choice:
Dictionary<Tuple<string, string, string>, string> pt;
Access to dictionary:
pt[Tuple.Create("id","group","part")]
UPDATE:
Value Tuples introduced in C# 7 is most eye-catching:
Dictionary<(string id, string group, string part), string> pt;
Access to dictionary:
pt[("id", "group", "part")]
这篇关于深层嵌套字典是反模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!