This is a very late answer, but the question still appears on Google.
One way or another, 5 years later ...
I have a pretty simple approach. Usually, when you need to use a "named dependency" because you are trying to implement some kind of strategy template. In this case, I simply create a level of indirection between Unity and the rest of my code called StrategyResolver so as not to depend directly on Unity.
public class StrategyResolver : IStrategyResolver { private IUnityContainer container; public StrategyResolver(IUnityContainer unityContainer) { this.container = unityContainer; } public T Resolve<T>(string namedStrategy) { return this.container.Resolve<T>(namedStrategy); } }
Using:
public class SomeClass: ISomeInterface { private IStrategyResolver strategyResolver; public SomeClass(IStrategyResolver stratResolver) { this.strategyResolver = stratResolver; } public void Process(SomeDto dto) { IActionHandler actionHanlder = this.strategyResolver.Resolve<IActionHandler>(dto.SomeProperty); actionHanlder.Handle(dto); } }
Registration:
container.RegisterType<IActionHandler, ActionOne>("One"); container.RegisterType<IActionHandler, ActionTwo>("Two"); container.RegisterType<IStrategyResolver, StrategyResolver>(); container.RegisterType<ISomeInterface, SomeClass>();
Now the good thing is that I will never have to touch StrategyResolver again when adding new strategies in the future.
It is very simple. Very clean and I kept dependency on Unity to a strict minimum. The only time I would touch StrategyResolver is if I decide to change the container technology, which is unlikely to happen.
Hope this helps!
Edit: I don't really like the accepted answer, because when you use the Dependency attribute in the service designer, you are actually very dependent on Unity. The Dependency attribute is part of the Unity library. At this point, you can also pass the IUnityContainer dependency everywhere.
I prefer that my service classes depend on objects that I fully own, rather than having a tight dependency on an external library everywhere. Also, the use of the Dependency attribute makes constructor signatures less clear and simple.
In addition, this method allows you to resolve named dependencies at run time without having to hard-code the named dependencies in the constructor, in the application configuration file, or use the InjectionParameter which are all methods that require you to know which named dependency to use at design time.
Editing (2016-09-19): for those who may wonder, the container will know that it will skip itself when you request the IUnityContainer as a dependency, as shown in the signature of the StrategyResolver constructor.
Edit (2018-10-20): here is another way, just using the factory:
public class SomeStrategyFactory : ISomeStrategyFactory { private IStrategy _stratA; private IStrategy _stratB; public SomeFactory(IStrategyA stratA, IStrategyB stratB) { _stratA = stratA; _stratB = stratB; } public IStrategy GetStrategy(string namedStrategy){ if (namedStrategy == "A") return _stratA; if (namedStrategy == "B") return _stratB; } } public interface IStrategy { void Execute(); } public interface IStrategyA : IStrategy {} public interface IStrategyB : IStrategy {} public class StrategyA : IStrategyA { public void Execute(){} } public class StrategyB : IStrategyB { public void Execute() {} }
Using:
public class SomeClass : ISomeClass { public SomeClass(ISomeStrategyFactory strategyFactory){ IStrategy strat = strategyFactory.GetStrategy("HelloStrategy"); strat.Execute(); } }
Registration:
container.RegisterType<ISomeStrategyFactory, SomeStrategyFactory>(); container.RegisterType<IStrategyA, StrategyA>(); container.RegisterType<IStrategyB, StrategyB>(); container.RegisterType<ISomeClass, SomeClass>();
This second sentence is the same, but using a factory design template.
Hope this helps!