injection using named mapping

Mar 19, 2009 at 9:27 PM

First, I must warn that I'm new to Unity, and trying to see if it will work for my current requirement.

Say I have multiple concrete classes that implements the same interface, and I want to be able to inject a different type depending on which concrete class I am in. So I have an interface IFoo, and I have Foo, ServerFoo, and ClientFoo classes. I want Unity to inject an instance of ServerFoo into ClientFoo when it's resolved. Not using config file, my code to setup the container will be something like:

container = new UnityContainer();
container.RegisterType<IFoo, Foo>();
container.RegisterType<IFoo, ClientFoo>("client");
container.RegisterType<IFoo, ServerFoo>("server");
ClientFoo will look something like:

public class ClientFoo : Foo
  IFoo _serverFoo;

  //this will not work, as I can't say I want the ServerFoo:
  public ClientFoo(IFoo serverFoo) {_serverFoo= serverFoo;}
  //Property injection might work if Dependency takes a parameter like so: [Dependency("server")]. But it does not at the moment.
  public IFoo Server 
    get { return _server; } 
    set { _serverFoo = value; }

Is there actually a way to accomplish what I want?


Mar 19, 2009 at 11:17 PM
Do this:

  public IFoo Server 
    get { return _server; } 
    set { _serverFoo = value; }

DependencyAttribute DOES take a name parameter.

You can also do this through the API:

new InjectionProperty("Server", new ResolvedParameter<IFoo>("server")));

Or the equivalent in the config file.
Mar 20, 2009 at 7:32 PM
Edited Mar 20, 2009 at 7:35 PM

thanks, Chris.  If I may throw in a follow-up question... 

I realized I can't use Setter or Method injection as I don't want to tie my classes to Unity (yet).
I was able to define the named dependency in the config file like so:


<type type="IFoo" mapTo="ServerFoo" name="server" />
<type type="IFoo" mapTo="ClientFoo" name="client">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
 <param name="server" parameterType="IFoo">
   <dependency name="server" />



But how do I code this using the API? I am trying to code it based on your example which I know is for Property injection, but I can't find the equivalent for contructor. The reason I want to do it in code is because I won't know until runtime which named mapping should be injected.

Mar 22, 2009 at 12:56 AM
It's pretty simple:

container.RegisterType<IFoo, ClientFoo>("client",
    new InjectionConstructor(new ResolvedParameter<IFoo>("server")));

This says when resolving IFoo, "client", to new up a ClientFoo object, and call the constructor that takes an IFoo parameter. To get the value for that parameter, use the container and resolve IFoo, "server".

Mar 22, 2009 at 4:17 PM
thanks, for accomodating my stupid questions, Chris.  I figured out the solution moments after posting (by reading the comprehensive documentation), but Codeplex was down for a good part of friday afternoon. Lesson learned: read the doc =)