I've been playing around with this for a bit, because it seems like it feels a lot like the documented posts/users example, but its slightly different and isn't working for me.
Assuming the following simplified setup (a contact has multiple phone numbers):
public class Contact
{
public int ContactID { get; set; }
public string ContactName { get; set; }
public IEnumerable<Phone> Phones { get; set; }
}
public class Phone
{
public int PhoneId { get; set; }
public int ContactID { get; set; } // foreign key
public string Number { get; set; }
public string Type { get; set; }
public bool IsActive { get; set; }
}
I'd love to end up with something that returns a Contact with multiple Phone objects. That way, if I had 2 contacts, with 2 phones each, my SQL would return a join of those as a result set with 4 total rows. Then Dapper would pop out 2 contact objects with two phones each.
Here is the SQL in the stored procedure:
SELECT *
FROM Contacts
LEFT OUTER JOIN Phones ON Phones.ReferenceId=Contacts.ReferenceId
WHERE clientid=1
I tried this, but ended up with 4 Tuples (which is OK, but not what I was hoping for... it just means I still have to re-normalize the result):
var x = cn.Query<Contact, Phone, Tuple<Contact, Phone>>("sproc_Contacts_SelectByClient",
(co, ph) => Tuple.Create(co, ph),
splitOn: "PhoneId", param: p,
commandType: CommandType.StoredProcedure);
and when I try another method (below), I get an exception of "Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.IEnumerable`1[Phone]'."
var x = cn.Query<Contact, IEnumerable<Phone>, Contact>("sproc_Contacts_SelectByClient",
(co, ph) => { co.Phones = ph; return co; },
splitOn: "PhoneId", param: p,
commandType: CommandType.StoredProcedure);
Am I just doing something wrong? It seems just like the posts/owner example, except that I'm going from the parent to the child instead of the child to the parent.
Thanks in advance
Multi result set support
In your case it would be much better (and easier as well) to have a multi resultset query. This simply means that you should write two select statements:
This way your objects would be unique and wouldn't duplicate.
Check out https://www.tritac.com/blog/dappernet-by-example/ You could do something like this:
I got this from the dapper.net tests: https://code.google.com/p/dapper-dot-net/source/browse/Tests/Tests.cs#1343
You are doing nothing wrong, it is just not the way the API was designed. All the
Query
APIs will always return an object per database row.So, this works well on the many -> one direction, but less well for the one -> many multi-map.
There are 2 issues here:
If we introduce a built-in mapper that works with your query, we would be expected to "discard" duplicate data. (Contacts.* is duplicated in your query)
If we design it to work with a one -> many pair, we will need some sort of identity map. Which adds complexity.
Take for example this query which is efficient if you just need to pull a limited number of records, if you push this up to a million stuff get trickier, cause you need to stream and can not load everything into memory:
What you could do is extend the
GridReader
to allow for the remapping:Assuming you extend your GridReader and with a mapper:
Since this is a bit tricky and complex, with caveats. I am not leaning towards including this in core.
FYI - I got Sam's answer working by doing the following:
First, I added a class file called "Extensions.cs". I had to change the "this" keyword to "reader" in two places:
Second, I added the following method, modifying the last parameter:
I wanted to share my solution to this issue and see if anyone has any constructive feedback on the approach I've used?
I have a few requirements in the project I'm working on which I need to explain first up:
So, what I have done is to get SQL to handle the 2nd - nth Level heirarchy by returning a Single JSON string as a column on the original row as follows (stripped out the other columns / properties etc to illustrate):
Then, my POCO's are built up like the below :
Where the POCO's inherit from BaseEntity. (To illustrate I've chosen a fairly simple, single level heirarchy as shown by the "Attributes" property of the client object. )
I then have in my Data Layer the following "Data Class" which inherits from the POCO
Client
.As you can see above, whats happening is that SQL is returning a column called "AttributeJson" which is mapped to the property
AttributeJson
in the dataClient class. This has only a setter which deserialises the JSON to theAttributes
property on the inheritedClient
class. The dataClient Class isinternal
to the Data Access Layer and theClientProvider
(my data factory) returns the original Client POCO to the calling App / Library like so :Note that I'm using Dapper.Contrib and have added a new
Get<T>
Method that returns anIEnumerable<T>
There are a couple things to note with this solution:
There's an obvious performance trade off with the JSON serialisation - I've benchmarked this against 1050 rows with 2 sub
List<T>
properties, each with 2 entities in the list and it clocks in at 279ms - which is acceptable for my projects needs - this is also with ZERO optimisation on the SQL side of things so I should be able to shave a few ms there.It does mean additional SQL queries are required to build up the JSON for each required
List<T>
property, but again, this suits me as I know SQL pretty well and am not so fluent on dynamics / reflection etc.. so this way I feel like I have more control over things as I actually understand whats happening under the hood :-)There may well be a better solution than this one and if there is I would really appreciate hearing your thoughts - this is just the solution I came up with that so far fits my needs for this project (although this is experimental at the stage of posting).
Based on Sam Saffron's (and Mike Gleason's) approach, here is a solution which will allow for multiple children and multiple levels.
Then you can have it read outside of the function.
The map function can then be called again for the next child object using the same parent object. You can also implement splits on the parent or child read statements independently of the map function.
Here is a 'single to N' additional extension method