Hi,
What I'm trying to do is make a DAL which can retrieve multiple result sets whilst only opening a database connection once.
Usually, a DAL will have methods like 'GetProducts' and 'GetProductCategories' and so if you want both sets for a single web page then you have to call both methods and it'll go to the database once for each.
So my idea is to have generic retrieval code which is passed objects which describe the required queries. So one would end up with code like the following...
DAL dal1 = new DAL();
EntityCollection collection1, collection2;
QueryDescriptor qd1 = Queries.GetProducts();
QueryDescriptor qd2 = Queries.GetProductCategories();
dal1.AddQuery(qd1, ref collection1);
dal1.AddQuery(qd2, ref collection2);
dal1.Retrieve();
So a 'QueryDescriptor' has properties for 'RelationPredicateBucket' and 'SortClause' etc. and a function like 'Queries.GetProducts()' would prepare one of these objects with the required state.
Now I think this is quite cool except I want to be able to cache query results. But if a result is in the cache then there's no reason to prepare the 'QueryDescriptor' instance's 'RelationPredicateBucket' and other things because there's no need to go to the database.
So what I'm thinking of doing is sub-classing 'QueryDescriptor' for each and every query such that methods on the derived class create the 'RelationPredicateBucket' and 'SortClause' and so forth but only if the DAL doesn't find the result set in the cache. So the above code would look like this....
DAL dal1 = new DAL();
EntityCollection collection1, collection2;
dal1.AddQuery(new GetProducts(), ref collection1);
dal1.AddQuery(new GetProductCategories(), ref collection2);
dal1.Retrieve();
Another method on a class derived from 'QueryDescriptor' would return the cache key. The DAL reads this and does a cache check and then only if the result isn't in the cache does it call the other derived class methods and so create the objects for generating the SQL.
So the question is, from an OO point of view, is there something dodgy about having one class for every query that I need to do?
Also, is there a better way of identifying a query in order to check if its result is in the cache without actually creating the query description first?
Cheers,
Ian.