RESTful发布多个实体的策略 [英] Strategy for RESTfully posting many entities

查看:98
本文介绍了RESTful发布多个实体的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在适应以REST方式做事.

I am still in the process of getting comfortable with doing things the REST way.

在我的情况下,客户端软件将与RESTful服务进行交互.客户端很少会上载其整个实体数据库(每个实体序列化为大约5kb的xml块).

In my situation, client software will be interacting with a RESTful service. Rarely, the client will upload its entire database of entities (each entity serializes into a roughly 5kb chunk of xml).

也许我错了,但是正确的RESTful策略似乎是在每个实体之间循环并分别对每个实体进行POST.但是,似乎可能有成千上万个这样的实体,而且某种程度上,如此之多的快速启动POST似乎并不符合要求.

Perhaps I'm wrong, but the proper RESTful strategy seems to be to cycle through each entity and individually POST each one. However, there may plausibly be tens of thousands of these entities, and somehow so many rapid-fire POSTs doesn't seem kosher.

在这种情况下,感觉像将所有实体打包为一个大xml表示形式将违反RESTful的处理方式,但同时也避免了成千上万个POST的需要.

In this situation, it feels like packaging all the entities into one big xml representation would violate the RESTful way of doing things, but it would also avoid the need for thousands of POSTs.

是否有一些实现此目的的标准做法?预先感谢!

Is there some standard-practice for accomplishing this? Thanks in advance!

推荐答案

这里至少存在两个问题,这些问题使您无法获得RESTful.

There are at least two problems here which prevent you from being RESTful.

  1. 每个资源都需要由URI标识.对资源采取行动意味着您必须使用HTTP调用来调用URI.因此,您不能仅通过一个HTTP调用就在多个资源中调用多个操作.

  1. Each resource needs to be identified by a URI. Acting on the resource means that you must call the URI using an HTTP call. Consequently, you cannot call multiple actions in multiple resources in just one HTTP call.

资源由名词标识并代表实体.这意味着要插入雇员"和汽车",您需要为各个实体调用两个不同的资源.

The resources are identified by nouns and represent entities. This implies that to insert an Employee and a Car you need to call two different resources for each of the respective entities.

因此,总之,您不能在此处采用纯粹的RESTful方法.但是,REST旨在通过约定来提供帮助,而不是限制您.最好的解决方案是为您创建一个可以满足您需求的自定义操作.

So in summation you cannot take a purely RESTful approach here. However, REST is designed to help by way of conventions, not constrict you. The best solution here is for you to create a custom action which does what you need.

或者,您可以使用INSERT,UPDATE和其他操作创建通用包装实体,这些包装实体将不同数据的斑点作为XML.但是,这将破坏您的其他端点,因为现在可以通过通用包装器和/Car/ URI插入Car记录了.

Alternately, you can create a generic wrapper entity with INSERT, UPDATE and other actions which take in blobs of disparate data as XML. However, this will undermine your other end points because now it becomes possible to insert a Car record through the generic wrapper and through the /Car/ URI.

在不了解您的实际需求的情况下,我建议您不要专门通过REST公开此功能.在幕后,一旦分离了不同对象的传入集合,仍然可以在各个Controller中调用INSERT操作方法.

Without knowing much about your actual requirements, I would suggest you don't expose this functionality via REST specifically. Behind the scenes you could still call your INSERT action methods within the various Controllers once you break up the incoming collection if disparate objects.

这篇关于RESTful发布多个实体的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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