STO
STO

Reputation: 10638

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?

Upvotes: 13

Views: 19431

Answers (3)

Paul Hatcher
Paul Hatcher

Reputation: 8156

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.

Upvotes: 7

Ethan Wu
Ethan Wu

Reputation: 428

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>

Upvotes: 10

Daniel Lemke
Daniel Lemke

Reputation: 2449

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

Upvotes: 7

Related Questions