将Patch API与其他请求成员一起使用还是Put API? [英] Use Patch API with Different Request Members, or Put API?

查看:119
本文介绍了将Patch API与其他请求成员一起使用还是Put API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Web应用程序中,我们通过API更新客户成员信息。数据是通过对象发送的。

In web application, we update customer member information in API. Data is sent in Objects.

有时,它只是城市,有时是电子邮件,有时是两者的结合。
参见下面的不同示例,

Sometimes, its just City, sometimes email, sometimes combination of both. See varying examples below,

后端C#开发人员说,只是向整个请求对象发送了全部20个项目成员,如果要更改一个项目,只需发送相同的19个班级成员,但其中一个项目是不同的。

The back end C# developer said, just sent whole request object all 20 items members, if you want to change one item, just send the same 19 class members, with the 1 item that is different.

(1)
所以我必须发送一个重复的Blob(PUT)相同的客户信息,仅更改1类属性?
这是执行此操作的正确方法吗?

(1) So I would have to send a repetitive blob (PUT) of same customer information, just to change 1 class attribute? Is this the proper way to conduct this?

(2)还是实现Patch API的好作法,让类成员始终保持不同?

(2) Or is it good Restful practice to implement Patch APIs where class members can be different all the time?

一个问题是,开发人员需要创建另一个新的PATCH API,该业务逻辑已经很复杂(除了创建的PUT API之外)。

One concern is, developer needs to create Another New PATCH API, where business logic is already complicated (in addition to the PUT API created).

数据

public class CustomerMember
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string StreetName{ get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
    public string PhoneNumber { get; set; }
    public string EmailAddress{ get; set; }
    ....
}

可能的请求:

{
 "FirstName": "Joe",
 "City": "Atlanta"
}

{
 "email": "joe@hotmail.com",
}

{
 "PhoneNumber": "555-555-5555",
 "ZipCode": "30318"
 "Email": "joe@hotmail.com"
}


推荐答案

您可以在CustomerMember存储库中使用以下方法:

You can have a method like this in your CustomerMember repository:

public virtual void UpdatePartially(TEntity entity, IEnumerable<string> updatedProperties)
    {
        var keyNames = DbContext.Model
            .FindEntityType(typeof(TEntity))
            .FindPrimaryKey().Properties
            .Select(x => x.Name)
            .ToArray();

        foreach (var item in updatedProperties)
        {
            if (!keyNames.Contains(item))
            {
                DbContext.Entry(entity).Property(item).IsModified = true;
            }
        }
        DbContext.SaveChanges();
    }

请注意,您可以在此处获得像这样的更新属性:

Note that here you can get updated properties like this:

var updatedProperties = typeof(CustomerMember).GetProperties()
            .Select(x => new
            {
                Property = x.Name,
                Value = x.GetValue(customerMemberInstance)
            })
            .Where(x => x.Value != default)
            .Select(_ => _.Property)
            .ToList();

其中 customerMemberInstance 是动作的主体,最后您是对的,当您要清除属性时,可以将其作为空字符串发送,并且在更新属性后,只需将具有空值的属性(同样使用反射)循环,并将其设置为null。希望这很清楚,或者有任何疑问,请发表评论。

Where customerMemberInstance is the body of the action, and finally you are right, when you want to clear a property, then you can send it as an empty string and after getting updating properties just loop over the properties that have empty value (again using reflection) and make them null. Hope this is clear, or in case of questions please comment.

注意:


  1. 要清除属性-发送空字符串,

  2. 要保留相同的属性-发送Null

  3. keyNames是主键属性,由于无法更新,因此代码只会更新所有提供的非空属性,这些属性不是键的一部分

这篇关于将Patch API与其他请求成员一起使用还是Put API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆