Unity: Build up a dictionary
Asked Answered
P

2

6

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?

Pearl answered 15/9, 2009 at 13:8 Comment(2)
I got something like this to work. see - #5597992Flatulent
Here is my solution for older version - it requires some changes in code - when I will have time maybe I will try to adapt that to new vesion of Unity unity.codeplex.com/discussions/230927Multitudinous
E
6

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?

Erepsin answered 15/9, 2009 at 13:37 Comment(1)
I had to use a different format, to work:<br/> <method name="Add"> <param name="key" value="tstKey"/> <param name="value" value="tstValue"/> </method>Drive
P
1

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.

Pearl answered 16/9, 2009 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.