Wednesday, July 24, 2013

SAP CRM and the Model-View-Controller Concept

When working with the CRM webclient platform, you can easily go ahead without knowledge of the so-called Model-View-Controller concept. I think if you want to do it right though, you should at least have some basic knowledge about the concept.
This graphic is explained in the accompanying text
For some of you, the model-view-controller concept might be something you have only heard about in an SAP classroom training or read about an SAP-Press book. For others though, this could be a concept you have learnt about in a non-SAP context.

This article will focus on how the SAP CRM webclient embraces the model-view-controller concept.

I will try to help you understand the concept as well as recognise the different parts in the SAP CRM Webclient. I will also try to show you what would be good and what would be bad practice.

What is the model-view-controller concept?

According to wikipedia, the MVC is 'a software architecture pattern' used in User Interfaces.
In the big picture, it is all about separating responsibilities, further enabling reusability of the software.

The MVC consists of three compontents, surprisingly enough, the Model, the View and the Controller.

The Model is responsible for the data that is shown in the User Interface.
The View is responsible for the layout and the actual look-and-feel of the user interface.
The Controller is often referred to as the 'glue'. The controller is 'in control' of both the view and the model. This is where the magic happens.

If you google on the model view controller, you will find that there are many images explaining the MVC.

I have decided to bother you guys with one from the SAP help page on MVC.
 
 
This graphic is explained in the accompanying text
There are some important rules that you should be aware of in the MVC.
The most important rule is that the View is never allowed to talk to the Model directly. So, when programming in the view, you can not access the data from the model. Luckily enough, the platform helps you out with this, but I will show that to you in a minute. It would also not be logical, but in some cases (if you are a good BSP programmer) it might be tempting...
 

How is this represented in the SAP CRM Webclient?

Let's look at the BSP Component workbench (BSP_WD_CMPWB).
The SAP CRM Webclient consists of a couple of layers.
 
 
The GENIL (Generic Interaction Layer) allows us to consume data from the business layer (basically the 'old core' of SAP CRM, consisting of BAPI's and function modules accessing the database tables.
The BOL (Business Object Layer) uses the data from the GENIL and logically joins the information to understandable business object (such as 'Businesspartner' or 'Service Order').
The MVC that we are talking about in this article is used in the presentation layer and allows the user to interact with the BOL.
 
When looking at the workbench, it can be very easy to be put on the wrong track, because the model-view-controller concept can be seen inside windows and views... I can hear you thinking 'How can the model-view-controller concept be part of a view?'.
 
Basically, the view in the component represents a definition of the model, view and controller. There apparently was not a more logical naming for this than 'View'.
 
If we look at the contents of this view though, we can easily identify which parts of the view definition are part of the model, the view and the controler. Let's take a more detailed look.
 

Controller

Let's start at the controller. This is represented by the *IMPL class in the workbench. This contains for instance the do_prepare_output, do_validate_input, but also for instance all the event handlers.
An important part of the controller is also the do_init_context, which brings us the the model. The model in the workbench is represented by the node Context (the *CTXT class) with it's context nodes (the CN** classes).
 

Model

The Context is initialised by the controller using the do_init_context. The context nodes are then created from the context class using the methods CREATE_CONTEXT_NODES and 'CONNECT_NODES'. Basically, the model is built up by the controller, giving the controller full control over the model.
 
The model nodes contain the definitions of all the fields from the model (the model being a representation of a BOL Object is all about fields). The model does not only know about the contents of these fields, but also about the field properties (such as field type, input-readyness, value helps etc).
 

View

The view is the actual BSP, and though connected to the context nodes (so it knows about the fields that are supposed to be in the screen), it does not know about the contents, nor does it contain any logic for these contents.
 

Additional information

Even though not focussed on SAP, there are also some quite good explanatory video's on youtube on the MVC. For instance, http://youtu.be/W_ikPMoQ8Rc gives a quick short explanation of the concept.

Also, the SAP-PRESS book SAP Web Client: A Comprehensive Guide for Developers gives more information on the Model View Controller.

If you want to know more, you can also check out the CR580 training by SAP Education.
 

No comments:

Post a Comment