Is Dapper just capable of returning flat classes?

dapper entity-framework

Question

I use Entity Framework and am considering Dapper as a substitute. I can do this in Entity Framework and it will fill my Team objects, the player collection property in each of them, and the team property inside of each player.

var players = context.Teams
.Include(x => x.Players)
.AsNoTracking()                                    
.Where(x => x.SportId == sportId).ToList();

However, it just appears to provide a flat player object without any team information, according to Dapper, which I discovered. Is it possible to have Dapper act in the same manner as EF, and if so, how?

var players = conn.Query<PlayerDTO, TeamDTO, PlayerDTO>("SELECT p.Id, 
p.FirstName, p.LastName, p.DateOfBirth, p.TeamId, t.Id as TeamId, t.Name, 
t.SportId FROM Team t "+ "INNER JOIN Player p ON t.Id = p.TeamId WHERE 
t.SportId = @ID", (player, team) => { return player; }, splitOn: "TeamId",
param: new { ID = sportId });
1
0
3/15/2016 11:47:06 AM

Accepted Answer

That is not presently built-in. The fact that there isn't a very decent method to do this with SQL, though, intrigues me. Since SQL data is flat overall, it shouldn't come as a surprise that dapper, which is located next to SQL, is likewise flat.

It contains the multi-map information that you've discovered. I would also be open to the idea of a more effective multi-grid reader that handled the necessary aggregating, but... it isn't a simple API to design or even implement, so it hasn't been done yet simply because it doesn't appear to be in high demand.

Asking EF what SQL it is using for that will probably reveal that it is not very effective. There are two options: very large repeated grids with a ton of duplicated data, or n+1 (x levels).

1
3/15/2016 7:12:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow