Discussion on challenges while implementing design pattern Model View Controller in ABAP objects.
In few words: In MVC, User Interface acts as the View; Business logic acts as the Model and the part which provides the link between the View and Model is known as the Controller.
To brush up your MVC knowledge, check out
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 1
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 2
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 3
Here is how the discussion shaped:
I have been trying to implement MVC in most of my developments( which has UI interaction ). Recently I got a development where I can make use of it so that I can learn it well.
I read a lot of documents about this paradigm. I am still confused about certain points. There are two different visions about MVC, I read that a model can be an observer and can directly notify the changes to the view, other vision is that controller requests model for data updates and then when done controller notifies/updates the views ( KNOWN AS PASSIVE VIEWS )
Also, I see in your examples, that controller holds the public instance of model and using this public instance the view displays the data, so what if the view edits a row, it gets directly changed in the model. Is this correct ? Is this applicable ?
In my development, i first thought of model providing its data reference to controller and then controller to the view. But here, its the same, when the row is changed in ALV, the model rows are changed directly( since it a data reference of model ).
Later, I removed this references and then stored the contents locally in controller retrieved from model using getter methods. The controller creates the reference for this data itself, creates appropriate view and passes this reference to the views. Now each view has its local handler for example: ALV line click / changing the contents ), once the content has been changed, it raises a event of controller called DATA_CHANGED, this event in controller updates the model with the new changes ). Here each views are inherited from a abstract class and the view instance is generated within the controller using a factory method.
Almost all languages use the controller to send the notification update the views. Like, Yahoo finance, where stock quotes are refreshed automatically. Here the webpage (view) is just a stub of showing the data. Server (Controller) sends out the constant stream of data to the view. Whenever the data is updated controller send it out.
The other is like polling – View at regular time interval sends out the request to controller. Controller keeps tab of if something is changed and asks the model to send out the information. I guess what we try to achieve in ABAP falls in this category.
Many times, we don’t have to constantly update the View. The main purpose of us to use the MVC is seamlessly make changes in the views. Like displaying data in ALV, or File or Smartforms or XML etc.
Since ALV needs to make changes to the data table, we need to link the table directly from the model as a reference. In this case it would make changes directly to the Data itself. Another point of MVC would be, if the data changes in the model, all the attached views needs to be updated. But, most of the time, we have only one view to worry about.
I also use the Data Changed mechanism to let my model know about if the data is changed, but I would say it would be mixture of Observer and MVC (as you have pointed out). Once I did development where I had multiple ALVs. If I make change on the one ALV, I would need to update the data into all other ALVs. So, I used the observer to send out that details.
ALV mechanism makes it difficult to send the information back to the model if you say trigger the event from View. Controller has to trap that and update back the model. In case of ALV, we don’t have clear cut idea when data is being updated. Thus I most of the time use the reference of the model.
Keshav replied back:
I came to know that there is no direct solution while implementing Pure MVC in ABAP. It varies based on requirement. Hope I am right. My requirement is a bit typical. It goes as follows:
Once the report is executed, the model prepares the data initially for different levels. For ex: Level1,2,3,4,5. Here Level1 is the main level and the default view displayed. The other levels are built based on level1( initially in model ). The other levels(views) are displayed based on button click on Level1 ALV tool bar.
Now, the display structure is entirely different from the data retrieved from the model, the level1 consists of buttons, icons, check boxes etc while displayed. Here what I do is, get the data from the model to controller using reference, pass this reference to view, the view itself later de-reference the data and prepares the output data( with icons, buttons etc ). The view has its local event handler for link click, hotspot etc. Also, the view might edit the values displayed( the data is not directly updated in model here, but in view itself )
The controller keeps observing the view for the events raised. for ex: once the data is changed in view level1, the controller hears that, later the event handler method in controller asks the model to change the Level 1 data and sequentially asks the model to refresh the other levels again. Also, the controller displays several popup views based on view events raised.
I replied to Keshav
I faced the same dilemma as you had. My table had few more additional columns specifically for styles (for buttons) and for colors on cell.
I did the same approach as you did – pass the data as reference to view, let view handle the view related massaging on data, send data back to model.
I believe you are on right track. Do let me know if you find any other better option.
I created a internal table, which had 5 include structures, among that one include structure was for buttons, icons, checkboxes etc. The model populates the rest of the fields. Later, I created a method in view called format data. This methods populates the icons, buttons etc to one of the include structure. When an event is handed by controller and when the model is changed, the controller later again calls the format data method of the view to change buttons, check boxes etc. Everything is working fine. Also, I will have to go through your new blog for better options.
Also, I created a abstract view class , which I was able to inherit to many types of views.
If you are using MVC or thinking of using MVC let me know if you have faced any issue while implementing and your remediation of it.