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?

1

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

5 Comments

Where is the reference to the appSetting named "value" in this configuration file?
<param name="val" value="Ethan Woo"/> : "value" attribute here.
There is no reference to a config files. Then how to map config file or just from <app key> ?
"Configuration file" here means app.config for win/console app, and web.config for iis hosting application e.g. web, wcf... container.LoadConfiguration(); will automatically load the configuration accordingly.
You don't aswer the question. This answer concerns about a key/value from web.config to Unity constructor and not how to define Unity constructor in web.config.
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

Comments

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.

2 Comments

why RegisterType<MyClass, MyClass>() register type function specify two name of same class?
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<IMyInterface, MyClass>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.