我正在研究一个遗留代码库,严重需要重构SOLID原则。作为第一步,我们将Simple Injector依赖注入容器添加到混合中。简单的注射器有办法注册一个实例工厂和TypeFactoryContext的组合吗?
因为我需要的东西非常像log4net的的RegisterConditional例如注册的一个,即:
container.RegisterConditional(
typeof(ILogger),
c => typeof(Logger<>).MakeGenericType(c.Consumer.ImplementationType),
Lifestyle.Singleton,
c => true);
不同的是,我要注入由工厂方法,它采用了c.Consumer返回一个实例.ImplementationType作为输入:
container.Register???(
typeof(ILogger),
c => LoggerProvider.GetLogger(c.Consumer.ImplementationType),
Lifestyle.Transient);
这可以在简单的喷油器中完成吗?
我已阅读a similar question的答案,但这还不够。 ImplementationType需要交给实例工厂。
暂时,我将实施记录仪<TParent>和使用,在RegisterConditional电话:
public class Logger<TParent> : ILogger
{
private readonly ILogger _decoratedLogger;
public Logger()
{
_decoratedLogger = Logger.GetLogger(typeof(TParent));
}
}
“我们将第一步添加Simple Injector依赖注入容器。”您可能想稍后开始使用DI容器。请参阅:https://stackoverflow.com/a/32033648/264697 – Steven
好点。我们确实考虑过为Pure DI提供短暂的时间。但是,代码库用于多个应用程序。每个都需要一个会快速损害可维护性的组合根。 – AroglDarthu