Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

oData v4 what are Functions and Actions in simple terms?

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.

like image 946
user576510 Avatar asked Jun 29 '16 03:06

user576510


People also ask

What are actions and functions in OData?

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.

How do I use OData v4 unbound actions?

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.

Why use Web API for OData?

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.

What is the use of actions and functions in web API?

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.


3 Answers

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.

like image 134
Vinit Avatar answered Oct 20 '22 19:10

Vinit


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
like image 39
Rene Avatar answered Oct 20 '22 21:10

Rene


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.

like image 35
Don Pedro Avatar answered Oct 20 '22 19:10

Don Pedro