I would highly recommend against creating a dedicated / local / injected UnityContainer for each type, such as the proposed poster.
There are two ways to approach this.
One of them is the specific definition of permission during registration, as suggested by Tyler Ollsen. This is a good solution, although if you later register both ARepository and BRepository as implementations for IRepository, you still have to deal with the fact that you have two implenetations for the same interface, and if the third class is something will require the implementation of IRepository, without defining it specifically during registration, it will receive an unpredictable instance.
The second option, which is slightly safer, registers the factory for IRepository. The simplest example would be to use a string as a key, for example:
// Create a resolver(abstract factory) for the IRepository interface type var resolver = myContainer.Resolve<Func<IRepository>>(); // ... other code here... // Register mappings for the IRepository interface to appropriate concrete types myContainer.RegisterType<IRepository, ARepository>("A"); myContainer.RegisterType<IRepository, BRepository>("B");
Then, in the implementation of FooController and BarController, get the func factory by injection and select the correct instance.
public class FooController { IRepository repository; public FooController(IService service, Func<IRepository> repositoryFactory) { repository = repositoryFactory("A"); } }
You can read more about this here: http://msdn.microsoft.com/en-us/library/ff660854%28v=pandp.20%29.aspx
Joef goldstein
source share