【发布时间】:2011-01-05 11:59:47
【问题描述】:
我一直在使用 c# 和 .net 使用存储库模式。
我的问题是关于创建someRepositoryObject 的新实例。到目前为止我看到的所有例子都使用这样的东西:
using(ISomeRepository someRepository = new SomeRepository().getRepository())
{
IsomeRepositoryObject repObj = new someRepositoryObject();
}
调用new someRepositoryObject 不是首先消除了使用接口的意义吗?这样做会更好:
using(ISomeRepository someRepository = new SomeRepository().getRepository())
{
IsomeRepositoryObject repObj = someRepository.NewsomeRepositoryObject();
}
所以存储库本身返回了所需对象的新实例,调用代码不知道传递给它的类,只是知道它的类型为ISomeRepositoryObject。
这对我来说是全新的,所以我可能会遗漏一些明显的东西!
任何帮助将不胜感激。
【问题讨论】:
-
只是出于好奇,为什么
new SomeRepository().getRepository()而不仅仅是new SomeRepository()或类似RepositoryFactory.getSomeRepository()甚至SomeRepository.getRepository()? -
想一想,没有理由!我正在使用 getRepository 来拉入 web.config appsetting,然后用于返回正确的存储库类型。这段代码可以添加到 SomeRepository 的构造函数中。
-
其实看代码,我是用 .getSomeRepository() 返回一个 IsomeRepository 对象。这是我在圣诞假期前一直在玩的东西——所以我再想一想。
标签: c# repository design-patterns