13

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?

Liam
  • 27,717
  • 28
  • 128
  • 190
STO
  • 10,390
  • 8
  • 32
  • 32
  • possible duplicate of [In Unity config, how to pass connectionString to a constructor?](http://stackoverflow.com/questions/4252231/in-unity-config-how-to-pass-connectionstring-to-a-constructor) – Mark Seemann Nov 18 '11 at 20:24

3 Answers3

10

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>

Ethan Wu
  • 428
  • 5
  • 15
7

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.

Andrew McLachlan
  • 349
  • 1
  • 14
Paul Hatcher
  • 7,342
  • 1
  • 54
  • 51
  • why `RegisterType()` register type function specify two name of same class? – Mou Sep 15 '15 at 13:56
  • The original XML registration says type, mapTo with the same type in both so I was reproducing that. For the fluent registration, you are correct you don't need the second value; typically however you you have RegisterType – Paul Hatcher Sep 17 '15 at 08:09
7

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

Daniel Lemke
  • 1,966
  • 18
  • 24