【发布时间】:2014-03-12 19:04:22
【问题描述】:
从 TDD 的角度来看,我清楚地了解什么是存储库模式及其重要性。我也明白,为应用程序切换底层数据存储是多么容易,因为存储库充当数据访问逻辑的窗口。
我没有得到的是如何同时支持多个数据存储。这是一个示例,假设我已经定义了一个存储库 IPersonRepository,它有两个实现,并且要求是读取 XML 文件并存储到 SQL 数据库中,反之亦然。
数据访问层
public interface IPersonRepository
{
Person GetPersonById(int id);
}
public class PersonRepositorySQL : IPersonRepository
{
public Person GetPersonById(int id)
{
// get person from sql db (using ado.net)
}
}
public class PersonRepositoryXML : IPersonRepository
{
public Person GetPersonById(int id)
{
// get person from xml file
}
}
业务逻辑层
public PersonService
{
private IPersonRepository personRepository;
public PersonService(IPersonRepository personRepository)
{
this.personRepository = personRepository;
}
public Person GetPersonById(int id)
{
personRepository.GetPersonById(id);
}
}
问题(按重要性排序):
- 这是否意味着我每次都必须通过分别传递 PersonRepositorySQL 和 PersonRepositoryXML 从 db 和 xml 读取数据来实例化两个 PersonService 对象?
- 为了执行上述操作,我必须在上层(主要是演示文稿)中添加对存储库的引用?如何避免这种情况?
- DAL 是保存存储库的好地方吗?
- 是否可以将 BLL 类命名为服务,例如:PersonService?
我意识到这篇文章已经变得很长,但我想把所有引起混淆的地方都记在心里。
-- NV
【问题讨论】:
标签: repository-pattern data-access-layer business-logic-layer