我从TDD的角度清楚了解存储库模式及其重要性。我也明白,为应用程序切换底层数据存储是多么容易,因为存储库充当数据访问逻辑的窗口。如何在存储库模式中为相同接口使用两个数据存储?
我没有得到的是如何同时支持多个数据存储。下面是一个例子,假设我已经定义了一个存储库IPersonRepository,它有两个实现,并且需要读取XML文件并存储到SQL数据库中,反之亦然。
DataAccessLayer
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
}
}
BusinessLogicLayer
public PersonService
{
private IPersonRepository personRepository;
public PersonService(IPersonRepository personRepository)
{
this.personRepository = personRepository;
}
public Person GetPersonById(int id)
{
personRepository.GetPersonById(id);
}
}
问题(按重要性排序):
- 这是否意味着我必须实例2 PersonService对象每次通过分别传递PersonRepositorySQL和PersonRepositoryXML来从db和xml读取数据?
- 为了达到上述目的,我必须添加对上层存储库(主要是演示文稿)的引用?这怎么可以避免?
- DAL是否是保存存储库的好地方?
- 可以将BLL类命名为Service ex:PersonService?
我意识到帖子变得很长,但我想把所有引起混淆的东西都记在脑后。
- NV