16

I have one interface: IFoo
Two classes implementing that interface: FooOne and FooTwo
And two classes ClassOne and ClassTwo receiving an IFoo parameter in the constructor.

How I configure unity so ClassOne receives a FooOne instance and ClassTwo receives a FooTwo using only one container?

I can't do it at runtime so it must be in the config file.

pomber
  • 23,132
  • 10
  • 81
  • 94

3 Answers3

19

Have a look at the Unity documentation.

For a more readable config file you should define type aliases for IFoo, FooOne, FooTwo, ClassOne and ClassTwo. Then you need to register the mappings from IFoo to your implementations. You need to set a name for the mappings. For the consumers of IFoo you need to register an InjectionConstructor.

Your config will look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
      Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="IFoo" type="UnityConfigFile.IFoo, UnityConfigFile" />
    <alias alias="FooOne" type="UnityConfigFile.FooOne, UnityConfigFile" />
    <alias alias="FooTwo" type="UnityConfigFile.FooTwo, UnityConfigFile" />
    <alias alias="ClassOne" type="UnityConfigFile.ClassOne, UnityConfigFile" />
    <alias alias="ClassTwo" type="UnityConfigFile.ClassTwo, UnityConfigFile" />
    <container>
      <register type="IFoo" name="1" mapTo="FooOne" />
      <register type="IFoo" name="2" mapTo="FooTwo" />
      <register type="ClassOne" mapTo="ClassOne">
        <constructor>
          <param name="foo">
            <dependency type="IFoo" name="1" />
          </param>
        </constructor>
      </register>
      <register type="ClassTwo" mapTo="ClassTwo">
        <constructor>
          <param name="foo">
            <dependency type="IFoo" name="2" />
          </param>
        </constructor>
      </register>
    </container>
  </unity>
</configuration>

That's the corresponding test that shows how it works.

UnityConfigurationSection config =
  (UnityConfigurationSection) ConfigurationManager.GetSection("unity");
IUnityContainer container = new UnityContainer();
container.LoadConfiguration(config);
ClassTwo two = container.Resolve<ClassTwo>();
Assert.IsInstanceOfType(two.Foo, typeof(FooTwo));

Update

At runtime you can do it like this

IUnityContainer container = new UnityContainer();
container.RegisterType<IFoo, FooOne>("One");
container.RegisterType<IFoo, FooTwo>("Two");
container.RegisterType<ClassOne>(new InjectionConstructor(
  new ResolvedParameter<IFoo>("One")));
container.RegisterType<ClassTwo>(new InjectionConstructor(
  new ResolvedParameter<IFoo>("Two")));
Sebastian Weber
  • 6,766
  • 2
  • 30
  • 49
5

You need to give them registration names to do this:

// Create an instance of a service you want to use. Alternatively, this
// may have been created by another process and passed to your application
LoggingService myLoggingService = new LoggingService();

// Register the existing object instance with the container
container.RegisterInstance<IMyService>("Logging", myLoggingService);

// Register a mapping for another service your application will use
container.RegisterType<IMyService, myDataService>("DataService");

// When required, retrieve an instance of these services
IMyService theDataService = container.Resolve<IMyService>("DataService");
IMyService theLoggingService = container.Resolve<IMyService>("Logging");

Taken from Resolving Instances Of Types Using Unity

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • In my application, we have default implementation of the interface and that's registered using Unity. But consumers are free to have their own implementation and register it using `app.config`. Can you tell how to handle this case? How to ensure that non-default one is used if one such type is found registered ? – Learner Dec 06 '13 at 09:32
  • Check out the answer from @SebastianWeber for details on using config files to register your services. – Fenton Dec 06 '13 at 10:08
  • Thanks for your response. But that answer is not useful to me. Because I want to ensure that the default instance registered with the Unity is "unregistered" and the one configured by the user is always provided. – Learner Dec 06 '13 at 10:24
  • I would put your registration in the config file - then if they want to change it, they'll edit it to change yours to the theirs. This would do what you want. – Fenton Dec 06 '13 at 15:05
0

I setup in my application like this

Installed Nuget Package Unity version 4.0.1

<package id="Unity" version="4.0.1" targetFramework="net452" />

In my App.config

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

<unity>
    <container>
        <register type="MyApp.MainWindow, MyApp">
            <lifetime type="singleton" />
        </register>
        <register name="UnityResolver" type="MyApp.Core.Interface.IResolver, MyApp.Core.Interface" mapTo="Avelyn.Core.Container.UnityResolver, Avelyn.Core" />
        <register name="NinjectResolver" type="MyApp.Core.Interface.IResolver, MyApp.Core.Interface" mapTo="Avelyn.Core.Container.NinjectResolver, Avelyn.Core" />
    </container>
</unity>

In My App.xaml.cs

var _container = new UnityContainer();
_container.LoadConfiguration();

IResolver _unityResolver = _container.Resolve<IResolver>("UnityResolver");
IResolver _ninject  = _container.Resolve<IResolver>("NinjectResolver");
MainWindow _win = _container.Resolve<MainWindow>();
dnxit
  • 7,118
  • 2
  • 30
  • 34