PDA

View Full Version : RowMapper or ResultSetExtractor



EJS
02-26-2008, 09:43 AM
Hi,

Currently working with the AdoSupport and the AdoTemplate, I could not really find out when to use a RowMapper and when to use a ResultSetExtractor.
Is this choice only based on preference, or is there any difference in e.g. performance.

Further, the RowMapper and ResultSetExtractor samples show getting column values from the IDataReader by column ordinal, and not by column name. Is there a performance penalty when using the column name? This does allow for a more flexible design that is a little bit less dependent on the order specified in the select statement.

Thanks in advance,
EJ

Mark Pollack
02-26-2008, 02:12 PM
Hi EJ,

The difference is that with RowMapper, there is an assumption that you will be mapping one row to one object. If that works for your case, it would be preferable over using ResultSetExtractor as you don't have to deal with the iteration over the IDataReader. RowMapper is more 'focused' than ResultSetExtractor in that sense, it is doing more for you but assuming a 1-row to 1-object mapping from the result set. The QueryWithRowMapper methods in AdoTemplate use a RowMapper to return a list of objects.

ResultSetExtractor lets you use operate on the result set as a whole, passing in the IDataReader which you are responsible for iterating over. You can return any object based on processing the whole result set (as compared to RowMapper which you return an object per row). The returned object could be a dictionary, a list, a custom business object, etc.

There is some additional description in the ref doc (http://www.springframework.net/doc-latest/reference/html/ado.html#d0e12353).

There is no difference based on performance between RowMapper and ResultSetExtractor, so it is just based on mapping needs/preference. There is a performance penalty using the column name. In normal use, i.e. not iterating over tens of thousands of rows, I'd say it is a negligible difference. If it reads more clearly to you, then go with strings, and measure just to make sure. With tools like 'dotTrace', I've found it pretty easy to make a quick test case with NUnit just to see quickly where bottlenecks may lie.

HTH,
Mark

EJS
02-26-2008, 02:55 PM
Cool! Very clear now.
Thanks for your quick reply.

EJ