Tuesday, January 26, 2016

MAF: Invoking WebServices (Which way to choose)

In MAF there are actually multiple ways to create UI based on webservice.

Your choices are
1. If service is SOAP based, you can create a datacontrol and use it directly to build UI
2. If service is SOAP based, you can create a datacontrol, Call datacontrol from POJO using ADFmfJavaUtilities.invokeDataControlMethod, Expose POJO as datacontrol and use it to build UI.
3. If service is REST based and output in XML, you can create a datacontrol and use it directly to build UI.
4. If service is REST based and output is XML, you can create a datacontrol, call datacontrol from POJO using ADFmfJavaUtilities.invokeDataControlMethod, expose POJO as datacontrol and use it to build UI.
5. If service is REST based and output is JSON, you can create a POJO, call webservice directly using RESTServiceAdapter class. Expose POJO as datacontrol and use it to build UI.

Below diagram depicts above mentioned choices a flow

Although we have multiple choices to invoke webservices, but I only go with two approaches. It reduces my confusion and provide me best options.

1. I do not drag and drop webservice datacontrol on UI. It creates few problems
     a. I lose ability to pre-process/validate/post-process/cache-result capabilities. These capabilities comes only if we invoke webservice datacontrol using POJO. So my preferred way is always have a POJO wrapper around service.

   b. If you directly drag and drop webservice datacontrol on UI, it may get executed more frequently than you want. At times you need to provide logic on UI rendered property or binding iterator to stop auto execution of webservice. It complicates thing.

  So my first suggestion do not drag and drop webservice datacontrol directly on UI, instead wrap it around a POJO and expose POJO as datacontrol. Inside POJO you can do multiple things and invoke service using AdfmfJavaUtilities.InvokeDataControlMethod if needed.

2. For REST service I do not go datacontrol way at all. I prefer simple POJO with RESTServiceAdapter class.
   a. If REST is XML based I have ability to create a datacontrol and then invoke it using AdfmfJavaUtilities but I do not prefer this approach. Its like completely defining your REST service at client side, what is possible url, what are output their attributes etc. If anything changes, I need to recreate datacontrol and it may be lots of testing again. But if I use RESTServiceAdapter, its either no impact or minimal impact on my code.


Effectively my way of invoking services in MAF is pretty straigt forward
1. If service is SOAP
       a. Create SOAP datacontrol
       b. Create POJO and use AdfmfJavaUtilities class to invoke webservice. Also implement pre-processing/validation-of-input/post-processing/caching-of-result logic in POJO.
       c. Expose POJO as datacontorl
       d. Create UI using POJO datacontrol

2. If service is REST
     a. Create POJO (No need to create datacontrol using REST service)
     b. Use RESTServiceAdapter class to invoke rest service from POJO. Also implement pre-processing/validation-of-input/post-processing/caching-of-result logic in POJO.
     c. Expose POJO as datacontrol
     d. Create UI using POJO datacontrol


Above two approaches are my preferred way.