Examples on creating Result Set Mappers

Topics: Data Access Application Block
Oct 7, 2010 at 4:07 AM


Does anyone know where I can find some examples on creating result set mappers for EL5?  The documentation has some examples on row mappers but none for result set mappers.



Best regards,


Oct 7, 2010 at 4:48 AM
Edited Oct 7, 2010 at 4:57 AM

A simple example:

public class ProductResultMapper : IResultSetMapper<Product>
        public IEnumerable<Product> MapSet(IDataReader reader)
            List<Product> products = new List<Product>();

            while (reader.Read())
                Product prod = new Product();
                prod.ID = int.Parse(reader["ProdID"].ToString());
                prod.ProdName = reader["ProdName"].ToString();
                prod.Price = double.Parse(reader["Price"].ToString());


            return products;

Hope this helps.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Oct 7, 2010 at 1:51 PM

Hi Sarah,

What about mapping a Customer object with Orders (collection of orders) property?  I thought this was what a Result Set mapper was meant for, i.e. more complex objects.



Best regards,


Oct 7, 2010 at 4:07 PM

Let me ask you this question: given a DataReader, how would you write the mapping code to build up a Customer with Orders collection? That's the code you need to write in the result set mapper. There's nothing magical or special about it. It also depends completely on the nature of the query you're writing and the order of things you get back from the database.

Typically, in your case I'd have two result sets returned in the reader. The first would have a single row, which would be the Customer information. I'd stick that into a Customer object, then advance to the next result set, which would contain the order information. At that point I'd start collecting Order objects, adding each one to the Customer. Then at the end return that Customer object.

That's just one way to do it, but that's the general idea.


Oct 8, 2010 at 4:20 AM

Hi ctavares,

Thanks for the clarification.  I'll give it a try.


Best regards,