具有嵌套集合的自定义配置部分的正确实现?
在 Web 应用程序中,我希望能够使用如下所示的配置部分定义一些映射:
<configuration>
<configSections>
<sectionGroup name="MyCustomer">
<section name="CatalogMappings" type="MyCustom.MyConfigSection" />
</sectionGroup>
</configSections>
<MyCustomer>
<catalogMappings>
<catalog name="toto">
<mapping value="1" displayText="titi" />
<mapping value="2" displayText="tata" />
</catalog>
<catalog name="toto2">
<mapping value="1" displayText="titi2" />
<mapping value="2" displayText="tata2" />
</catalog>
</catalogMappings>
</MyCustomer>
</configuration>
我正在努力实现此目标,特别是在定义我的集合集合时。为了实现这一目标,我需要实现哪些类?
目前,我有:
public class CatalogMappingSection : System.Configuration.ConfigurationSection
{
public class Mapping : ConfigurationElement
{
[ConfigurationProperty("externalKey")]
public string ExternalKey { get; set; }
[ConfigurationProperty("displayText", IsRequired=true)]
public string DisplayText { get; set; }
[ConfigurationProperty("value", IsRequired=true, IsKey=true)]
public int Value { get; set; }
}
public class Catalog : ConfigurationElementCollection
{
[ConfigurationProperty("name", IsRequired=true, IsKey=true)]
public string Name { get; set; }
protected override ConfigurationElement CreateNewElement()
{
return new Mapping();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Mapping)element).Value;
}
}
public class CatalogCollection : ConfigurationElementCollection
{
[ConfigurationProperty("catalog")]
[ConfigurationCollection(typeof(Catalog))]
public Catalog CatalogMappingCollection
{
get
{
return (Catalog)base["catalog"];
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Catalog();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Catalog)element).Name;
}
}
[ConfigurationProperty("catalogMappings")]
[ConfigurationCollection(typeof(CatalogCollection))]
public CatalogCollection CatalogMappings
{
get
{
return (CatalogCollection)base["catalogMappings"];
}
}
}
但是,这没有按预期工作。
In a web application, I want to be able to define some mapping using a config section like this:
<configuration>
<configSections>
<sectionGroup name="MyCustomer">
<section name="CatalogMappings" type="MyCustom.MyConfigSection" />
</sectionGroup>
</configSections>
<MyCustomer>
<catalogMappings>
<catalog name="toto">
<mapping value="1" displayText="titi" />
<mapping value="2" displayText="tata" />
</catalog>
<catalog name="toto2">
<mapping value="1" displayText="titi2" />
<mapping value="2" displayText="tata2" />
</catalog>
</catalogMappings>
</MyCustomer>
</configuration>
I'm struggling to achieve this goal, especially when defining my collection of collections. What are the classes that I need to implement to achieve this?
Currently, I have:
public class CatalogMappingSection : System.Configuration.ConfigurationSection
{
public class Mapping : ConfigurationElement
{
[ConfigurationProperty("externalKey")]
public string ExternalKey { get; set; }
[ConfigurationProperty("displayText", IsRequired=true)]
public string DisplayText { get; set; }
[ConfigurationProperty("value", IsRequired=true, IsKey=true)]
public int Value { get; set; }
}
public class Catalog : ConfigurationElementCollection
{
[ConfigurationProperty("name", IsRequired=true, IsKey=true)]
public string Name { get; set; }
protected override ConfigurationElement CreateNewElement()
{
return new Mapping();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Mapping)element).Value;
}
}
public class CatalogCollection : ConfigurationElementCollection
{
[ConfigurationProperty("catalog")]
[ConfigurationCollection(typeof(Catalog))]
public Catalog CatalogMappingCollection
{
get
{
return (Catalog)base["catalog"];
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Catalog();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Catalog)element).Name;
}
}
[ConfigurationProperty("catalogMappings")]
[ConfigurationCollection(typeof(CatalogCollection))]
public CatalogCollection CatalogMappings
{
get
{
return (CatalogCollection)base["catalogMappings"];
}
}
}
But, this is not working as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于找到了这个人的例子。它经过编码并且开箱即用。
http://manyrootsofallevilrants.blogspot.com/2011/07/ nested-custom-configuration-collections.html
我将把代码粘贴到这里......只是因为当有人说“你的答案在这里”时我无法忍受,并且链接已失效。
请先尝试他的网站,如果有效,请留下“谢谢”。
请注意,如果您想要一个“更知名”(对象模型)示例,我还有一个“美国州和美国县”示例(此 SOF 问题的答案)。
I finally found this guy's example. It was coded and worked right out of the box.
http://manyrootsofallevilrants.blogspot.com/2011/07/nested-custom-configuration-collections.html
I am going to paste the code here......only because I cannot stand it when someone says "Your answer is here", and the link is dead.
Please try his website first, and leave a "thank you" if it works.
Note, I also have a "Usa-States and Usa-Counties" example (an answer to THIS SOF question) if you want a "more well known" (object model) example.
由于我的其他答案很受欢迎,我使用美国州和美国县编写了一个自定义示例。一个更容易理解的例子。
我还添加了一些接口,允许对单元测试进行一些模拟,这是我在编写这个原始答案时并没有完全意识到的。
我还添加了将设置放在自己的文件中,这样您的 app.config 文件中就不会包含十亿个内容。
我还添加了一个“Finder”来封装对集合中特定项目的搜索。
我还添加了一个自定义枚举。
“UsaStatesSettings.config”(确保将其标记为“始终复制”)
“app.config”
和现在的“代码”
Due to the popularity of my other answer, I've written a custom example using Usa-States and Usa-Counties. An example that's a tad easier to follow.
I've also added some interfaces, which allows some mocking for unit-tests, something I wasn't fully aware of when I wrote this original answer.
I also added putting the settings in its own file, so your app.config file doesn't have a billion things in it.
I've also added a "Finder" to encapsulate searching for a specific item in the collection.
I've also added a custom Enum.
"UsaStatesSettings.config" (make sure you mark this as "copy-always")
The "app.config"
and now the "code"