Let us design a Customer BSP application at high level(from 36,000 feet) as shown in the picture below, which has three controllers and corresponding views. Customer list is handled by controller List.do (list.htm) and controller details.do (details.htm) displays the details of selected customer.
Controller Design Highlevel
Main.do (main.htm) is the parent controller which has List.do and details.do as its children. When a record in the list got selected, the details.do controller should get the selected record and fetch the details for display on the page. I am sure there are many ways to exchange information between two subcontrollers, I would like to mention the two methods I have used their advantages and disadvantages.
While developing complex BSP applications that are built with many discrete components (controllers) designed for greater flexibility and reuse, exchanging parameters between controllers becomes crucial and complex. I have seen many asking questions in SDN forums about exchanging data between the controllers. Main.do ZCL_MYAPPL_MAIN List.do ZCL_MYAPPL_LIST Detail.do ZCCL_MYAPPL_DETAIL
Assume that Customer id is the key to get customer details, which is available as part of the row in the list.
When a record is selected, the selection event of the table control is used to capture the selected record and corresponding customer id. Now, this customer id need to be passed to the details.do controller, there is no direct way of accessing the instance of this controller from List.do but through the parent controller main.do
1. Create an attribute CUSTOMERID in main.do
2. In DO_HANDLE_EVENT of LIST.Do capture the row selected event and set the parent controllers attribute CUSTOMERID with customer id of selected row.
3. In DO_INIT_ATTRIBUTES of details.do, get the customer id from parent controller that was set by the list selection event.
Get the customer details based on me->customerid to display in the page. Make sure to clear the parent attribute as soon as you fetched it in detail.
Method 2. (Push Method)
In this method the customerid attribute in details.do can be set during the row select event in the list by referencing the parents subcontrollers with component id of details.do
There is an issue with this method, when the application is started for the first time, only list is displayed and the details.do(details.htm) is not displayed as there is no detail to be displayed(unless it is coded to display blank details page below the list), details.do is not registered as the subcontroller of main and the row selection event will not be able to set the customerid in details.do
Method 3. (Pull Method).
This is implemented in DO_INIT_ATTRIBUTES of details.do
All these above methods can be used to exchange data between parent and subcontrollers, but it is not the best approach as the implementation classes are referred (hard coded) , which is against the basics of Object Oriented Design Patterns .
If I were to call the details.do from another applications (say , Contact. Do) controller, this approach generates a runtime error as the parent is not main.do which was hard coded in the above three methods (lo_main type ref to ZCL_MYAPPL_MAIN).
Part II of this weblog uses Object Oriented Design patterns that gives more flexiblity.