我們有一個外部項目,它具有一個QCServiceLog類,具有由Unity解析的ILogging依賴關係。 但QCServiceLog是一個Singleton類,你可以在下面的例子中看到:具有依賴注入的單例類c#
private readonly ILogging _logging = null;
private static QCServiceLog _instance = null;
public static QCServiceLog Instance
{
get
{
return _instance;
}
}
public QCServiceLog(ILogging logging)
{
_logging = logging;
if (_instance == null)
{
_instance = this;
}
}
我們正在嘗試使用它,並在我們的解決方案,我們做了登記,如:
uc.RegisterType<ILogging, QCFileManager>(new ContainerControlledLifetimeManager());
但由於QCServiceLog是一個Singleton,我們相信代碼永遠不會通過構造函數,然後_instance永遠不會被實例化。 我們正在使用它這樣做:
QCServiceLog.Instance.Log(ex);
那是辛格爾頓正確實施?我們認爲它從未做過QCServiceLog的新的。
您認爲如何?在沒有改變外部項目的情況下,我們可以做什麼? 您可以想象的例外是:
未將對象引用設置爲對象的實例。
我真的很感謝你的幫助!