There is a controversial discussion of Repositories, their use and layout on stackoverflow and throughout the web. I am confused about how to implement the data access abstraction (e.g. database) behind a Repository efficiently.
I am not using an ORM tool/framework as I want to see the gritty details myself. At the moment I am using DAO objects to access a (mysql) database and provide Business Objects (domain objects). Associations given by foreign keys in the database tables are resolved and loaded within the DAO of the respective object (no lazy loading currently). As I don't want to use my database DAOs directly in the business logic I considered a Repository to be a good further abstraction. I got stuck when implementing sophisticated queries like GetEmployeesByShopAndPosition() in the Repository: I see two possibilities to implement this:
- Brute Force: Use the Employee DAO and load all employees as business objects (including associated shops/positions) from the database into the Repository's collection. Iterate through collection and return those Employees working in the given Shop and Position.
- Efficient: Implement a database query that joins the concerned tables and returns only wanted Employees by where clause in the EmployeeDAO.
The first approach uses the collection nature a Repository should actually have, but seems to be very inefficient. The second approach generates a bloated DAO but is much more efficient.
My questions:
- What is to prefer here or how is it done in practice?
- Am I wrong and the Repository should not be used in conjunction with DAOs and the database related code can go directly in the Repository?
- As a Repository deals with aggregates, should it actually assemble the associated foreign keys to build (full) Business Objects instead of the DAOs I am currently using?
I know this topic is not black/white as the involved design patterns can also be implemented in different ways, but I guess there are some guidelines that should not be broken or confused to get seperation of concerns and Persistence Ignorance (PI).