In oData 4.0 Actions and functions are frequently referred. I couldn't get what are they and how they are different. Are function same functions we have in any programming language or something else ? What are Actions ? and how both are different. Thanks for your help.
Actions - Can be used to perform CRUD operations on an entity. That means , you can create,update,delete the entity using custom actions if default actions (POST/PUT/DELETE) doesn't support your requirements. Also you can use the custom actions to fetch the data from multiple entities for complex types. Actions are similar to Stored procedures in SQL which allows SELECT as well as DML queries.
Functions - Ideally, you should use functions to get data only and not for data modifications. These are similar to functions in SQL which allows SELECT queries only.
Short explanation copied from the Spec:
Actions are operations exposed by an OData service that MAY have side effects when invoked. Actions MAY return data but MUST NOT be further composed with additional path segments.
Functions are operations exposed by an OData service that MUST return data and MUST have no observable side effects.
Path Segment
Each seperate part of the OData URL is a path segment.
The URI /Products(1)/Supplier
has three path segments.
- Entity Set -
Products
- Key -
1
- Navigation -
Supplier
Seems better you should learn about what a "side effect" is in general, this is not OData related but if you wanna mess with OData you need to know!
Each computer system has a kind of "state", part of that is observable from the outside (e. g. by some query). A function from above OData definition "has no side effects" in that it does not change the state of the database, e. g. a GET request for some cell content. You can repeat it over and over again and will get the same result each time.
Contrary to that is an action from above definition which "may have side effects". Means the execution of the action could change the database content and if you execute it more than once you may get different results each time. E. g. a DELETE command to some entity. The first call might succeed but every successive call will bail out with "not found". So this call has a side effect, it deletes an object. The same is with mutating actions like PATCH, the object is not deleted but modified. So it has side effects.
From the spec in OData.org, they are operations, and the difference is have side effect or not.
In WebAPI/OData's implementation you can refer to the docs here:
http://odata.github.io/WebApi/#04-06-function-parameter-support
http://odata.github.io/WebApi/#04-07-action-parameter-support
you can see the signature is obvious:
Action:
[HttpPost]
public IHttpActionResult PrimitiveAction(ODataActionParameters parameters)
Function:
[HttpGet]
public string ComplexFunction([FromODataUri]Address c1, [FromODataUri]IEnumerable<Address> c2)
© 2022 - 2024 — McMap. All rights reserved.