Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Hi Friends,

The following a new way of understanding of MVC Architecture those who know ABAP Objects and ABAP WEBDYNPRO already.

It gives definitely clear understanding of MVC Architecture clearly with high level of understanding what actually is happening MVC.

We are giving your Model name to your View and your view pulling the data from model and showing with the help of controller. If 

you are bored with theoretical explanation of MVC Architecture and if you know ABAP Objects including casting!

The following is the Technical explanation on how the MVC Architecture works with example.

My question to you?

Can a view accept any model to display it? If so how is it happening? Let’s see.

1. MODEL

Model (Model class) is nothing but a class which forms certain data in it which you want to show in WEBDYNPRO Screen. Let us create a simple global class using transaction SE24 and write a simple select query that will select certain data into an internal table.

Go to t-code SE24 and create a class ZCL_MODE1 as follows

Give description as above and save.

Now let’s create a method in this model class, where this method can fetch some data as follows

Here, I am creating an instance method, which has the visibility as public with exporting parameter

Now let us create the signature of the method by keeping the cursor on the GET_VBAP and clicking on the Parameters Tab.

Give the parameter name as ET_VBAP and make the type as ‘Exporting’ and the associated type as VBAP_T

Now let’s go back to the method to implement the method. Double click on the method and write the code as follows to build some data in our model class.

Now save syntax check and activate the method and whole class.


With this Our Model class is ready with some VBAP data. This helps understand that in MVC architecture a ‘MODEL’ means "a unit/section which contains or builds certain data to display" on any WEBDYNPRO Application (VIEW).

*****************************************************************************************************************************************************

2. CONTROLLER

Let us see about Controller (Controller class) . We know ‘Model’ and ‘View’ cannot talk to each directly. We have an intermediate communication channel between ‘Model’ and ‘View’ which is nothing but our ‘Controller’.

‘Controller’ or ‘Controllers’ are nothing but the intermediate channels that enables us to show the modeled data (data from model class) on view (WEBDYNPRO application).

Now let us make prepare the Controller class.

Create a controller class using transaction SE 24

Do you remember?

What happens when you create a service call and call the model?

An attribute will be created in an attribute tab right, which is of type reference to that model.


Do you remember?

When you work with Assistance class! When you incorporate your assistance class in your controllers, what happens?

Everywhere you will have an attribute ‘WD_ASSIST’.

Same process we have to follow here, so whenever you have a model class concept, there is no doubt you will have an attribute and that attribute is of Type Reference to the model.

That attribute can be used by any view Or that can be used by any controller method also.

So in the same way of type reference to our model class, let us create a reference variable in our "CONROLLER CLASS"

Save your controller class and go to the Attributes tab in this class.

Declare an Instance variable called ‘Model’ with the visibility ‘Public’ and the Typing as ‘Type Ref To’ and the Associated type as ‘OBJECT’.

Where ‘OBJECT’ is the super class of all classes. Here why we are referring this, if you know the name of the model class clearly declare the variable of the same type reference, where if you don’t know, let us declare our reference variable of model class with reference to ‘Super class’ of the classes.

Go to the Method tab in this controller class and declare a method which is of type ‘Instance’ and ‘Public’ with an importing parameter.

Keep the cursor on the method ‘GET_MODEL’ and click on ‘Parameters’.

Declare an importing parameter which can import a class name (Nothing but the model class name). We know the data type of any class is ‘SEOCLSNAME’, so declare the importing parameter also of the same type.

Now let us implement this method as follows.

As of now we are supposed to import a class name (name of model class) to this method in this controller class. Now instantiate the attribute ‘Model’ with its class (importing class), so that the attribute ‘model’ can access the method (GET_VBAP) of ‘Model class’ (ZCL_MODE1).

So we are supposed to create instantiation for the variable/attribute ‘model’ with type reference to the class which is coming and sitting in the variable iv_class_intf.

The above statement is nothing but ‘NARROW CASTING’. We are creating the reference of variable (which is of type reference of super class OBJECT with respect to type of class coming into (iv_class_intf).

Here instantiation of super class variable is done with type casting (Narrow Casting) of child class coming into (iv_class_intf).

Any class is a child of super class ‘OBJECT’.

So finally, we have created the reference variable for our model class in our controller class.

*****************************************************************************************************************************************************

3. MODEL

Now let us discuss about the View class (View Controller).

Tell me one thing; can I use my component controller in my view controller directly?

The answer would be No.

We need to tell the usage of component controller in view controller right, which is nothing but the instantiation of component controller in view controller.

In the same way, to use the controller class service in view class, first we have to instantiate the controller class in our view controller. Through this method I can get model because model and view cannot talk directly.

Controller is a mediator between the Model & View. So how can we access controller? By instantiation!

Let us create a local class for View as follows and put the code written as follows

The same code is as follows.

PARAMETERS: p_class type char30.

data: lcl_vbap type table of vbap,

      wa type vbap.

START-OF-SELETION.



*Instantiate the component controller and through this talk to the model class



data: o_controler type ref to zcl_controler1.

create object o_controler.



o_controler->get_model( p_class ).



data: o_model type ref to zcl_mode1. "Create the reference variable of model class



o_model ?= o_controler->model. "Wide casting or "Instantiate the model class reference variable

*                                                with Model variable

*                                                which is superclass ref vairable



o_model->get_vbap( importing et_vbap = lcl_vbap ). "Now call the method which forms the data using

*                                                   this model class reference



loop at lcl_vbap INTO wa.            "Display the data in View class or View controller

  WRITE: / wa-vbeln, wa-posnr.

ENDLOOP.

**********************************************************************************************************************************

Let’s execute the M (Model), V (View), C (Controller) classes simultaneously where these 3 works individually.

Give your Model class name which has data selection in it.

Now Execute. It will shows the following data in our view class

CONCLUSION:-

You are giving your Model name to your View and your view is pulling the data from model and showing with the help of controller. This how any MVC Architecture will work!

4 Comments
Labels in this area