ken*_*ura 6 c# asp.net castle-monorail unity-container
我正在将Castle/Monorails应用程序转换为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>
Run Code Online (Sandbox Code Playgroud)
似乎Unity支持Array但不支持Dictionary,我想做这样的事情:
<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>
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这样的目标?
我认为你必须使用方法 -element来存档它.这不是一件好事,而是一种解决方法
您的类型必须定义一个方法Add(字符串键,字符串值),unity容器用它来注入值.
<method name="Add">
<param name="key" parameterType="string">
<value value="localHost"/>
</param>
<param name="value" parameterType="string">
<value value="127.0.0.1"/>
</param>
</method>
Run Code Online (Sandbox Code Playgroud)
Unity肯定不支持容器配置的字典.请参阅使用Unity容器构建字典?