You have various development options to create an OData Service:
- Manual generation
You can create the Model-Provider-Class and the Data-Provider-Class, as well as the technical model and the technical service manually. Choosing this way, you have to familiarize yourself with OData API.
- Generation in the Object Navigator (SE80)
Create a technical model in the Object Navigator (SE80) and generate the Model-Provider-Class (MPC) and the Data-Provider-Class (DPC) from existing RFC function modules, Business Object Repository (BOR) objects, or SAP-GUI-screens. You can then also have the technical service generated in the Object Navigator. Ideally, you do not have to code at all with this approach.
- Generation in the Gateway Service Builder (SEGW)
SAP Gateway Service Builder provides you with an easy-to-use set of tools for creating services. It has been conceived for the code-based OData Channel and supports you throughout the development life cycle of a service. The Gateway Service Builder allows you to describe the object model and to generate the entire Model-Provider-Class and part of the Data-Provider-Class from this description. The Gateway Service Builder can also generate the technical model and the service. If you choose this way, you only have to code the data access.
Regardless of which option you choose, you have to create the OData-Service in the service catalog manually. For creating the OData-Service, use transaction: /IWFND/MAINT_SERVICE.
The development of services using the Gateway Service Builder takes place in the SAP back-end system.
The Gateway Service Builder supports the following development steps:
- Project Creation
The process starts when you create a project in the Gateway back-end system. To create a project, use the transaction code: SEGW. The Service Builder bundles the artifacts you need to develop in a project.
- Data Model Definition
In this phase, you define the model your service is to be based on. That is, you define the entity types, entity sets, associations, and so on that underlie your service.
- Service Implementation
In this phase, you implement the operations that are supported by the service. Here, the runtime objects (ABAP classes and other repository objects) that make up a Gateway service are generated. The actual implementation of the service can be done either by writing code, or by mapping the methods of an OData service to the methods of a data source. In this phase, you also register the service in the backend.
- Service Maintenance
In this phase, you activate (or publish) the service on the SAP Gateway hub system that has been registered in the SAP backend system.
Data Model Definition
An OData service is a logical data model that describes entities by using associations and operations. The figure, Data Model Example, shows a simple logical data model and the associations between the entity types. In this example, we want to expose a service to access business partners, sales orders, sales order line items, products, and the contacts of business partners.
When building the OData model in the Service Builder, you can reuse existing data structures, or manually create model components.
There are two types of documents associated with each OData service:
- Service document
- Service metadata document
Both document types can be retrieved from a server by means of a HTTP GET call. The service document is available at the Service Root URI and is formatted in Atom or JSON. If you click on the hyperlink, you send a HTTP GET call to the server and the server responds with the service document.
Service Builder Tree Structure
The Service Builder provides a nice tree view folder structure to organize the different components of a data model. This structure provides one folder for each of the following:
- Entity Types
- Complex Types
- Entity Sets
- Association Sets
- Function Imports
In the figure, Data Model in Service Builder, the example entity data model consists of the five entity types: Product, BusinessPartner, SalesOrder, SalesOrderItem, and Country (not yet modeled in the Service Builder).
Gateway Service Implementation
The results of the design-time activities in the SAP Gateway Service Builder are mirrored in ABAP classes and two new Gateway-specific repository objects.
An OData service is based on two regular ABAP classes:
- A Model Provider Class (MPC)
- A Data Provider Class (DPC)
The MPC defines the model and metadata of the service. The DPC provides the functionality of the Gateway Service. Both the MPC and the DPC are registered as a service by the Service Builder when the project is generated.
The two base classes contain logic, which is generated by the Service Builder. The logic in the base classes is overwritten every time the project is regenerated. For this reason, you should not modify the code in these classes, as the Service Builder overwrites their contents.
The extension class names have the suffix EXT. An extension class is a subclass of the base class that is created once, only when the project is generated for the first time. An extension class initially contains no logic. The Service Builder provides the extension class for you to write your own code. Regenerating the project does not overwrite your code in the extension classes, therefore the service implementation takes place in the data provider extension class.
Usually, there is no need to maintain the MPC_EXT also.