Search code examples
odataetag

OData etag handling for related entities


I am building an OData service for a Sales order object. For simplicity, assume the sales order object has 2 entities - Header and Items. From a business perspective, Header has Total Amount which is updated based on the Item amount values.

Now I have a client application where both the Header and Items are fetched and displayed. When the client application fetches the order the Header entity has an Etag HE1 and the Item entity (assume that there is only 1 item) has Etag value IE1.

The client application updates the Item Amount value. A PATCH call is done for the Item which results in Item Etag being changed from IE1 -> IE2. However even though no explicit updated was done on the Header, due to the Total Amount calculation logic the Header Etag also has technically changed from HE1 -> HE2. However, since the PATCH operation is only done at the Item level how can the client know that Header etag has changed ? Without this information if the client now tries to update the header the (via a separate PATCH call) this would now fail as the ETag for the header has changed meanwhile.

Any ideas around how this can be done ?


Solution

  • The scenario you mention can not only happen with OData services but with applications that use optimistic locking. The only solution I see is that the client application has to be aware that changing a sales order item also affects the sales order header.

    Therefore the client application has to reload the header data after a successful PATCH request (maybe using a batch request for bundling the PATCH on the item and the GET on the header). After this the client application knows both the updated header data and the new header ETag value.