Which approach should I take in this case?

Abby

New Member
I have implemented the Data Mapper pattern with the Identity Map pattern. In short: When I want to have 2000 objects from the database, the mapper checks the result set against an hash map which contains references to already created objects. If an id is already in the hash map, the old object is added to the return array instead. Otherwise a new object is created and added to the return array. The return array will contain 2000 objects.Note: This numbers are theoretical! The platform may be highly frequented, so this may happen many times per minute or even second.Question: Which option is better and why?A) Retrieve all 2000 objects from the database. Iterate over the record set (2000 rows), and check every id against the identity map. If it's in there, add the referenced object of the identity map to the object array. If not, create a new object and add it to the result array.B) Create a (maybe HUGE) sql query which excludes all id's which are in the identity map. Get a record set that contains only data for new objects. Create new objects without checking the identity map for every row. Incorporates a lot of string concatenation operations to build the query, but may save a whole bunch of hash map lookups.Which approach would you take?(yes I know, I should just implement both versions and make a performance test, but maybe someone can answer this from practical experience)
 
Back
Top