I want to use Unity resolve IService
to two different implementations, to make use of a wrapper class, the equivalent of:
IService service = new
You can use RegisterType overloads that accept string based names. In this case you'll do something like:
container.RegisterType<IService, RealService>("real");
container.RegisterType<IService, DispatcherService>("dispatcher");
and announce your dependencies with names as well.
[Dependency("real")]
This will allow you to avoid marker interface which is in most cases is not a good idea.
However if want to keep your code clean from Unity presence (like DependencyAttribute) and in most cases you will use only 1 implementation during application lifetime (for example, only DispatcherService will be used) you basically to make decision whether you need to wrap requested IService with DispatcherService or not. In this case you can look at Static Factory Extension for Unity. Factory delegate will be aware of configuration and based on configuration will either wrap IService with DispatcherService or simply return IService implementation obtained from container.
I have found out that this is trivial to do in the Castle Windsor IoC container. Just register the classes in the right order - first the wrapper, then the wrapped class, and the right thing will happen.
e.g.
container.Kernel.AddComponent<IService, DispatcherService>();
container.Kernel.AddComponent<IService, RealService>();
Not only is this a lot less fuss than in Unity, it preserves one of the key advantages of IoC - that if the parameters to the DispatcherService constructor change, no other code needs to change.
Let's hope that a future version of Unity steps up to make this scenario as simple as in Windsor.