Problem 2 - DAO Pattern, Leaky Abstraction
There is a problem with the DAO Pattern that AutoFetch solves so let's look at an example.
customerDao.findById(Integer id); A DAO Developer builds a customer find by id method and there are
2 developers that want to use this method.
Developer 1 really just needs the customers name and status.
Developer 2 wants all the properties on the customer and the customer's shipping and billing address.
In running the application it is decided there is a performance problem. The query executed is not
suited for use in either of the two cases. The DAO Developer wants to tune the query to perform better
and so he decides to provide 2 methods tuned for use by each of the developers.
public Customer findByIdWithName(Integer id){
Query<Customer> query = Ebean.createQuery(Customer.class);
query.setId(id);
query.select("id,name");
return query.findUnique();
}
public Customer findByIdWithAddresses(Integer id){
Query<Customer> query = Ebean.createQuery(Customer.class);
query.setId(id);
query.join("shippingAddress");
query.join("billingAddress");
return query.findUnique();
}
Now what happens if there are other use cases that use other completely different parts of a Customer?
Should we continue adding a method for each situation? Leaky abstraction perhaps? Is the implementation
detail of the customerDao leaking back up into the application code?
This issue becomes more of a problem as the application gets larger and performance more critical.
Problem 2 - solution
With AutoFetch a single query can be tuned different according to the call stack that was used to call
the query. For example it can tune the query for both use cases differently giving optimal query performance
for both developer 1 and developer 2.
NEXT: Using AutoFetch with Ebean
|