Friday, May 28, 2010

CairnGorm Example, MVC Architecture

1) Service Controller:
------------------------
All USed services is define in service controller
Eg.

"<" ?xml version="1.0" encoding="utf-8"? ">"
"<" cairngorm:ServiceLocator xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cairngorm="com.adobe.cairngorm.business.*" ">"


"<" mx:WebService id="projectService"
wsdl="{ModelURL.API_WEBSERVICE}"
useProxy="false"
showBusyCursor="true" ">"
"<" /mx:WebService ">"



"<" /cairngorm:ServiceLocator ">"


2) Front Controller:
------------------------
All Command are linked with related event in this section for each event it's command is exists and it's preinitialize here.
Eg.

package com.project.control
{
import com.adobe.cairngorm.control.FrontController;
import com.zmarketing.zebra.commands.*;

public class projectController extends FrontController
{
public function projectController()
{

this.addCommand( projectController.GETALLUSER_EVENT, GetAllUserCommand );

}

// Webservice Event
public static const GETALLUSER_EVENT : String = 'GetAllUser';



}
}


2) Event :
------------------------

Event is extend from CairngormEvent and in default constucter it's calling a super class and register here.
Eg.


package com.project.control
{
import com.adobe.cairngorm.control.CairngormEvent;

public final class GetAllUserEvent extends CairngormEvent
{
public function GetAllUserEvent() : void
{
super( projectController.GETALLUSER_EVENT );
}
}
}


2) Command :
------------------------

In Command there is implement two interface ICommand, IResponder which have method of
i. execute - > used while dispatch event at that time initialize delegate and calling delegate function.
ii. result - > When result is comes at that time return in this function.
iii. fault -> Call when any fault while calling.



package com.project.commands
{
import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;
import com.project.business.GetAllUserDelegate;
import com.project.model.ModelLocator;
import com.project.vo.voAddUpdateCampProductIndustryType;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.IResponder;

public class GetAllUserCommand implements ICommand, IResponder
{
private var model : ModelLocator = ModelLocator.getInstance();

public function execute( event : CairngormEvent ) : void
{
var delegate : GetAllUserDelegate = new GetAllUserDelegate( this );
// This is passed for initialize responder which curretly point to
// result function of this command.

delegate. GetAllUser( event );
// event argument is used for gettting event.data for paramenter as well as
// if any extra cairngormHandler apply we get this handler by event.handler
}

public function result( data : Object ) : void
{
if( data.result )
{

}

}

public function fault( info : Object ) : void
{
Alert.show( "GetAllUserCommand.as\n" + info.toLocaleString(), 'Error' );
}
}
}


2) Delegate :
------------------------

Used for calling backend services and applying responder.
Eg.

package com.project.business
{
import com.adobe.cairngorm.business.ServiceLocator;
import com.adobe.cairngorm.control.CairngormEvent;

import mx.rpc.AsyncToken;
import mx.rpc.IResponder;

public class GetAllUserDelegate
{
private var responder : IResponder;
private var service : Object;

public function GetListALLCampProductIndustryTypeDelegate( responder : IResponder )
{
this.service = ServiceLocator.getInstance().getWebService( ServiceConst.servicename ).getOperation( ServiceConst.oprGetAllUser );
// Get service name and operation here

this.responder = responder;
// responder is a related cairngormCommand's responder which call a result function of command while get result.
}

public function GetAllUser( event : CairngormEvent ): void
{
// take AsyncToken for asynchronize calling
// and apply extra CairngormHandler using event.handler.
// you can also get data using event.data if needed to pass parameter.

var token:AsyncToken = this.service.send("paramenters if needed");
token.addResponder( responder );
token.addResponder( event.handler );
}
}

}



Calling Example :
-----------------------

// Create a new Event , apply data on it and if required add extra handler here is getAllUserResultHandler
var getalluser:GetAllUserEvent = new GetAllUserEvent();
getalluser.data = userid;
getalluser.dispatch( new CairngormResponder( getAllUserResultHandler ) );


private function getAllUserResultHandler( event:ResultEvent ) : void
{
// calling when get server side response.
}