Using ABAP Object Services framework

ABAP Object Services is a framework that allows us to implement object-relational persistence. ABAP Object Services applying an object-oriented concept.


Advantages of using persistent object services for DB CRUD activities:
  1. It allows smaller, autonomous units to be built so you can test each unit independently before it is integrated into the larger application.
  2. A very well defined modular approach helps efficient development, enhancement, and bug fixing.
  3. Overall maintainability of the code improves multifold.

Object-Relational Mapping

It is to handle the synchronization of data stored in objects with relational database tables using the three core services:
  1. Persistence Service 
    • This service is at the core of the framework, implementing the low-level technical details/plumbing needed to synchronize object instances with relational database tables. 
  2. Query Service 
    • The Query Service provides us with an object-oriented API that we can use to search for object instances within the database. 
  3. Transaction Service 
Mapping variants
....

Working with the Persistence Service ( design and configure )

  1. During creation, a class set the checkbox Persistent Class
    • Once the persistent class is initially created, .. the IF_OS_STATE interface is mentioned within the persistent class
    • Two more classes are created: the persistent class's class actor/agent. At runtime, these agent classes run interference between persistent objects and the ABAP Object Services layer 
    • Instead, we’ll have to work with the class agent to obtain persistent object instances from the ABAP Object Services layer.
  2. Mapping Persistent Classes 
    • Within the Class Builder tool, this can be achieved by opening the class in edit mode and clicking on the PERSISTENCE button..

Working with Persistent Objects 

Creating Persistent ObjectInstances 

DATA lo_obj TYPE REF TO zcl_persian. 
DATA lx_os_ex TYPE REF TO cx_os_object_existing. 
TRY. lo_author = zca_persian=>agent->create_persistent(...parameters ). 
COMMIT WORK. 
CATCH cx_os_object_existing INTO lx_os_ex. 
ENDTRY.

Reading Persistent ObjectInstances (By Business Key)

DATA lo_object TYPE REF TO zcl_persian. 
DATA lv_variable TYPE zvariable. 
DATA lx_obj_not_found TYPE REF TO cx_os_object_not_found. 

TRY. lo_object = zca_persian=>agent->get_persistent( 'DU100' ).  
lv_variable =  lo_object->get_variable( ). 
CATCH cx_os_object_not_found INTO lx_obj_not_found. 
...ENDTRY.

Updating Persistent Objects

DATA lo_object_s TYPE REF TO zcl_object_s. 
DATA lo_object_m TYPE REF TO zcl_object_m. 
DATA lx_os_ex TYPE REF TO cx_os_object_existing. 
DATA lx_obj_not_found TYPE REF TO cx_os_object_not_found. 

TRY. 
lo_object_s = zca_object_s=>agent->create_persistent( ). 
lo_object_s->set_ID( 'YYXX' ).  

lo_object_m = zca_object_m=>agent->get_persistent( 'DUM100' ). 

lo_object_m->set_publisher( lo_object_s ). 

COMMIT WORK.
  CATCH cx_os_object_existing INTO lx_os_ex. 
  CATCH cx_os_object_not_found INTO lx_obj_not_found. 
ENDTRY.

Deleting Persistent Objects 

DATA lo_object TYPE REF TO zcl_object. 
DATA lx_os_ex TYPE REF TO cx_os_object_existing. 
DATA lx_obj_not_found TYPE REF TO cx_os_object_not_found. 

TRY. 
lo_object = zca_object=>agent->get_persistent( 'DUM100' ). 
zca_object=>agent->if_os_factory~delete_persistent( lo_object ). 

COMMIT WORK. 
CATCH cx_os_object_existing INTO lx_os_ex. 
CATCH cx_os_object_not_found INTO lx_obj_not_found.
ENDTRY.

Querying Persistent Objects with the Query Service

Processing Transactions

Information on ABAP Object services 

Comments