我沒有看到建在溫莎做一些這樣的事情。我雖然關於一個HandlerSelector,但是當選擇器正在運行時,你不知道請求依賴關係的類型,所以這沒有幫助。您可以使用TypedFactoryFacility。只需創建一個接口來回報您的IService對象(C#版本):
public interface IProviderFactory
{
IService GetServiceOne();
IService GetServiceTwo();
IService GetServiceThree();
}
然後命名你的IService實例作爲 「ServiceOne」, 「ServiceTwo」,等等
Component.For<IService>().ImplementedBy<ServiceOne>().Named("ServiceOne"),
Component.For<IService>().ImplementedBy<ServiceTwo>().Named("ServiceTwo"),
Component.For<IService>().ImplementedBy<ServiceThree>().Named("ServiceThree"),
然後讓你的供應商取決於工廠並調用相應的「Get」方法:
public class ServiceOneProvider
{
public ServiceOneProvider(IProviderFactory factory)
{
var service = factory.GetServiceOne();
}
}
public class ServiceTwoProvider
{
public ServiceTwoProvider(IProviderFactory factory)
{
var service = factory.GetServiceTwo();
}
}
public class ServiceThreeProvider
{
public ServiceThreeProvider(IProviderFactory factory)
{
var service = factory.GetServiceThree();
}
}