Unity:建立字典

发布于 2024-08-04 17:35:58 字数 1910 浏览 3 评论 0原文

我正在将 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 技术交流群。

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

发布评论

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

评论(2

倾`听者〃 2024-08-11 17:35:58

我认为您必须使用 method 元素来存档此内容。这不是很好,但是一个解决方法。

您的类型必须定义一个方法 Add(string key, string value),统一容器使用该方法来注入值。

<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>

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.

<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>

Unity definitely does not support dictionaries for container configuration. See Build Dictionaries using Unity container?

别挽留 2024-08-11 17:35:58

我发现Unity在处理泛型时有bug
(http://unity.codeplex.com/Thread/View.aspx?ThreadId =30292),
有一个非常丑陋的解决方法:

public class MyDictionary : Dictionary<string,string>{

    public MyDictionary() { 

    }
}

现在在配置文件中:

        <typeAlias alias="string" type="System.String, mscorlib" />            
        <typeAlias alias="Dictionary" type="MyFakeNamespace.MyDictionary, MyFakeAppDll" />

...

然后使用 Jehof 建议:

<type name="ConfigurationDictionary" type="Dictionary">
                    <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
                        <method name="Add" key="0">
                            <param name="key" parameterType="string">
                                <value value="localHost"/>
                            </param>
                            <param name="value" parameterType="string">
                                <value value="127.0.0.1"/>
                            </param>
                        </method>
                        <method name="Add" key="1">
                            <param name="key" parameterType="string">
                                <value value="contentHost"/>
                            </param>
                            <param name="value" parameterType="string">
                                <value value="\\content"/>
                            </param>
                        </method>
                        <method name="Add" key="2">
                            <param name="key" parameterType="string">
                                <value value="virtualDir"/>
                            </param>
                            <param name="value" parameterType="string">
                                <value value="/"/>
                            </param>
                        </method>
                    </typeConfig>

                 </type>

方法标记中的 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:

public class MyDictionary : Dictionary<string,string>{

    public MyDictionary() { 

    }
}

now in the configuration file:

        <typeAlias alias="string" type="System.String, mscorlib" />            
        <typeAlias alias="Dictionary" type="MyFakeNamespace.MyDictionary, MyFakeAppDll" />

...

and then using the Jehof suggestion:

<type name="ConfigurationDictionary" type="Dictionary">
                    <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
                        <method name="Add" key="0">
                            <param name="key" parameterType="string">
                                <value value="localHost"/>
                            </param>
                            <param name="value" parameterType="string">
                                <value value="127.0.0.1"/>
                            </param>
                        </method>
                        <method name="Add" key="1">
                            <param name="key" parameterType="string">
                                <value value="contentHost"/>
                            </param>
                            <param name="value" parameterType="string">
                                <value value="\\content"/>
                            </param>
                        </method>
                        <method name="Add" key="2">
                            <param name="key" parameterType="string">
                                <value value="virtualDir"/>
                            </param>
                            <param name="value" parameterType="string">
                                <value value="/"/>
                            </param>
                        </method>
                    </typeConfig>

                 </type>

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.

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