<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Christophe Coenraets &#187; Parsley</title>
	<atom:link href="http://coenraets.org/blog/category/parsley/feed/" rel="self" type="application/rss+xml" />
	<link>http://coenraets.org/blog</link>
	<description>Rich Internet Applications, Flex, AIR, Java, Android</description>
	<lastBuildDate>Fri, 23 Jul 2010 14:45:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Using the Flash Builder 4 Data Centric Features with Parsley (and other frameworks)</title>
		<link>http://coenraets.org/blog/2010/05/using-the-flash-builder-4-data-centric-features-with-parsley-and-other-frameworks/</link>
		<comments>http://coenraets.org/blog/2010/05/using-the-flash-builder-4-data-centric-features-with-parsley-and-other-frameworks/#comments</comments>
		<pubDate>Wed, 05 May 2010 13:59:13 +0000</pubDate>
		<dc:creator>Christophe Coenraets</dc:creator>
				<category><![CDATA[BlazeDS]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[LCDS]]></category>
		<category><![CDATA[Parsley]]></category>

		<guid isPermaLink="false">http://coenraets.org/blog/?p=673</guid>
		<description><![CDATA[I have blogged about the new data-centric features in Flash Builder 4 here and here. One question people often ask me is: “Can I use this feature if I use a Framework (Cairngorm, Mate, Parsley, Spring ActionScript, Swiz, etc)?” The answer is yes: The classes generated by Flash Builder (Value Objects and Service Stubs) are [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://coenraets.org/apps/contacts/contacts.jpg" alt="" /></p>
<p>I have blogged about the new data-centric features in Flash Builder 4 <a href="http://coenraets.org/blog/2010/03/flex-4-sample-application-using-a-java-back-end-blazeds-4-and-flash-builder-4-data-wizards/">here </a>and <a href="http://coenraets.org/blog/2010/03/flex-4-sample-application-using-a-java-back-end-blazeds-4-and-flash-builder-4-data-wizards/">here</a>. One question people often ask me is: “Can I use this feature if I use a Framework (Cairngorm, Mate, Parsley, Spring ActionScript, Swiz, etc)?”</p>
<p>The answer is yes: The classes generated by Flash Builder (Value Objects and Service Stubs) are standard building blocks that are part of most RIA design patterns, regardless of whether or not you use a framework.</p>
<p>The way you leverage these classes in your application may differ slightly depending on the framework you are using. For example, each framework may have a different approach to configure service endpoints or to instantiate generated service stubs.</p>
<p>To demonstrate this, I built a version of my “Contacts” application (also known as InSync) using the Flash Builder 4 data-centric features and the <a href="http://www.spicefactory.org/parsley/">Parsley Framework</a>.</p>
<p><span id="more-673"></span><br />
<strong>Configuring Service Endpoints</strong></p>
<p>By default, an application built using a BlazeDS or LCDS project determines its service endpoints using a combination of assumptions made at runtime and hardcoded references baked into the swf at compile time. Read my <a href="http://coenraets.org/blog/2009/03/externalizing-service-configuration-using-blazeds-and-lcds/">Externalizing Service Configuration</a> blog post for more information on this topic. The bottom line is that this default behavior is insufficient for any application other than a simple demo: you want endpoints to be fully configurable so that you can move services around without having to recompile your application.</p>
<p>Using Parsley, I inject the service endpoint configuration (in the form of a ChannelSet) in the generated service stubs as follows:</p>
<pre class="brush: jscript;">
public class ContactService extends _Super_ContactService
{
	[Inject]
	override public function set channelSet(cs:ChannelSet):void
	{
		super.channelSet = cs;
	}
}
</pre>
<p>Note that I added this code in ContactService.as and not _Super_ContactService.as. Contrary to _Super_ContactService.as, ContactService.as will never be regenerated and is therefore the right place to add your custom code.</p>
<p>The injected ChannelSet is defined in my own channels-config.xml configuration file as follows:</p>
<pre class="brush: xml;">
&lt;object type=&quot;mx.messaging.ChannelSet&quot;&gt;
 	&lt;property name=&quot;channels&quot;&gt;
    		&lt;array&gt;
		    	&lt;object type=&quot;mx.messaging.channels.AMFChannel&quot;&gt;
		        	&lt;property name=&quot;uri&quot; value=&quot;http://localhost:8400/testdrive/messagebroker/amf&quot;/&gt;
		        &lt;/object&gt;
	        &lt;/array&gt;
	&lt;/property&gt;
&lt;/object&gt;
</pre>
<p>Note: Unlike the BlazeDS/LCDS services-config.xml file, channels-config.xml is read at runtime.</p>
<p>Because the ChannelSet is configured in an xml file, you can easily change the channel’s endpoint URI without recompiling the application. You could also add failoverURIs to this channel, or add completely new fall back channels to the ChannelSet.</p>
<p><strong>Instantiating Service Stubs</strong></p>
<p>As we already alluded to, Parsley is a dependency injection framework: Instead of letting a component instantiate its dependencies, those dependencies are injected in the component at runtime. For example, in ContactForm, instead of instantiating a ContactService as follows:</p>
<pre class="brush: xml;">
&lt;services:ContactService/&gt;
</pre>
<p>… I simply defined a contactService reference variable that I annotated with [Inject] to let Parsley inject a ContactService instance a runtime.</p>
<pre class="brush: jscript;">
[Inject]
public var contactService:ContactService
</pre>
<p>The instance of ContactService that Parsley injects is defined in Config.mxml and has itself been injected with a ChannelSet. Object configuration in Parsley is very flexible. In this application I combine the xml and mxml configuration options. I use the xml configuration option for the things I don’t want to hardcode in my application, and the mxml configuration for traditional object instantiation and configuration.</p>
<p>Note: Parsley doesn’t force you to use any specific design pattern. For simplicity in this sample application, I inject a service directly into the view. You can of course use other patterns (like the Presentation Model) to achieve a greater level of abstraction. The Dependency Injection and Messaging infrastructure of Parsley will make the implementation of these patterns easier.</p>
<h2>Installation Instructions</h2>
<ol>
<li><a href="https://www.adobe.com/cfusion/entitlement/index.cfm?e=lc_blazeds">Download</a> and install BlazeDS 4</li>
<li>Make a copy the blazeds web application in blazeds/tomcat/webapps, and call the new web application contacts</li>
<li>Download <a href="http://coenraets.org/apps/contacts/contacts.zip">contacts.zip</a>, and unzip it anywhere on your file system.</li>
<li>Overlay contacts/webapps/contacts (from contacts.zip) on top of blazeds/webapps/contacts</li>
<li>Start the server</li>
<li>Open a Command Window or Shell, navigate to /blazeds/tomcat/bin, and start Tomcat (for instance: catalina run).</li>
<li>Open a browser and access <a href="http://localhost:8400/contacts/ContactsParsley/ContactsParsley.html">http://localhost:8400/contacts/ContactsParsley/ContactsParsley.html</a>.</li>
</ol>
<p>Note: The web application also include a plain (framework-less) version of the application available at this URL: <a href="http://localhost:8400/contacts/Contacts/Contacts.html">http://localhost:8400/contacts/Contacts/Contacts.html</a></></p>
<h2>Importing the projects in Flash Builder 4</h2>
<ol>
<li>In Flash Builder 4, click <strong>File > Import > General > Existing Projects into Workspace</strong>.</li>
<li>Specify <strong>contacts/projects</strong> as the root directory and click finish.</li>
<li>Explore the projects: Contacts is the Flex project for the plain version of the application, ContactsParsley is the version discussed in this article, and java-testdrive is the Java project for the server-side classes.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://coenraets.org/blog/2010/05/using-the-flash-builder-4-data-centric-features-with-parsley-and-other-frameworks/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Building a Flex Application with the Parsley Framework</title>
		<link>http://coenraets.org/blog/2009/07/building-a-flex-application-with-the-parsley-framework/</link>
		<comments>http://coenraets.org/blog/2009/07/building-a-flex-application-with-the-parsley-framework/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 20:47:24 +0000</pubDate>
		<dc:creator>Christophe Coenraets</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Parsley]]></category>

		<guid isPermaLink="false">http://coenraets.org/blog/?p=122</guid>
		<description><![CDATA[After my recent explorations of “Swiz”, and “Spring ActionScript” (1,2,3), I decided to take the new version of the Parsley framework for a test drive, and build the Parsley version of inSync: the simple Contact Management application I often use to try out and demonstrate different features and techniques in Flex and Adobe AIR. Parsley [...]]]></description>
			<content:encoded><![CDATA[<p>After my recent explorations of “<a href="http://coenraets.org/blog/2009/02/sample-application-using-the-swiz-framework-and-blazeds/">Swiz</a>”, and “Spring ActionScript” (<a href="http://coenraets.org/blog/2009/03/the-%E2%80%9Cspring-actionscript%E2%80%9D-framework-%E2%80%93-part-1-the-basics/">1</a>,<a href="http://coenraets.org/blog/2009/03/the-spring-actionscript-framework-%E2%80%93-part-2-autowiring/">2</a>,<a href="http://coenraets.org/blog/2009/03/the-spring-actionscript-framework-part-3-injecting-services-and-mock-services/">3</a>), I decided to take the new version of the <a href="http://www.spicefactory.org">Parsley framework</a> for a test drive, and build the Parsley version of inSync: the simple Contact Management application I often use to try out and demonstrate different features and techniques in Flex and Adobe AIR.</p>
<p><img src="http://coenraets.org/apps/insyncparsley/parsley.png"/></p>
<p>Parsley is primarily a Dependency Injection framework. It also offers a very interesting messaging infrastructure. I won’t get into the details of dependency injection here: My previous Swiz and Spring ActionScript articles provide some background information, and there are also plenty of detailed resources out there.</p>
<p>If you just want to run the application and dive into the code, here are the links:</p>
<ul>
<li>Click <a href="http://www.coenraets.org/apps/insyncparsley/main.html">here</a> to run the application.</li>
<li>Click <a href="http://www.coenraets.org/apps/insyncparsley/srcview/index.html">here</a> to view the source code.</li>
</ul>
<h2>Object Configuration</h2>
<p>Parsley allows you to configure the core objects of your application (the objects other components depend on) in MXML (like Swiz), in XML (like Spring ActionScript), in ActionScript, or using any combination of these approaches. You can also extend the framework and create your own configuration mechanism.</p>
<p>For the inSync application, I used a simple MXML configuration (Config.mxml) defined as follows:</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;Object xmlns=&quot;*&quot; xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; xmlns:services=&quot;insync.parsley.services.*&quot;&gt;

    &lt;mx:RemoteObject
            id=&quot;contactRO&quot;
            destination=&quot;contacts&quot;
            endpoint=&quot;http://localhost:8400/samples/messagebroker/amf&quot;
            showBusyCursor=&quot;true&quot; /&gt;

    &lt;services:ContactService /&gt;

&lt;/Object&gt;
</pre>
<p>Note: In a real life application, you would not want to hardcode the endpoints of your services in your source code. You could use the XML configuration option to configure your endpoints in a more “externalized” fashion. More on this topic <a href="http://coenraets.org/blog/2009/03/externalizing-service-configuration-using-blazeds-and-lcds/">here</a>.</p>
<h2>Framework Initialization</h2>
<p>You initialize the Parsley framework using the ContextBuilder class corresponding to the configuration approach you chose. Here is the main application file for the inSync application. Parsley is initialized in the preinitialize event of the application.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; xmlns:views=&quot;insync.parsley.views.*&quot;
	paddingTop=&quot;0&quot; paddingLeft=&quot;0&quot; paddingRight=&quot;0&quot; paddingBottom=&quot;0&quot;
	preinitialize=&quot;FlexContextBuilder.build(Config)&quot;&gt;

	&lt;mx:Script&gt;
		&lt;![CDATA[
			import org.spicefactory.parsley.flex.FlexContextBuilder;
		]]&gt;
	&lt;/mx:Script&gt;

	&lt;mx:Style source=&quot;styles.css&quot; /&gt;

	&lt;views:MainView /&gt;

&lt;/mx:Application&gt;
</pre>
<p><span id="more-122"></span></p>
<h2>Dependency Injection</h2>
<p>Parsley supports constructor injection, method injection, and property injection (by type or by id).</p>
<p>In the following example (ContactForm.mxml), the framework will inject a specific implementation of the IContactService interface. In this case, the ContactService instance created in Config.mxml will be injected. This is an example of injection by type. Coding to the interface allows us to keep the view decoupled from a specific implementation of IContactService.</p>
<p>Note: Parsley doesn’t force you to use any specific design pattern. For simplicity in this sample application, I inject a service directly into the view. The use of an interface provides some level of decoupling, but you can of course use other patterns (like the Presentation Model) to achieve a greater level of abstraction. The Dependency Injection and Messaging infrastructure of Parsley will make the implementation of these patterns easier.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Canvas xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
	addedToStage=&quot;dispatchEvent(new Event('configureIOC', true))&quot;&gt;

	&lt;mx:Script&gt;
		&lt;![CDATA[

		import insync.parsley.model.Contact;
		import insync.parsley.services.IContactService;

		[Inject]
		public var service:IContactService;

		[Bindable]
		public var contact:Contact;

		private function save():void
		{
			contact.firstName = firstName.text;
			contact.lastName = lastName.text;
			contact.email = email.text;
			service.save(contact);
		}

		private function remove():void
		{
			service.remove(contact);
		}

		]]&gt;
	&lt;/mx:Script&gt;

	&lt;mx:Form&gt;
		&lt;mx:FormItem label=&quot;Id&quot;&gt;
			&lt;mx:TextInput text=&quot;{contact.id}&quot; enabled=&quot;false&quot;/&gt;
		&lt;/mx:FormItem&gt;
		&lt;mx:FormItem label=&quot;First Name&quot;&gt;
			&lt;mx:TextInput id=&quot;firstName&quot; text=&quot;{contact.firstName}&quot;/&gt;
		&lt;/mx:FormItem&gt;
		&lt;mx:FormItem label=&quot;Last Name&quot;&gt;
			&lt;mx:TextInput id=&quot;lastName&quot; text=&quot;{contact.lastName}&quot;/&gt;
		&lt;/mx:FormItem&gt;
		&lt;mx:FormItem label=&quot;Email&quot;&gt;
			&lt;mx:TextInput id=&quot;email&quot; text=&quot;{contact.email}&quot;/&gt;
		&lt;/mx:FormItem&gt;
	&lt;/mx:Form&gt;

	&lt;mx:HBox left=&quot;12&quot; bottom=&quot;12&quot;&gt;
		&lt;mx:Button label=&quot;Save&quot; click=&quot;save()&quot;/&gt;
		&lt;mx:Button label=&quot;Delete&quot; click=&quot;remove()&quot;/&gt;
	&lt;/mx:HBox&gt;

&lt;/mx:Canvas&gt;
</pre>
<p>Note that in order to get its dependencies injected when it’s added to the stage, a view has to dispatch the “configureIOC” event.</p>
<h2>Messaging</h2>
<p>Parsley provides a generic Messaging infrastructure that allows you to exchange messages between objects in a decoupled, flexible, and easy-to-use manner:</p>
<ul>
<li>The event dispatcher and the event handler don’t know about each other.</li>
<li>The event dispatcher and the event handler are decoupled from the framework: You dispatch events the Flex way.</li>
</ul>
<p>Let’s look at the Search feature inSync as an example:</p>
<ul>
<li>The Toolbar class includes a simple TextInput that allows the user to specify a search criterion.</li>
<li>The ContactList class displays the Search Results in a DataGrid.</li>
</ul>
<p>The Toolbar class is defined as follows (non essential code removed for clarity):</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Canvas xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; width=&quot;100%&quot;
	addedToStage=&quot;dispatchEvent(new Event('configureIOC', true))&quot;&gt;

	&lt;mx:Metadata&gt;
		[ManagedEvents(&quot;search&quot;)]
	&lt;/mx:Metadata&gt;

	&lt;mx:Script&gt;
		&lt;![CDATA[
			import insync.parsley.events.SearchEvent;
		]]&gt;
	&lt;/mx:Script&gt;

	&lt;mx:Label text=&quot;Search:&quot; top=&quot;18&quot; right=&quot;164&quot;/&gt;

	&lt;mx:TextInput id=&quot;searchBox&quot; right=&quot;0&quot; top=&quot;16&quot;
		change=&quot;dispatchEvent(new SearchEvent(SearchEvent.SEARCH, searchBox.text))&quot;/&gt;

&lt;/mx:Canvas&gt;
</pre>
<p>Note that it dispatches a SearchEvent when the content of the search input field changes.</p>
<p>That event is defined as a managed event (using the ManagedEvents annotation), meaning that the framework will make sure that all the parties interested in that event will receive it.</p>
<p>How do you register as a listener for a managed event? You simply annotate a function with [MessageHandler], and it automatically becomes an event handler for that event, regardless of the display object hierarchy or whether the event bubbles or not.</p>
<p>Here is how it&#8217;s done in ContactList (non essential code removed for clarity):</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Canvas xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; width=&quot;100%&quot; height=&quot;100%&quot;
	addedToStage=&quot;dispatchEvent(new Event('configureIOC', true))&quot;&gt;

	&lt;mx:Script&gt;
		&lt;![CDATA[

			import mx.controls.Alert;
			import mx.collections.ArrayCollection;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.AsyncToken;
			import mx.rpc.AsyncResponder;
			import mx.rpc.events.ResultEvent;

			import insync.parsley.events.SearchEvent;
			import insync.parsley.services.IContactService;			

			[Inject]
			[Bindable]
			public var service:IContactService;

			[Bindable]
			public var contacts:ArrayCollection;

			[MessageHandler]
			public function searchHanlder(event:SearchEvent):void
			{
				service.getContactsByName(searchStr).addResponder(new AsyncResponder(getContacts_result, faultHandler));
			}

			private function getContacts_result(event:ResultEvent, token:AsyncToken):void
			{
				contacts = event.result as ArrayCollection;
			}	

			private function faultHandler(event:FaultEvent):void
			{
				Alert.show(event.fault.faultString);
			}		

		]]&gt;
	&lt;/mx:Script&gt;

	&lt;mx:DataGrid id=&quot;dg&quot; dataProvider=&quot;{contacts}&quot; width=&quot;100%&quot; top=&quot;0&quot; left=&quot;0&quot; right=&quot;0&quot; bottom=&quot;0&quot;/&gt;

&lt;/mx:Canvas&gt;
</pre>
<p>With the simple [MessageHandler] annotation, the handler will be invoked every time an event of the function argument’s data type is dispatched in the application. If you need finer control, you can use the selector notation to specify the event type. For example in MainView.mxml, we register the contactDeleted function as a handler for the ContactEvent.DELETED event as follows.</p>
<pre class="brush: jscript;">
[MessageHandler(selector=&quot;contactDeleted&quot;)]
public function contactDeletedHandler(event:ContactEvent):void
{
}
</pre>
<p>There is more to the Messaging capabilities of the Parsley framework. For example, you can use [MessageBinding] to bind a property of an object to the property of an event.</p>
<pre class="brush: jscript;">
[MessageBinding(messageProperty=&quot;result&quot;,type=&quot; mx.rpc.events.ResultEvent&quot;)]
public var contacts:ArrayCollection;
</pre>
<h2>Installation Instructions</h2>
<ol>
<li>Download the project <a href="http://www.coenraets.org/apps/insyncparsley/srcview/insync-parsley.zip">here</a></li>
<li>Import it in Flex Builder</li>
<li>Run the application. The application is configured to use the MockService (ContactServiceMock) by default, so it will run without any back-end.</li>
</ol>
<p>If you want to use the Remoting service instead of the MockService:</p>
<ol>
<li>Replace &lt;services:ContactServiceMock/&gt; with &lt;services:ContactServiceRemote/&gt; in Config.mxml</li>
<li>Install the <a href="http://opensource.adobe.com/blazeds">BlazeDS turnkey server</a>.</li>
<li>Download <a href="http://coenraets.org/apps/insyncparsley/insync-parsley-java.zip">insync-parsley-java.zip</a>, and unzip it on your local file system.</li>
<li>Copy classes/insync to blazeds/tomcat/webapps/samples/WEB-INF/classes/insync.</li>
<li>Add the following destination to blazeds/tomcat/webapps/samples/WEB-INF/flex/remoting-config.xml:</li>
<p><pre class="brush: xml;">
&lt;destination id=&quot;contacts&quot;&gt;
        &lt;properties&gt;
            &lt;source&gt;insync.dao.ContactDAO&lt;/source&gt;
            &lt;scope&gt;application&lt;/scope&gt;
        &lt;/properties&gt;
&lt;/destination&gt;
</pre>
</p>
<li>Copy insync/sampledb/insync to blazeds/sampledb/insync</li>
<li>Edit server.properties in blazeds/sampledb, and modify the file as follows to add the insync database to the startup procedure.
<p><pre class="brush: jscript;">
server.database.0=file:flexdemodb/flexdemodb
server.dbname.0=flexdemodb
server.database.1=file:insync/insync
server.dbname.1=insync
server.port=9002
server.silent=true
server.trace=false
</pre>
</p>
</li>
<li>Start the database (startdb.bat or startdb.sh)</li>
<li>Start BlazeDS</li>
<li>Run the application</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://coenraets.org/blog/2009/07/building-a-flex-application-with-the-parsley-framework/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
