我有一個類,看起來像這樣:工廠模式在依賴注入
public class SomeRepo : ISomeRepo
{
private IThingFactory _thingFactory;
public class SomeRepo (IThingFactory thingFactory)
{
_thingFactory = thingFactory;
}
public IThing GetThingFromDatabase(int id)
{
string thingName = /* some call that gets back some primitives */
IThing returnVal = _thingFactory.createThing(thingName);
return returnVal;
}
}
因此,在短期,SomeRepo
是一個回購協議,負責一些數據存儲通信得到一個IThing
的標識。而IThingFactory
是一個簡單的工廠,返回給定字符串屬性的new IThing
。
如果我使用依賴注入容器,我還應該依靠IThingFactory
嗎?爲了方便起見,我似乎混合了兩種設計模式。有沒有更好的方法來構建IThing
而不需要工廠,或者我有一個好的模式可以遵循嗎?
謝謝
編輯:我使用的DI容器是Ninject。
你能形容'IThing'嗎?有多少類實現它?這是一個簡單的財產包?它是否包含您可能想要改變的行爲? –
'IThing'僅由1類實現,但可以通過在未來其他類來實現。這是一個單一的財產包。 – Rhs
看看[這篇文章newables和注射劑(http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/)。 –