我有一个类,看起来像这样:工厂模式在依赖注入
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/)。 –