Prana enabled Cairngorm Store

ActionScript, Cairngorm, Flex, Inversion of Control, Prana 8 Comments »

Following up on Douglas McCarroll's modified Cairngorm Store update, I thought I'd take this a step further and add a Prana application context to configure the business delegates and the service locator. This example has support for mock delegates that contain hardcoded local data and is also able to connect to php services using Remote Objects and AMFPHP.

This examples replaces the older Cairngorm Store example that was configured to work with AMF0 and Renaun Erickson's RemoteObjectAMF0. It now uses the latest beta version of AMFPHP and the AMF3 protocol. The services are hosted on this domain so you don't need to setup AMFPHP in order to see this in action.

The code is currently in svn and can be checked out at http://prana.svn.sourceforge.net/viewvc/prana/trunk/samples/PranaCairngormStore/. This sample will also be included in the next release.

Here's a snippet from the readme file:

***

The following files have been added/changed in order to add Prana configuration support for business delegates and service locator:

* Main.mxml: added application context loader and forced compilation of configured classes

package com.adobe.cairngorm.samples.store.business
* BaseBusinessDelegateMock.as: implements IResponderAware, removed responder constructor argument
* CreditCardDelegate.as: extends AbstractRemoteObjectDelegaten, implements ICreditCardDelegate
* CreditCardDelegateMock.as: implements ICreditCardDelegate
* ICreditCardDelegate.as: interface for credit card delegates
* IProductDelegate.as: interface for product delegates
* ProductDelegate.as: extends AbstractRemoteObjectDelegaten, implements IProductDelegate
* ProductDelegateMock.as: implements IProductDelegate

package com.adobe.cairngorm.samples.store.commands
* GetProductsCommand.as: added delegate lookup via ShopModelLocator
* ValidateCreditCardCommand.as: added delegate lookup via ShopModelLocator

package com.adobe.cairngorm.samples.store.model
* ShopModelLocator.as: added creditCardDelegate and productDelegate variables to configure business delegates

***

As a result, we can now lookup the implementation of our business delegates in the commands instead of instantiating new ones. The commands are completely unaware of the implementation of the business delegates. One note though: since we are not creating new delegate instances, we need to set the responder as a property of the delegate before calling its methods.

Actionscript:
  1. var delegate:IProductDelegate = ShopModelLocator.getInstance().productDelegate;
  2. delegate.responder = this;
  3. delegate.getProducts();

Here's how the application is configured in the application context to use the mock delegates:

XML:
  1. <object id="shopModelLocator" class="com.adobe.cairngorm.samples.store.model.ShopModelLocator" factory-method="getInstance">
  2.     <property name="creditCardDelegate">
  3.         <object class="com.adobe.cairngorm.samples.store.business.CreditCardDelegateMock"/>
  4.     </property>
  5.     <property name="productDelegate">
  6.         <object class="com.adobe.cairngorm.samples.store.business.ProductDelegateMock"/>
  7.     </property>
  8. </object>

And here's how the remote object delegates and the service locator are configured:

XML:
  1. <object id="endPoint" class="String">
  2.     <constructor-arg value="http://www.herrodius.com/amfphp/gateway.php"/>
  3. </object>
  4.  
  5. <object id="serviceLocator" class="org.pranaframework.cairngorm.CairngormServiceLocator" factory-method="getInstance">
  6.     <property name="productService">
  7.         <object class="mx.rpc.remoting.mxml.RemoteObject">
  8.             <property name="destination" value="GenericDestination"/>
  9.             <property name="endpoint">
  10.                 <ref>endPoint</ref>
  11.             </property>
  12.             <property name="source" value="com.adobe.cairngorm.samples.store.business.ProductService"/>
  13.         </object>
  14.     </property>
  15.     <property name="creditCardService">
  16.         <object class="mx.rpc.remoting.mxml.RemoteObject">
  17.             <property name="destination" value="GenericDestination"/>
  18.             <property name="endpoint">
  19.                 <ref>endPoint</ref>
  20.             </property>
  21.             <property name="source" value="com.adobe.cairngorm.samples.store.business.CreditCardService"/>
  22.         </object>
  23.     </property>
  24. </object>
  25.  
  26. <object id="shopModelLocator" class="com.adobe.cairngorm.samples.store.model.ShopModelLocator" factory-method="getInstance">
  27.     <property name="creditCardDelegate">
  28.         <object class="com.adobe.cairngorm.samples.store.business.CreditCardDelegate"/>
  29.     </property>
  30.     <property name="productDelegate">
  31.         <object class="com.adobe.cairngorm.samples.store.business.ProductDelegate"/>
  32.     </property>
  33. </object>


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

Prana 0.3 released!

ActionScript, Cairngorm, Flex, Prana 1 Comment »

I'm proud to announce that Prana 0.3 has been released.

Some of the key features and updates:

  • major update to the core IoC container
  • support for templates in the application context (blog post)
  • support for initializing objects in the application context (Log example)
  • compliancy with Spring application contexts
  • event sequencing in Cairngorm (blog post)
  • several utilities

In the coming days, I'll post some more about the new features.

General info: http://www.pranaframework.org
Download: SourceForge Download Page

Changes in version 0.3 (24.11.2007)
-----------------------------------

General
* added Movie sample application based on article by Martin Fowler
* improved documentation
* moved Cairngorm related classes to org.pranaframework.cairngorm
* Cairngorm Store sample now has different configuration of service locator

Package org.pranaframework.cairngorm
* added support for event sequencing
* CairngormFrontController now checks for commands implementing the ICommands interface
* implementation of CairngormServiceLocator

Package org.pranaframework.errors
* added ClassNotFoundError for retrieving a class by name when class was not found

Package org.pranaframework.ioc
* added serializer to create an application context from an object
* added support for initializing object with IInitializingObject
* added support for templates in application context
* added support for <map> definitions, parsed to Dictionary
* added support for <list> definitions, parsed to ArrayList
* made container spring context compliant
* improved error handling when creating object from object definitions
* rewrote preprocessing of xml application context, now using chain of preprocessors
* fixed creation of object without id, id is now auto generated
* fixed order for object definitions so that they no longer need to be chronological

Package org.pranaframework.reflection
* added metadata introspection
* added "getFullName" method to Method class
* changed arguments in "invoke" method from ... to array

Package org.pranaframework.utils
* added "forName" method in ClassUtils for retrieving Class objects via class names
* added "getParentClass" method in ClassUtils that returns the parent class of a given class
* added "isSubclassOf" method in ClassUtils that returns if a class extends another class
* added StringUtils
* added DisplayObjectContainerUtils
* added MethodInvoker to support initializing objects from application context
* added "state" method to Assert class
* changed TypeConverter so that it can return Class objects


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

Tony Hillerson on Cairngorm Business Delegates with Prana

ActionScript, Cairngorm, Design Patterns, Flex, Prana 2 Comments »

In case you haven't read this, Tony Hillerson has posted an article on configuring a Business Delegate Factory for Cairngorm with Prana. This allows you to quickly switch between different implementations for testing or production purposes.

Read it at http://thillerson.blogspot.com/2007/10/delegate-factories-with-prana.html

It's good to see that the community is picking up Prana and benefits from it. Keep on supporting the project guys, thx!


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

Chaining commands in Cairngorm with Prana

ActionScript, Cairngorm, Flex, Prana 13 Comments »

Problem

We want to be able to execute commands in a sequence, where each command needs to be executed after the previous command has finished. This can be right after a call to execute() in case the command only implements ICommand, or after the invocation of the result() or fault() method, in case the command implements the IResponder interface and fetches some remote data.

Read the rest of this entry »


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

Adding a check for ICommand to addCommand() in Cairngorm

ActionScript, Cairngorm, Design Patterns, Flex, Prana 2 Comments »

Once in a while, when adding event/command mappings in Cairngorm's frontcontroller, I mistakingly add an event instead of a command class to the addCommand() method. Here's an example:

Actionscript:
  1. addCommand(UserEvent.LOAD, LoadUserEvent);

while it really should be:

Actionscript:
  1. addCommand(UserEvent.LOAD, LoadUserCommand);

This is just a mistake when typing fast and with code completion but it can take a while until you find out what you did wrong. To prevent this, I decided to override addCommand() in Prana's CairngormFrontController and add in a check to see if the commandRef argument actually is an implementation of ICommand.

You can checkout this code from Prana's SVN if you like, or you can build a custom version of Cairngorm with the following code:

Actionscript:
  1. public function addCommand(commandName:String, commandRef:Class, useWeakReference:Boolean = true ):void {
  2.   if (commands[ commandName ] != null )
  3.     throw new CairngormError( CairngormMessageCodes.COMMAND_ALREADY_REGISTERED, commandName );
  4.  
  5.   if (null == commandRef) {
  6.     throw new Error("The commandRef argument cannot be null");
  7.   }
  8.   else {
  9.     var classDescription:XML = describeType(commandRef) as XML;
  10.     var implementsICommand:Boolean = (classDescription.factory.implementsInterface.(@type == getQualifiedClassName(ICommand)).length() != 0);
  11.     if (!implementsICommand)
  12.       throw new Error("The commandRef argument '" + commandRef + "' should implement the ICommand interface");
  13.   }
  14.  
  15.   commands[ commandName ] = commandRef;
  16.   CairngormEventDispatcher.getInstance().addEventListener( commandName, executeCommand, false, 0, useWeakReference );
  17. }

This should save you some time in the future.

On a side note, I have been working on an alternative approach to chaining events/commands in Cairngorm. The code is already in Prana's SVN but I haven't had the time to blog about this. Check it out if you're interested and mail me if you want a code example. In the meantime, I'll work a blogpost with an example on how to use it and will publish this by the end of the week.

Happy coding!


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login