2

Say we have:

class A
{
   ILogger myLog;
   A(ILogger log)
   {
      this.myLog = log;
   }
 ...
}

And we have registered the ILogger interface before in the unity container, e.g.

 container.RegisterType<ILogger, SomeLogger>();

And here the SomeLogger class:

class SomeLogger : ILogger
{
   string myString;
   SomeLogger(string test)
   {
     myString = test;
   }
 ...
}

Now, how can unity create an instance of SomeLogger for class A without passing a string to the ctor of SomeLogger? Suppose there is no other ctor for SomeLogger. Where can one specify the parameter(s) for ctor of the mapped SomeLogger type?

3 Answers 3

2

You can do that in your configuration:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    </configSections>
    <unity>
        <containers>
            <container>
                <types>
                    <type type="[Namespace].ILogger, [AssemblyName]" mapTo="[Namespace].SomeLogger, [AssemblyName]">
                        <constructor>
                            <param name="test">
                                <value value="MyDesiredValue" />
                            </param>
                        </constructor>
                    </type>
                </types>
            </container>
        </containers>
  </unity>
</configuration>

This also declaratively registers your type, so the

container.RegisterType<ILogger, SomeLogger>();

call is no longer necessary.

-Doug

1
  • Thanks for this example. How can one specify the this in code? The doumentation only talks about ctor parameters for the injection type but not for the mapped type of an interface.
    – Juergen
    Oct 12, 2011 at 14:31
2

You can do this:

container.RegisterType<ILogger, SomeLogger>(new InjectionConstructor("myStringValue"));
1

you could also do this in the registration code as follows:

UnityContainer.RegisterType<ILogger, SomeLogger>();
UnityContainer.Configure<InjectedMembers>()
              .ConfigureInjectionFor<SomeLogger>(new InjectionConstructor("TestString"));
4
  • Many thanks, this is exactly what I was looking for. In my concrete problem I have a WFC callback class and the WCF client ctor needs an instance context. Until now I did not know hot to pass this instance context to the unity container for the mapping for the WCF client.
    – Juergen
    Oct 12, 2011 at 14:35
  • 1
    There's an overload to RegisterType that takes a params InjectionMember[] which you can use to pass in the InjectionConstrutor directly. No need to call configure separately.
    – BFree
    Oct 12, 2011 at 14:37
  • What happens if one does not provide the string paramter for the unity container? Runtime excpetion?
    – Juergen
    Oct 12, 2011 at 14:43
  • 1
    @Juergen I don't know, I would assume null is passed for nullable types? I'll have to test that..
    – RoelF
    Oct 12, 2011 at 14:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.