Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject constructor argument from config file with Unity

Imagine we have a class

public class MyClass
{
    private string _val;
    public MyClass(string val) 
    {
         _val = val;
    }
}

and app.config (or web.config)

<appSettings>
    <add key="value" value="some value" />
</appSettings>

Is there way to register type MyClass in Unity container and point Unity to inject value for val constructor parameter from config file?

like image 839
STO Avatar asked Nov 18 '11 19:11

STO


3 Answers

it is very easy.

C# Code:

var container = new UnityContainer();
container.LoadConfiguration();
MyClass mc = container.Resolve<MyClass>();

Configuration file:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">    
 <container>
  <register type="[namespace].MyClass, [assembly-name]" 
    mapTo="[namespace].MyClass, [assembly-name]">
    <constructor>
      <param name="val" value="Ethan Woo"/>
    </constructor>
  </register>
</container>

like image 162
Ethan Wu Avatar answered Nov 10 '22 07:11

Ethan Wu


Quite an old post but I thought the following information could be helpful in case that it's not a value for a native type but a complex data type instead:

<configuration>

  <configsections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration">
    </section>
  </configsections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

    <alias alias="IRepository" type="UnityTest.IRepository, UnityTest">
      <alias alias="Repository" type="UnityTest.Repository, UnityTest">

        <container>
          <register mapto="Repository" type="IRepository">

            <register name="MyClass" type="UnityTest.MyClass, UnityTest">
              <constructor>
                <param name="repository" type="IRepository">
                <dependency name="IRepository">

                </dependency>
              </constructor>
            </register>

          </register>
        </container>

      </alias>
    </alias>
  </unity>

</configuration>

A bit more detailed described here: http://postlabs.blogspot.com/2015/05/injecting-non-native-data-type-via.html

like image 7
Daniel Lemke Avatar answered Nov 10 '22 08:11

Daniel Lemke


If you are using XML config you can do this by defining an extension that handles AppSettings as Unity parameters, see http://www.neovolve.com/2010/04/23/appsetting-parameter-injection-in-unity-2/.

Alternatively, if you are doing C# configuration you can use an injection constructor as follows...

var container = new UnityContainer();
container.RegisterType<MyClass>(
    new InjectionConstructor(
       InjectionParameter<string>(ConfigurationManager.AppSettings["value"])));

The reason to use the AppSettings value rather than the string directly in the XML config is that it centralises all the paramter values into AppSettings and simplifies migrations between environments.

like image 7
Paul Hatcher Avatar answered Nov 10 '22 08:11

Paul Hatcher