Reputation:
In Dapper, what's the best way to map and object that contains a list and each of those list items have their own lists?
class Object1 {
int Object1Id;
List<Object2> object2s;
}
class Object2 {
int Object2Id;
List<Object3> object3s;
}
class Object3 {
int Object3Id;
}
Here's the SQL I wanted to use
SELECT *
FROM [Object1] o1
left join [Object2] o2 on o1.Object1Id = o2.Object1Id
left join [Object3] o3 on o2.Object2Id = o3.Object2Id
Using the Left joins because it's possible Object1 has no Object2's and Object2's may not have any Object3's
I thought about using
connection.Query<Object1, Object2, Object3, Object1>
but keeping track of a dictionary within a dictionary to add them to the list seems like a lot of lines of code.
Would I be better off using QueryMultiple?
Upvotes: 2
Views: 624
Reputation: 1579
If you're using SQL Server 2016 or above I would go with JSON. You can return the data as a JSON document, like this:
USE tempdb
GO
DROP TABLE IF EXISTS dbo.[Object1]
DROP TABLE IF EXISTS dbo.[Object2]
DROP TABLE IF EXISTS dbo.[Object3]
GO
CREATE TABLE dbo.[Object1] ([Object1Id] INT PRIMARY KEY, [Value1] NVARCHAR(100));
CREATE TABLE dbo.[Object2] ([Object2Id] INT PRIMARY KEY, [Object1Id] INT, [Value2] NVARCHAR(100));
CREATE TABLE dbo.[Object3] ([Object3Id] INT PRIMARY KEY, [Object2Id] INT, [Value3] NVARCHAR(100));
GO
INSERT INTO dbo.[Object1] VALUES (1, 'Object 1 Value 1')
INSERT INTO dbo.[Object1] VALUES (2, 'Object 1 Value 2')
INSERT INTO dbo.[Object1] VALUES (3, 'Object 1 Value 3')
GO
INSERT INTO dbo.[Object2] VALUES (10, 1, 'Object 2 Value 1')
INSERT INTO dbo.[Object2] VALUES (20, 1, 'Object 2 Value 2')
INSERT INTO dbo.[Object2] VALUES (30, 2, 'Object 2 Value 3')
GO
INSERT INTO dbo.[Object3] VALUES (100, 10, 'Object 3 Value 1')
INSERT INTO dbo.[Object3] VALUES (200, 10, 'Object 3 Value 2')
INSERT INTO dbo.[Object3] VALUES (300, 30, 'Object 3 Value 3')
GO
SELECT
*
FROM
[Object1]
LEFT JOIN
[Object2] AS Object2s ON [Object1].Object1Id = [Object2s].Object1Id
LEFT JOIN
[Object3] AS Object3s ON [Object2s].Object2Id = [Object3s].Object2Id
WHERE
[Object1].Object1Id = 1
FOR
JSON AUTO,WITHOUT_ARRAY_WRAPPER
With a JSON as result it is easy to then deserialize it in a class you want:
class Program
{
class Object1 {
public int Object1Id;
public List<Object2> Object2s;
}
class Object2 {
public int Object2Id;
public List<Object3> Object3s;
}
class Object3 {
public int Object3Id;
}
static void Main(string[] args)
{
using(var conn = new SqlConnection("Data Source=localhost; Initial Catalog=tempdb; Integrated Security=SSPI"))
{
var json = conn.ExecuteScalar<string>(@"
SELECT
*
FROM
[Object1]
LEFT JOIN
[Object2] AS Object2s ON [Object1].Object1Id = [Object2s].Object1Id
LEFT JOIN
[Object3] AS Object3s ON [Object2s].Object2Id = [Object3s].Object2Id
WHERE
[Object1].Object1Id = 1
FOR
JSON AUTO,WITHOUT_ARRAY_WRAPPER
");
var result = JsonConvert.DeserializeObject<Object1>(json.Replace("\"", "'"));
Console.WriteLine(result.Object2s.Count);
}
}
}
Performances are also very good.
Upvotes: 1
Reputation: 1064114
You're absolutely right : this kind of parent-child handling isn't a scenario that dapper handles very well right now. It would be nice if it did, but it simply hasn't been the top priority to do something about it.
You have a choice of two inelegant solutions. Personally I think the QueryMultiple
approach is proabbly cleaner.
Upvotes: 0