这是我使用的解决方案的一个版本。我通过继承层次结构而非将属性设置为null来回避Ronnie在
他的答案中提出的问题,但本质上是相同的。
以下是SQL语句:用户拥有物品和收藏夹,物品可以在收藏夹中。
CREATE TABLE Users
(id UNIQUEIDENTIFIER DEFAULT (NEWID()) NOT NULL,
name NVARCHAR (MAX) NULL,
email NVARCHAR (128) NULL,
PRIMARY KEY (id))
CREATE TABLE Items
(id UNIQUEIDENTIFIER DEFAULT (NEWID()) NOT NULL,
userId UNIQUEIDENTIFIER NOT NULL,
name NVARCHAR (MAX) NULL,
description NVARCHAR (MAX) NULL,
PRIMARY KEY (id),
FOREIGN KEY (userId) REFERENCES Users (id))
CREATE TABLE Collections
(id UNIQUEIDENTIFIER DEFAULT (NEWID()) NOT NULL,
userId UNIQUEIDENTIFIER NOT NULL,
name NVARCHAR (MAX) NULL,
layoutSettings NVARCHAR (MAX) NULL,
PRIMARY KEY (id),
FOREIGN KEY (userId) REFERENCES Users (id))
CREATE TABLE CollectedItems
(itemId UNIQUEIDENTIFIER NOT NULL,
collectionId UNIQUEIDENTIFIER NOT NULL,
PRIMARY KEY CLUSTERED (itemId, collectionId),
FOREIGN KEY (itemId) REFERENCES Items (id),
FOREIGN KEY (collectionId) REFERENCES Collections (id))
现在是数据模型类。为了处理Dapper多查询的多重映射,集合比我预期的要复杂一些。
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public List<Item> Items { get; set; }
public List<Collection> Collections { get; set; }
}
public class Item
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class CoreCollection
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Name { get; set; }
public string LayoutSettings { get; set; }
}
public class PartialDataCollection : CoreCollection
{
public Guid ItemId { get; set; }
}
public class Collection : CoreCollection
{
public List<Guid> ItemIds { get; set; }
}
public class CollectedItem
{
public Guid ItemId { get; set; }
public Guid CollectionId { get; set; }
public DateTime CreatedAt { get; set; }
}
最后,我们有一个控制器方法,使用Dapper多映射和多个查询。
[Route("GetUser/{id}")]
public User GetUser(Guid id)
{
var sql = @"SELECT * FROM Users WHERE id = @id
SELECT * FROM Items WHERE userId = @id
SELECT * FROM Collections
LEFT OUTER JOIN CollectedItems ON Collections.id = CollectedItems.collectionId
WHERE userId = @id";
using (var connection = new SqlConnection(ConnectionString))
{
var multi = connection.QueryMultiple(sql, new { id = id });
var user = multi.Read<User>().Single();
var items = multi.Read<Item>().ToList();
var partialDataCollections = multi.Read<PartialDataCollection, CollectedItem, PartialDataCollection>(AddCollectedItem, splitOn: "itemId").ToList();
user.Items = items;
user.Collections = partialDataCollections.GroupBy(
pdc => pdc.Id,
(key, group) => new Collection
{
Id = key,
UserId = group.First().UserId,
Name = group.First().Name,
LayoutSettings = group.First().LayoutSettings,
ItemIds = group.Select(groupMember => groupMember.ItemId).ToList()
}).ToList();
return user;
}
}
private PartialDataCollection AddCollectedItem(PartialDataCollection collection, CollectedItem collectedItem)
{
if (collection != null && collectedItem != null)
{
collection.ItemId = collectedItem.ItemId;
}
return collection;
}
Ronnie担心在他的回答中设置person.Check = null
,而我则担心将类PartialDataCollection
添加到我的模型会增加额外的复杂性。但是我看不到简单的解决方法。
(注:我已经在Dapper GitHub项目上提出了这个问题。)