当前推荐的使用 Web API 执行部分更新的方法是什么? [英] What's the currently recommended way of performing partial updates with Web API?

查看:22
本文介绍了当前推荐的使用 Web API 执行部分更新的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 ASP.NET Web API 的 RESTful 接口实现部分更新?例如,假设我们通过以下结构的连线传递对象:

I'm wondering how to implement partial updates with ASP.NET Web API's RESTful interface? Let's say for example we are passing objects over the wire of the following structure:

public class Person {
    public int Id { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
}

如何支持一次只更新 Person 的一部分,例如 Email 属性?是否建议通过 OData 和 PATCH 动词,还是自己实现 PATCH 会更好?

How would one support updating just parts of a Person at a time, for example the Email property? Is it recommended to implement this via OData and the PATCH verb, or would it be better to implement PATCH oneself?

推荐答案

当前最新的 稳定 版本的 Web API(从 2012 年 8 月开始)不支持.因此,如果您只想使用 Web API RTM,则必须自己实现整个管道.

There is no support in the current latest stable release of Web API (from August 2012). So if all you want to use is Web API RTM, you would have to implement the whole plumbing yourself.

话虽如此,OData 预发布包通过新的 Delta 对象非常好地支持部分更新.目前 Microsoft.AspNet.WebApi.OData 包已经是 RC 版本 (0.3),可以从这里获得:http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData

With that said, OData prerelease package supports partial updates very nicely through the new Delta<T> object. Currently the Microsoft.AspNet.WebApi.OData package is at RC version already (0.3) and can be obtained from here: http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData

安装后,您可以相应地使用它:

Once you install that, you can then use that accordingly:

[AcceptVerbs("PATCH")]
public void Patch(int id, Delta<Person> person)
{
    var personFromDb = _personRepository.Get(id);
    person.Patch(personFromDb);
    _personRepository.Save();
}

你会像这样从客户端调用它:

And you'd call it from the client like this:

$.ajax({
    url: 'api/person/1',
    type: 'PATCH',
    data: JSON.stringify(obj),
    dataType: 'json',
    contentType: 'application/json',
    success: function(callback) {            
       //handle errors, do stuff yada yada yada
    }
});

这样做的明显优点是它适用于任何属性,您不必关心是更新Email 还是Username 或诸如此类.

The obvious advantage of this is that it works for any property, and you don't have to care whether you update Email or Username or whatnot.

您可能还想查看这篇文章,因为它展示了一种非常相似的技术 http://techbrij.com/http-patch-request-asp-net-webapi

You might also want to look into this post, as it shows a very similar technique http://techbrij.com/http-patch-request-asp-net-webapi

编辑(更多信息):为了只使用 PATCH,您不需要启用任何与 OData 相关的东西,除了添加 OData 包 - 以访问 Delta 对象.

EDIT (more info): In order to just use PATCH, you do not need to enable anything OData related, except for adding the OData package - to get access to the Delta<TEntityType> object.

然后你可以这样做:

public class ValuesController : ApiController
{
    private static List<Item> items = new List<Item> {new Item {Id = 1, Age = 1, Name = "Abc"}, new Item {Id = 2, Age = 10, Name = "Def"}, new Item {Id = 3, Age = 100, Name = "Ghj"}};

    public Item Get(int id)
    {
        return items.Find(i => i.Id == id);
    }

    [AcceptVerbs("PATCH")]
    public void Patch(int id, Delta<Item> item)
    {
        var itemDb = items.Find(i => i.Id == id);
        item.Patch(itemDb);
    }
}

如果您的商品是,请说:

If your item is, let's say:

{
    "Id": 3,
    "Name": "hello",
    "Age": 100
}

您可以使用以下命令 PATCH 到 /api/values/3:

You can PATCH to /api/values/3 with:

{
    "Name": "changed!"
}

这将正确更新您的对象.

and that will correctly update your object.

Delta 将为您跟踪更改.它是一个动态类,充当您的类型的轻量级代理,并将了解原始对象(即来自数据库)和客户端传递的对象之间的差异.

Delta<TEntity> will keep track of the changes for you. It is a dynamic class that acts as a lightweight proxy for your Type and will understand the differences between the original object (i.e. from the DB) and the one passed by the client.

这不会以任何方式影响 API 的其余部分(当然,除了用更新的 DLL 替换 DLL 以促进 OData 包依赖项).

This WILL NOT affect the rest of your API in any way (except of course replacing the DLLs with newer ones to facilitate the OData package dependencies).

我添加了一个示例项目来演示 PATCH + Delta 的工作 - 你可以在这里获取它(它是 VS2012)https://www.dropbox.com/s/hq7wt3a2w84egbh/MvcApplication3.zip

I have added a sample project to demonstrate the work of PATCH + Delta - you can grab it here (it.s VS2012) https://www.dropbox.com/s/hq7wt3a2w84egbh/MvcApplication3.zip

这篇关于当前推荐的使用 Web API 执行部分更新的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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