Home > Flex > Making use of Cairngorm ValueObjects


Making use of Cairngorm ValueObjects

September 2nd, 2007

When I first started working with Cairngorm I didn’t really see the purpose to ValueObjects. I would just define my variables inside the event like many of the examples out there. Then I found an old blog post at richinternetapps.com and was convinced to give them a shot.

First I defined a simple User ValueObject:

package net.huyler.UserManager.vo
{
    import com.adobe.cairngorm.vo.ValueObject;
    [Bindable]
    public class User implements ValueObject
    {
         public var name:String;
         public var password:String;
    }
}

Next I added the ValueObject to the Event:

package net.huyler.UserManager.events
{
    import com.adobe.cairngorm.control.CairngormEvent;
    import net.huyler.UserManager.control.Controller;
    import net.huyler.UserManager.vo.User;

    public class AuthenticateEvent extends CairngormEvent
    {
    	public static const EVENT_AUTHENTICATE : String =
                "authenticateEvent";

        public var user : User;

        public function AuthenticateEvent( userInput:User )
        {
            super( EVENT_AUTHENTICATE );
            user = userInput;
        }
    }
}

In my command, the execute function doesn’t have to have any knowledge of the ValueObject other than its existance inside the event:

public function execute( event : CairngormEvent ) : void
{
	var delegate : ManagerServiceDelegate =
		new ManagerServiceDelegate( this );
	delegate.authenticate( AuthenticateEvent(event).user );
}

The best part comes inside your view component. You can define the ValueObject in MXML and bind the values to your input fields:

<vo:User id="userValueObject"
    	name="{nameInput.text}"
    	password="{passInput.text}"/>

Then you can dispatch the event without setting each field manually:

private function onLogin():void
{
	var event : AuthenticateEvent =
            new AuthenticateEvent( userValueObject );
	CairngormEventDispatcher.getInstance().
            dispatchEvent( event );
}

So ValueObjects, when used throughout your code, can be a great way to eliminate tedious setting and retrieving of data from events. I’m officially sold on them.



Christopher Flex

  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.