所以我有这个应用程序,即使用IOC(autofac)。同时,我发现自己处于需要工厂的位置。在工厂内部,我创建了具有依赖性的新对象 - 所以现在我想知道,我怎么能结婚呢?工厂和国际石油公司一起 - 如何使用两者?
public class SubscriptionHandlerFactory : ISubscriptionHandlerFactory
{
public ISubscriptionHandler GetProvider(StreamType streamType)
{
switch (streamType)
{
case StreamType.Rss:
return new RssSubscriptionHandler(null,null,null,null);
case StreamType.Person:
return new PersonSubscriptionHandler(null, null, null, null);
default:
throw new ArgumentOutOfRangeException(nameof(streamType), streamType, null);
}
}
}
注入的IoC容器进工厂,然后使用暴露的方法来获取所需的具体类型的实例。然后容器将为您解析其他依赖关系。 –
@MartinCostello这是一个服务定位器,很大程度上被认为是反模式,即使在工厂内完成。 –
你确定你需要一个工厂吗?为什么你确定你不能让IoC容器为你构建对象依赖关系图? –