Question

In my Windows Phone 8 app I have multiple services which implements the same interface. Based on user selection I need to use one of these services (implementations).

Usually I configure my services in bootstrapper, like this:

 protected override void Configure()
{
    ...
    container.PerRequest<ICarService, CompanyACarService>();
    //container.PerRequest<ICarService, CompanyBCarService>();
    ...
}

How should I add functionality to bootstrapper, so that It chooses CompanyACarService or CompanyBCarService implementation based on some logic?

Was it helpful?

Solution

You can use

container.RegisterPerRequest(interface, key, implementation);

to register multiple implementations of an interface with different keys (identifiers). Then you can use the service locator (anti)pattern to get an instance

var instance = IoC.Get<interface>(key);

I do not think you can achieve the same result using constructor injection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top