Practical Use of OOPs ABAP – Model View Controller (MVC). Part I

12
9

The subject (ABAP Objects) I would like to share with you here today might have already been discussed numerous times in different forums and blogs, but still, I decided to write this blog because I want to present my case with a real time SAP Business example. Trust me, I will not waste your time with Vehicle Class examples (remember cars, trucks etc) or Animal Class examples. My focus today would be a real project program with a selection screen, data fetching, and display. 🙂

Being a traditional ABAP developer I was actively participating in the tug of war between the ABAP traditional approach and ABAP object oriented approach. The question is therefore NOT about using ABAP Objects or not. It is about knowing When to use it and How to use it. Along with these questions, another question strikes in mind ‘How to utilize MVC Design Pattern in ABAP? Well, I am still working on this question to ascertain the best solution, especially when screen design comes into the picture.

Let’s describe the Complex MVC (Model View Controller) in simple words. ‘Isolate the business logic from the user interface to provide the flexibility to change business logic independently‘. Wow!! We have finished complex MVC in just one sentence. Now let’s see how we can decouple the logic and what are the upside of this approach.

The advantage of using this approach is to reuse the existing logic in multiple similar requirements.   Let’s say we have a sales order report to prepare.  We can design our Model (business logic classes) in such a way that the same Class (model) can be used for creating the different views like report, smart forms. This approach gives us the flexibility to reuse that logic as it is part of the Model class.

Also Read: Just 4 Versions of the same program to understand OOPs ABAP

Here is a simple report to present a real time practical scenario.

PS: This is Part 1 of the 3 Parts series on MVC Concept.

Scenario 1:

Create a report to display the Sales Documents (sales order, quotation, contract) based on Plant.

Let’s design our Selection screen.

OOPs ABAP

OOPs ABAP Programming

To follow MVC pattern, I have created three separate local classes.

  • Selection screen CL_SEL
  • Fetch Data CL_FETCH(Act as MODEL)
  • Display data CL_ALV (Act as VIEW).

1. Class for Selection Screen – CL_SEL:

We have one method here GET_SCREEN. This method will get the data from the selection screen.

abap objects

This class CL_SEL will just get the data from selection parameters to make it available for our next class CL_FETCH. (All the validations we can perform in class CL_SEL, let’s say Plant validations, which we will cover it in next part)

2. Class for Database Fetch – CL_FETCH:

This class has two methods CONSTRUCTOR and FETCH_DATA.

CONSTRUCTOR: contain the ref of the selection class CL_SEL.

FETCH_DATA: will select the data from DB table.

object oriented programming

Now if you notice here in CL_FETCH class we have created object of CL_SEL class, as the objective is to decouple as much as possible so we are just passing the reference in Constructor.

Q: Why in the constructor? 

Ans: Simply because constructor method will get called automatically when the object is created.

Now if you see our FETCH_DATA method we are fetching the records based on our selection screen parameters which are in another class (Magic of using Object).

3. Class for Display – CL_ALV:

This class is acting as the View and we have two methods here CONSTRUCTOR and DISPLAY_ALV.

CONSTRUCTOR: contain the ref of class CL_FETCH.

DISPLAY_ALV: will display the data using SALV table FACTORY method.

ABAP OOPs

Now we have created the Object and we are good to go for calling our Methods (so our dish is almost ready, we are simply garnishing our dish).

call abap objects in program

Do you Know? How to convert an existing ABAP Report to OOPs ABAP?

Did you Notice, we are using all the events of ABAP report?

This is a very simple report to demonstrate the concept of MVC so I am using local classes but it is always recommended to use global classes (i.e. class created in t-code SE24). One more reason behind local classes; we lazy ABAPers are very good in copy and paste options, so this is an extra tip for you.

Why global classes? 

Ans: Being lazy ABAPers, we often don’t listen to our Experts. I did the same (ignored global class and used my local class in the program). After some time, I got another requirement. Using the same input parameters, develop another report which will send our forms in PDF format to our special Customers. Then I realized the power of MVC and global classes …. as yet gaining from my oversights.

Here is the fresh hot DISH from the oven i.e. our First Real OOPs Program.

oops abap

object oriented programming

Want to learn HANA ABAP? ABAP on SAP HANA. Part I. First Program in ABAP HANA

Do you really think End-User will agree with this report?

Obviously No! They will request for a tasteful design (better user interface in the output View) with huge numbers of options/alternatives in it (like print, sort, download etc). So let’s cover them in our next article, how we can modify the layout (View) without changing the data class (Model). Please stay tuned.

 

OOPs ABAP series on MVC Concept

Part I – Model View Controller (MVC) in SAP ABAP – Introduction

Part II – Model View Controller (MVC) in SAP ABAP – Enhance the View

Part III – Model View Controller (MVC) in SAP ABAP – Enhance the Model

Please Subscribe, Like and Share our Page.

 

12 COMMENTS

    • Hi Bhavesh,
      As per my understanding there can be two different vision about MVC, the model can be observer and directly notify the changes to View and the other is controller request the updates to model and once done the controller sends the information to view .
      After some developments in MVC and report I observe that there is no direct solution for implementing MVC in ABAP. It is totally based on the requirement.
      I will try to post the other way of using MVC soon. I am hoping that it will solve your confusion and you will be able to compare both the approaches.

  1. Thanks for the article, I have one basic question , do we still use END-OF-SELECTION? Why?
    What will be the difference if we don’t use it & write the code in START-OF-SELECTION.

    • Thanks Sharath….
      This event is used when there is use of logical data base in the report once the logical database completes reading all data i.e. all the selection is finished. This statement tells the server that all the database reading is completed and no more data reading is going to take place.

      What will be the difference if we don’t use it & write the code in START-OF-SELECTION ?
      The answer is when a program is executed by the user, the database is locked when it encounters a START-OF-SELECTION statement and the lock is released when an END-OF-SELECTION statement is encountered (to maintain the consistency). Data selection is done between START-OF-SELECTION and END-OF-SELECTION. Now if we don’t write the END-OF-SELECTION statement, the database will remain locked till the programs get terminated. Hence, the performance will be reduced. So it is always good practice to write END-OF-SELECTION statement after finishing the data selection from database.
      Even you Can display the Output in START-OF-SELECTION there will be no difference.
      Output will be same But to Segregate the Display Coding we use it.
      I hope it help you…

Leave a Reply to Jyoti Patel Cancel reply

Please enter your comment!
Please enter your name here