I have clear understanding of what repository pattern is and its importance from TDD's standpoint. I also understand that how easy it can be to switch underlying data store for the application because Repository acts as a window to data access logic.
What I am NOT getting is how to support multiple data stores at a same time. Here's an example, assume that I have defined a repository IPersonRepository which has two implementations and the requirement is to read XML file and store into SQL database or vice-versa.
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);
}
}
Questions (ordered by importance):
- Does this mean I have to instantiate two PersonService objects everytime for reading data from db and xml by passing PersonRepositorySQL and PersonRepositoryXML respectively?
- For doing above, I have to add reference to repositories in upper level (mostly presentation)? How can this be avoided?
- Is DAL good place to keep repositories?
- Is it OK to name a BLL class as Service ex: PersonService?
I realize that post has become very long but I wanted to put everything that's causing confusions in mind.
-- NV