我正在研究一個遺留代碼庫,嚴重需要重構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