In this post, we will see how we can implement the Views which will access the Controller and model which is encapsulated in the controller. This post is in continuation of previous post: ABAP Objects Design Patterns – Model View Controller (MVC) Part 2.
First Demo Application – ALV
For our first Application view will be ALV output. To get the data for the ALV into the application, we will use the reference of the MODEL class created in the Controller. This way our model class is entrily separated by the view.
Code Snippet for View 1: ALV of MVC design
*&---------------------------------------------------------------------* *& Purpose - ABAP Design Patterns - Model View Controller MVC - ALV demo *& Author - Naimesh Patel *& URL - http://zevolving.com/?p=52 *&---------------------------------------------------------------------* * REPORT ztest_mvc_alv. * START-OF-SELECTION. *--------- * Controller *--------- DATA: lo_control TYPE REF TO zcl_control. * * Iniiate controller CREATE OBJECT lo_control. * * Get the object from Control CALL METHOD lo_control->get_object EXPORTING if_name = 'ZCL_MODEL'. * *--------- * Model - Business Logic *--------- * Date Range DATA: r_erdat TYPE RANGE OF vbak-erdat, la_erdat LIKE LINE OF r_erdat. * la_erdat-sign = 'I'. la_erdat-option = 'BT'. la_erdat-low = sy-datum - 10. la_erdat-high = sy-datum. APPEND la_erdat TO r_erdat. * * Get data method CALL METHOD lo_control->o_model->get_data EXPORTING ir_erdat = r_erdat. * *--------- * View - ALV output *--------- DATA: lo_alv TYPE REF TO cl_salv_table. * DATA: lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = lo_alv CHANGING t_table = lo_control->o_model->t_vbak ). CATCH cx_salv_msg INTO lx_msg. ENDTRY. * * * Displaying the ALV lo_alv->display( ).
First Demo Application – SmartForms Output
Our Second application is fairly simple once we have implemented the first application as our core logic of getting the data is out of the report. In this application only part which differs is calling the Smartform.
Code Snippet for View 2: Smartforms of MVC design
*&---------------------------------------------------------------------* *& Purpose - ABAP Design Patterns - Model View Controller MVC - *& SmartForms Demo *& Author - Naimesh Patel *& URL - http://zevolving.com/?p=52 *&---------------------------------------------------------------------* * REPORT ztest_mvc_view_2_ssf. * START-OF-SELECTION. *--------- * Controller *--------- DATA: lo_control TYPE REF TO zcl_control. * * Iniiate controller CREATE OBJECT lo_control. * * Get the object from Control CALL METHOD lo_control->get_object EXPORTING if_name = 'ZCL_MODEL'. * *--------- * Model - Business Logic *--------- * Date Range DATA: r_erdat TYPE RANGE OF vbak-erdat, la_erdat LIKE LINE OF r_erdat. * la_erdat-sign = 'I'. la_erdat-option = 'BT'. la_erdat-low = sy-datum - 10. la_erdat-high = sy-datum. APPEND la_erdat TO r_erdat. * * Get data method CALL METHOD lo_control->o_model->get_data EXPORTING ir_erdat = r_erdat. * *--------- * View - Smartform Output *--------- * Smartform FM DATA: l_form TYPE tdsfname VALUE 'ZTEST_MVC_VIEW_2', l_fm TYPE rs38l_fnam. * CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' EXPORTING formname = l_form IMPORTING fm_name = l_fm EXCEPTIONS no_form = 1 no_function_module = 2 OTHERS = 3. * * calling Smartform FM DATA: ls_control TYPE ssfctrlop. " Controlling info DATA: ls_composer TYPE ssfcompop. " Output info * CALL FUNCTION l_fm EXPORTING control_parameters = ls_control output_options = ls_composer user_settings = ' ' t_vbak = lo_control->o_model->t_vbak EXCEPTIONS formatting_error = 1 internal_error = 2 send_error = 3 user_canceled = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.
Let’s say, in future we enhanced the business logic model class and now we want to implement for the business. In this case, we just have to change the object reference created in the Controller and we are good to go. Obviously, we have to take care of the newly created methods or methods which parameters are enhanced.
Related Links:
ABAP Objects Design Patterns
SDN Wiki – Model View Controller design Pattern
In the article, I don’t understand why “must” need controller?
Naimesh,
Wonderful article. For the first time, I got the feeling that I just might be able to tame this animal called MVC. I am a functional consultant and am not able to grip MVC from a programming angle. I request a little more explanation using the same sales order report you started with. Why can’t the view directly call the model (why can’t we directly ask for the data based on view requirements)? For what kind of business changes in the background does the controller help to reduce development efforts (how does MVC improve on just MV)? Could you explain with the same sales order report?
Thanks!
Fischer Carl
Hello Fischer Carl,
Glad you like the article.
Since we have a new CONTROLLER (another layer of abstraction) we can easily maintain those application. Like: You have a report, which was created using the MVC, than you can very well build another view (BSP, Web Dynpro etc.) easily on it. And the controller will control the flow of the individual view.
Regards,
Naimesh Patel
Hello Naimesh,
I found this article and it is great.
I suggest you for using SAPLINK to provide the Abap code.
Thanks,
Jatra
Hello Naimesh,
Thanks for the great article. I tried to compile your code in my system. However, I got this error message that Field "O_MODEL->GET_DATA" is unknown.
May I know how you define the o_model?
Hello Anonymous,
You asked: May I know how you define the o_model?You can find it in the previous article:
http://help-abap.blogspot.com/2008/10/abap-objects-design-patterns-model-view_13.html
Regards,
Naimesh Patel
Hi Naimesh,
Great Use of MVC concept in ABAP objects as till now only use MVC in WebDynpros only.
Regards
Nimesh S Patel.
Hi Naimesh,
Now I understand about MVC concept in abap
a little.
May I be fan of yours?
joke:)
thank for the post.
Hi Naimesh,
First thanx alot for sharing this!
Maybe you can help me a little bit, reading about MVC I see that common thing is to call first the controler and the controler determines which view and model to execute, it seems here that for ABAP it is the View that´s been called first is this rigth? and also I´ve seen a Context Binding found in MVC on Webdynpro ABAP do you know how this helps on MVC?
Thans Again and Kind Regards!
Gerardo J
Hello Gerardo J,
I'm glad, you liked it. You asked:
it seems here that for ABAP it is the View that´s been called first is this rigth?
Actually, its always a controller. Like if we run a ABAP report, which gets data (Model) and generates a Output(View), the ABAP Report is the Controller. The way I have shown it here, delegates the control to the class.
Regards,
Naimesh Patel
Thanx Alot Naimesh!
Kind Regards,
Gerardo J
Hi Naimesh,
Congratulations for your simple but very interesting article of yours. I’m newbie in MVC design pattern and ABAP objects in general and this article had cleared out the main concepts of MVC pattern with ABAP Objects.
I have a question which might be quite simple but i haven’t found any answer.
While reading about MVC, the Model examples i found dealt with sql statement on a single table.
When we have more complicated sql statements using joins and so, how do the Model looks like?
Thank you in advance…..