Friday, November 19, 2010

Business Delegate

If user has exposed services as Session façades at business layer and a developer has to call this Session Facade bean at presentation layer say in Application Controller or Servlet then presentation layer developer has to write a lot of repetitive code to access the Session Bean which increase coupling with business layer and duplicate code. In some cases developer at presentation layer doesn’t know how to call or deal with session beans. So to reduce the complexity of handling remote services and coupling developer at presentation layer can use business delegate. A business delegate is like a proxy or shadow POJO object which delegates to remote service, here a Session Façade.
Here we are going to take an example of Session façade bean which has been exposed as service on business layer and will write a Business Delegate POJO for this Session façade bean. So when a developer wants to call this Session Façade he has to just use the Business Delegate object.
Business delegate object can have some access logic like try more than one time to get the services if not available or cache some data which don’t change frequently.
Business delegate should be developed by business layer developers because they know all the functionality of business layer which can help during caching mechanism in business delegate if required.
Generally Business delegate use Service Locator to locate the services and shadow one POJO per Session Façade bean and single method for every exposed method by Session bean.
Below example is a Business Delegate object for Session façade in article http://tiwarij2eeblog.blogspot.com/2010/11/transaction-design-with-session-bean.html
And business delegate also uses the service locator at
We can implement Business Delegate as Singleton
package org.paandav.businessdelegate;

import java.util.List;

import org.paandav.relation.OpAddress;
import org.paandav.relation.OpPerson;
import org.paandav.servicelocator.ServiceLocator;
import org.paandav.session.SessionFacade;

public class SessionFacadeBusinessDelegate {
private static final String SESSIONFACADE_JNDI_KEY_NAME = "ORG.PAANDAV.SESSION.SESSIONFACADE";
private static ServiceLocator sl = null;
private static SessionFacade fac = null;
// This list changes once in month in DB or fixed at businessLayer and
// changes rarely.
private static List<String> dropList = null;

public SessionFacadeBusinessDelegate() throws Exception {
if (sl == null) {
sl = ServiceLocator.getInstance();
fac = (SessionFacade) sl.getObject(SESSIONFACADE_JNDI_KEY_NAME);
if (fac == null) {
throw new Exception("Could not initialize");
}
dropList = fac.getDropList();
}
}

public String sessionFacadeUseCaseOne(OpPerson person, OpAddress[] add)
throws Exception {

return fac.sessionFacadeUseCaseOne(person, add);
}

// No need to call again and again method on session facade bean.
public List<String> getDropList() throws Exception {
return dropList;
}
}

No comments:

Post a Comment