I ask this because it seems people usually consider the code that goes into a DAO or Repository implementation as "data access code", while the code that directly uses these DAOs/Repositories as "business logic code".
However, this doesn't seem to make logical sense.
Consider a business service method with code like the following (using Java, but it would be similar in C#, etc.):
public BigDecimal computeCustomerTotal(Customer customer) {
List<Order> orders = orderRepository.findByCustomer(customer);
BigDecimal total = orders.stream()
.filter(Order::isActive).map(Order::getTotal).reduce(ZERO, BigDecimal::add);
return total;
}
Sure, the above implementation doesn't make "proper" use of a database's capabilities, but bear with me. The point here is that, I assume, everyone agrees it contains only business logic code, with Customer
and Order
being domain entities (ie, they are not "data" objects).
So, what if I want to make it right, by moving the whole computation to the database, instead of executing in the client program? If I follow "conventional wisdom", I would create a new method in the repository:
public BigDecimal getCustomerTotal(Customer customer) {
BigDecimal total = performQuery(
"select sum(o.total) from Order o where o.customer = ?1 and o.active",
customer);
return total;
}
(Assume performQuery
is a JPA-based utility method which takes a JPA-QL
sentence with optional arguments.)
The second implementation is obviously better than the first, as it probably performs better, and makes proper use of the underlying technologies (JPA/Hibernate, and a relational database).
The problem, though, is that business logic (the rule that only active orders should be considered) has been moved from a business service method to a (supposed) data access method, through a simple implementation change. Also, this code which is now in the DAO/Repository does not actually know about data access concerns, such as the mapping from entity types and attributes to tables and columns, or about the actual SQL code that gets generated and sent to the DB engine.
Wouldn't it be more logical to regard both methods as containing only business logic code, and just get rid of the DAO/Repository/DAL altogether?
Personally, I don't see any data access code there, and to me things like DAOs, Repositories, or "data access layers" (DAL) are in reality anti-patterns. Note that even when using JDBC + SQL in a DAO/Repository implementation, it will inevitably contain business logic, typically in the form of "where" clauses.