I am currently stuck on what I consider to be a simple architecture issue.
I have a controller (these are just examples, since I cannot share my real code, but the principle matters):
public class StackOverflowController : Controller { private readonly IStackOverflowService stackOverflowService; public StackOverflowService (IStackOverflowService stackOverflowService) { this.stackOverflowService = stackOverflowService; }
I am using Bootstrapper Unity MVC4 for injection management:
public static class Bootstrapper { public static IUnityContainer Initialise() { var container = BuildUnityContainer(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); return container; } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType<IStackOverflowService, StackOverflowService>("DefaultStackOverflowService"); return container; } }
So far so good, the injections work. Where I'm stuck, itβs now easy to use either a configuration or some kind of variable, I want to change the implementation of StackOverflowService .
So, for example, if url = "somethingelse.com", I want to be able to automatically enter a CustomStackOverflowService that inherits from IStackOverflowService. But here I am lost. I tried using Named Registration, but I cannot figure out how to manually allow the correct service to be implemented based on some alternative criteria.
If someone can help, it will be epic :)
EDIT: The criteria for changing the implementation should ideally be calculated in real time, so tell me based on the actual URL that is currently being visited. This is a multidirectional environment, so it can be something like domain.com/stackoverflow vs domain2.com/stackoverflow. The implementation should be different for each site. I donβt even know if you can be honest or how to do it. I am new to the entire subject of IoC.
EDIT 2: I got a little more with this and managed to manually invoke a specific implementation. Now I am considering using the Factory user controller to decide which implementation to use, but I'm not sure if this is the right way, or if there is an easier way to do this.
I need to keep in mind that as more customers enter the market, I need to be able to quickly add additional implementations for each new client. Therefore, ideally, I will have an easy way to control methods for a specific client.
EDIT 3: Further update, I rewrote my controller to allow the constructor to be injected to determine the dependency. However, my original problem is still relevant, since it works when the controller is built, I do not have access to the request and therefore cannot determine the URL to resolve the dependency. Any ideas would be appreciated.