Retrieve parent and child objects with minimum DB hits using Dapper

dapper sql-server-2005

Question

Object hierarchy may be like Customers and Orders, and I want all customers with orders as List property. Typically, in ado.net, we loop through each customer and load their orders which might result it N number of DB hits with N customers. So, is there are any way to achieve this directly in Dapper, if not, is there better way to achieve this.

Edit

I've idea of retrieving all records at once using join but customers get duplicated. And again, I need to construct the required object from resultant DTO. But I don't know if it is best possible way or there may be any other approach which I might be missing esp. w.r.t Dapper.

Popular Answer

You can write a query joining customer and order tables. That will reduce the no of calls to DB but it will increase the amount of duplicate data you fetch from db. But that depends on the number of columns that you fetch from customer table.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why