forked from simplefx/Simple.OData
-
Notifications
You must be signed in to change notification settings - Fork 199
Batch requests
Jason Finch edited this page Aug 23, 2018
·
10 revisions
To optimize network communication OData protocol supports sending multiple updates in batches. This is not the same as wrapping updates in transactions (the underlying OData service provider may not even support transactions, and OData client would be completely unaware of this fact), however it's a very efficient method of sending multiple update requests in a single HTTP POST request. Simple.OData.Client has a special class ODataBatch to wrap multiple operations in a batch.
var batch = new ODataBatch(serviceUri);
batch += c => c.InsertEntryAsync(
"Products",
new Entry()
{
{ "ProductName", "Test1" },
{ "UnitPrice", 21m }
},
false);
batch += c => c.InsertEntryAsync(
"Products",
new Entry()
{
{ "ProductName", "Test2" },
{ "UnitPrice", 22m }
},
false);
await batch.ExecuteAsync();
var batch = new ODataBatch(_serviceUri);
batch += c => c
.For<Product>()
.Set(new Product() { ProductName = "Test1", UnitPrice = 21m })
.InsertEntryAsync(false);
batch += c => c
.For<Product>()
.Set(new Product() { ProductName = "Test2", UnitPrice = 22m })
.InsertEntryAsync(false);
await batch.ExecuteAsync();
var x = ODataDynamic.Expression;
var batch = new ODataBatch(_serviceUri);
batch += c => c
.For(x.Products)
.Set(x.ProductName = "Test1", x.UnitPrice = 21m)
.InsertEntryAsync(false);
batch += c => c
.For<Product>()
.Set(x.ProductName = "Test2", x.UnitPrice = 22m)
.InsertEntryAsync(false);
await batch.ExecuteAsync();
Request URI: POST /$batch
Request content:
--batch_77b3aa21-b11a-4307-b0df-36a5c566667e
Content-Type: multipart/mixed; boundary=changeset_f4b2eee4-7345-4344-b592-be7b7348e53a
--changeset_f4b2eee4-7345-4344-b592-be7b7348e53a
Content-Type: application/http
Content-Transfer-Encoding:binary
POST http://nrkdt58341/Temporary_Listen_Addresses/SimpleODataTestService2/Products HTTP/1.1
Content-ID: 1
Content-Type: application/atom+xml;type=entry
Content-Length: 496
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title />
<updated>2012-10-15T15:03:45.3000000Z</updated>
<author>
<name />
</author>
<id />
<content type="application/xml">
<m:properties>
<d:ProductName>Test3</d:ProductName>
<d:UnitPrice m:type="Edm.Decimal">21</d:UnitPrice>
</m:properties>
</content>
</entry>
--changeset_f4b2eee4-7345-4344-b592-be7b7348e53a
Content-Type: application/http
Content-Transfer-Encoding:binary
POST http://nrkdt58341/Temporary_Listen_Addresses/SimpleODataTestService2/Products HTTP/1.1
Content-ID: 2
Content-Type: application/atom+xml;type=entry
Content-Length: 496
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title />
<updated>2012-10-15T15:03:45.3660000Z</updated>
<author>
<name />
</author>
<id />
<content type="application/xml">
<m:properties>
<d:ProductName>Test4</d:ProductName>
<d:UnitPrice m:type="Edm.Decimal">22</d:UnitPrice>
</m:properties>
</content>
</entry>
--changeset_f4b2eee4-7345-4344-b592-be7b7348e53a--
--batch_77b3aa21-b11a-4307-b0df-36a5c566667e--
IDictionary<string, object> category = null;
var batch = new ODataBatch(serviceUri);
batch += async x => category = await x.InsertEntryAsync(
"Categories",
new Entry()
{
{ "CategoryName", "Test15" }
});
batch += c => c.InsertEntryAsync(
"Products",
new Entry()
{
{ "ProductName", "Test16" },
{ "UnitPrice", 18m },
{ "Category", category }
},
false);
await batch.ExecuteAsync();
var category = new Category() { CategoryName = "Test15" };
var batch = new ODataBatch(serviceUri);
batch += async c => await c
.For<Category>()
.Set(category)
.InsertEntryAsync();
batch += c => c
.For<Product>()
.Set(new { ProductName = "Test16", UnitPrice = 18m, Category = category })
.InsertEntryAsync()
await batch.ExecuteAsync();
var x = ODataDynamic.Expression;
var category = new Category() { CategoryName = "Test15" };
var batch = new ODataBatch(serviceUri);
batch += async c => await c
.For(x.Categories)
.Set(category)
.InsertEntryAsync();
batch += c => c
.For(x.Products)
.Set(x.ProductName = "Test16", x.UnitPrice = 18m, x.Category = category)
.InsertEntryAsync();
await batch.ExecuteAsync();
Request URI: POST /$batch
Request content:
--batch_6c440ee2-34a4-41a6-b891-331a5b54ee29
Content-Type: multipart/mixed; boundary=changeset_98134adc-dba3-419d-b44f-256fd4849c25
--changeset_98134adc-dba3-419d-b44f-256fd4849c25
Content-Type: application/http
Content-Transfer-Encoding:binary
POST http://nrkdt58341/Temporary_Listen_Addresses/SimpleODataTestService2/Categories HTTP/1.1
Content-ID: 1
Content-Type: application/atom+xml;type=entry
Content-Length: 441
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title />
<updated>2012-10-15T15:09:14.9040000Z</updated>
<author>
<name />
</author>
<id />
<content type="application/xml">
<m:properties>
<d:CategoryName>Test15</d:CategoryName>
</m:properties>
</content>
</entry>
--changeset_98134adc-dba3-419d-b44f-256fd4849c25
Content-Type: application/http
Content-Transfer-Encoding:binary
POST http://nrkdt58341/Temporary_Listen_Addresses/SimpleODataTestService2/Products HTTP/1.1
Content-ID: 2
Content-Type: application/atom+xml;type=entry
Content-Length: 497
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title />
<updated>2012-10-15T15:09:15.0160000Z</updated>
<author>
<name />
</author>
<id />
<content type="application/xml">
<m:properties>
<d:ProductName>Test16</d:ProductName>
<d:UnitPrice m:type="Edm.Decimal">18</d:UnitPrice>
</m:properties>
</content>
</entry>
--changeset_98134adc-dba3-419d-b44f-256fd4849c25
Content-Type: application/http
Content-Transfer-Encoding:binary
PUT $2/$links/Category HTTP/1.1
Content-ID: 3
Content-Type: application/xml
Content-Length: 84
<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">$1</uri>
--changeset_98134adc-dba3-419d-b44f-256fd4849c25--
--batch_6c440ee2-34a4-41a6-b891-331a5b54ee29--
See also:
Modifying data
OData batch processing