Unity:建立字典
我正在将 Castle/Monorarails 应用程序转换为 Unity/Asp.NET MVC 应用程序, 我一直在尝试转换此组件配置:
<component
id="ComponentBaseConfiguration"
service="MyFakeNamespace.BOL.IConfiguration, MyFakeAppDll"
type="MyFakeNamespace.BOL.ConfigurableConfiguration, MyFakeAppDll">
<parameters>
<!-- Setting Configuration (Dictionary<string,string>)-->
<Config>
<dictionary>
<entry key="localHost">#{LocalHost}</entry>
<entry key="contentHost">#{ContentHost}</entry>
<entry key="virtualDir">#{VirtualDir}</entry>
</dictionary>
</Config>
</parameters>
似乎Unity支持数组但不支持字典,我想做这样的事情:
<unity>
<containers>
<container>
<types>
<type name="ComponentBaseConfiguration" type="MyFakeNamespace.BOL.IConfiguration, MyFakeAppDll" mapTo="MyFakeNamespace.BOL.ConfigurableConfiguration, MyFakeAppDll">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
<property name="Config" propertyType="System.Collections.Generic.Dictionary`2[[System.String, mscorlib], [System.String, mscorlib]],mscorlib">
<dictionary>
<entry key="localHost">127.0.0.1</keyedValue>
<entry key="contentHost">\\content</keyedValue>
<entry key="virtualDir">/</keyedValue>
</dictionary>
</property>
</typeConfig>
</type>
</types>
</container>
</containers></unity>
我怎样才能实现这样的事情?
I'm converting a Castle/Monorails application into a Unity/Asp.NET MVC one,
I'm stuck in trying to converting this component configuration:
<component
id="ComponentBaseConfiguration"
service="MyFakeNamespace.BOL.IConfiguration, MyFakeAppDll"
type="MyFakeNamespace.BOL.ConfigurableConfiguration, MyFakeAppDll">
<parameters>
<!-- Setting Configuration (Dictionary<string,string>)-->
<Config>
<dictionary>
<entry key="localHost">#{LocalHost}</entry>
<entry key="contentHost">#{ContentHost}</entry>
<entry key="virtualDir">#{VirtualDir}</entry>
</dictionary>
</Config>
</parameters>
seems that Unity supports Array but not Dictionary, I would like to do something like this:
<unity>
<containers>
<container>
<types>
<type name="ComponentBaseConfiguration" type="MyFakeNamespace.BOL.IConfiguration, MyFakeAppDll" mapTo="MyFakeNamespace.BOL.ConfigurableConfiguration, MyFakeAppDll">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
<property name="Config" propertyType="System.Collections.Generic.Dictionary`2[[System.String, mscorlib], [System.String, mscorlib]],mscorlib">
<dictionary>
<entry key="localHost">127.0.0.1</keyedValue>
<entry key="contentHost">\\content</keyedValue>
<entry key="virtualDir">/</keyedValue>
</dictionary>
</property>
</typeConfig>
</type>
</types>
</container>
</containers></unity>
How can I achieve something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您必须使用 method 元素来存档此内容。这不是很好,但是一个解决方法。
您的类型必须定义一个方法 Add(string key, string value),统一容器使用该方法来注入值。
Unity 绝对不支持容器配置的字典。请参阅使用 Unity 容器构建字典?
I think you have to use the method-element to archive this. It´s not nice but a workaround.
Your type must define a method Add(string key, string value) which the unity container uses to inject the values.
Unity definitely does not support dictionaries for container configuration. See Build Dictionaries using Unity container?
我发现Unity在处理泛型时有bug
(http://unity.codeplex.com/Thread/View.aspx?ThreadId =30292),
有一个非常丑陋的解决方法:
现在在配置文件中:
...
然后使用 Jehof 建议:
方法标记中的 key 属性需要是唯一的,以便多次调用方法 Add 。
然后,当 bug 得到解决时,typeAlias 中的一点变化允许我们输入正确的类型,但我想我会保持原样。
I discovered that Unity have bugs when handling Generics
(http://unity.codeplex.com/Thread/View.aspx?ThreadId=30292),
there is a quite ugly workaround to this:
now in the configuration file:
...
and then using the Jehof suggestion:
the key attribute in the method tag need to be unique in order to call the method Add multiple times.
Then, when the bug will be solved a little change in the typeAlias allow us to put the right type, but I think I will leave as is it.