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 and Functions in OData v4 Using ASP.NET Web API 2.2. In OData, actions and functions are a way to add server-side behaviors that are not easily defined as CRUD operations on entities.
The same thing can be achieved by using OData V4 unbound actions as described in this article. To declare an OData unbound action define a codeunit with a procedure with the desired business logic. The following example illustrates a simple codeunit with three procedures that can be exposed as a web service and called as OData unbound actions.
From these examples, we can see that the functions and actions shipped with Web API provides a very easy and useful way for customers to add their special and customized server-side behaviors to their OData service.
The actions and functions are the way to perform server side operations which are not easily defined as CRUD operations on entities. We can define actions and functions to OData V4 endpoint with Web API. Both actions and functions are able to return data. The following are some use of actions.
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.
Products
1
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With