是否有可能在具有2個構造函數的類中使用依賴注入?依賴注入與2構造函數
例如,假設我有一個界面:
public interface ILogger
{
void Log(LogLevel level, string message, string CallerID);
}
和它的實現:
internal class NLogger : ILogger
{
private static NLog.Logger _logger;
public NLogger()
{
_logger = LogManager.GetCurrentClassLogger();
}
public NLogger(ISomeService srv): this()
{
srv.doSomthing();
}
public void Log(LogLevel level, string message, string CallerID)
{
//implementation
}
}
我要注入ISomeService
以防萬一它存在於我的DI容器,並在情況下,它不存在 - 使用空構造函數並繼續工作,但不使用ISomeService
。
可能嗎?如果沒有,你有建議如何實現類似的東西?
擁有多個構造函數[是反模式](http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=97)。請不要這樣做。 – Steven