JSON补丁和"聚合"的DTO

时间:2017-08-24 14:42:36

标签: c# asp.net-core json-patch

一个有点人为但却很重要的例子。

假设以下情况,UserDetails是一个聚合DTO(不确定正确的术语,请教育我,但基本上是来自不同商店/服务的收集信息的模型),它由RESTful Web服务使用。它不一定具有与它们一起收集的对象相同的属性名称。

public class UserDetails
{
    public int UserId { get;set; }
    public string GivenName { get; set; }
    public string Surname { get; set; }
    public int? UserGroupId { get;set; } // FK in a different database
}

让我们的商店坚持以下模式:

public class User
{
    public int Id { get; set; }
    public string GivenName { get; set; }
    public string Surname { get; set; }
}


public class UserGroup
{
    public int UserId { get; set; }
    public int GroupId { get; set; }
}

让UserDetails对象如此填充:

User user = _userService.GetUser(userId) ?? throw new Exception();
UserGroup userGroup = _userGroupService.GetUserGroup(user.Id);

UserDetails userDetails = new UserDetails {
    UserId = user.Id,
    GivenName = user.GivenName,
    Surname = user.Surname,
    UserGroupId = userGroup?.GroupId
};

也就是说,设置FirstNameSurname应该委托给UserService,将UserGroupId委托给GroupService

这个UserDetails对象用于GET和PUT,这里的逻辑非常简单,但是为PATCH请求发送了该对象的JSON补丁文档。这显然要复杂得多。

我们如何改变用户群?最好的(最好的'使用得非常松散)我想出来的是:

int userId;
JsonPatchDocument<UserDetails> patch;

// This likely works fine, because the properties in `UserDetails`
// are named the same as those in `User`
IEnumerable<string> userPaths = new List<string> {"/givenName", "/surname"};
if (patch.Operations.Any(x => userPaths.Contains(x.path))) {
    User user = _userService.GetUserByUserId(userId);
    patch.ApplyTo(user);
    _userService.SetUser(userId, user);
}

// Do specialised stuff for UserGroup
// Can't do ApplyTo() because `UserDetails.UserGroupId` is not named the same as `UserGroup.GroupId`
IEnumerable<Operation<UserDetails>> groupOps = patch.Operations.Where(x => x.path == "/userGroupId");
foreach (Operation<UserDetails> op in groupOps)
{
    switch (op.OperationType)
    {
        case OperationType.Add:
        case OperationType.Replace:
            _groupService.SetOrUpdateUserGroup(userId, (int?)(op.value));
            break;

        case OperationType.Remove:
            _groupService.RemoveUserGroup(userId);
            break;
    }
}

这非常糟糕。它有很多样板,并且依赖于魔法弦。

无需更改Microsoft.AspNetCore.JsonPatch API,例如

JsonPatchDocument<UserDetails> tmpPatch = new JsonPatchDocument<UserDetails>();
tmpPatch.Add(x => x.GivenName, String.Empty);
tmpPatch.Add(x => x.Surname, String.Empty);
IEnumerable<string> userPaths = tmpPatch.Operations.Select(x => x.path);

至少会摆脱魔法弦,但是,imo,这只是感觉不对!

JsonPatch在这方面看起来非常有限,似乎更适合DAO(实体)和DTO(模型)之间存在1:1映射的系统。

有人有什么好主意吗?难以击败我想出的肚子!!

1 个答案:

答案 0 :(得分:0)

Json Merge Patch - RFC7396会更适合这个。