<?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>Fusioncube &#187; AOP</title>
	<atom:link href="http://www.fusioncube.net/index.php/category/development/design-patterns/aop/feed" rel="self" type="application/rss+xml" />
	<link>http://www.fusioncube.net</link>
	<description>The online journey of a technophile, by Steve Brownlee</description>
	<lastBuildDate>Tue, 31 Aug 2010 13:34:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>ColdFusion Object Creation &#8211; More</title>
		<link>http://www.fusioncube.net/index.php/coldfusion-object-creation-more</link>
		<comments>http://www.fusioncube.net/index.php/coldfusion-object-creation-more#comments</comments>
		<pubDate>Fri, 13 Feb 2009 04:51:52 +0000</pubDate>
		<dc:creator>Steve Brownlee</dc:creator>
				<category><![CDATA[AOP]]></category>
		<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[coldspring]]></category>
		<category><![CDATA[design patterns]]></category>

		<guid isPermaLink="false">http://www.fusioncube.net/?p=319</guid>
		<description><![CDATA[Following up on my original post about ColdFusion performance hits on creating objects &#8211; it was suggested that I return an array of typed structures instead of an array of components. I already had a nice AOP class for each of these scenarios, so all I had to do was add the __type__ column to [...]]]></description>
			<content:encoded><![CDATA[<p>Following up on my <a href="http://www.fusioncube.net/index.php/coldfusion-object-creation-performance-hit">original post about ColdFusion performance hits on creating objects</a> &#8211; it was suggested that I return an array of typed structures instead of an array of components.</p>
<p>I already had a nice AOP class for each of these scenarios, so all I had to do was add the __type__ column to the array of structures, and I was good.  The test results below seem logical&#8230; except for the first set of testing 50 rows.  Converting the query results into an array of structs using a simple function call to another component was actually SLOWER than using AOP or converting to DTOs.</p>
<p>Besides that anomaly, the performance hit for using ColdSpring AOP proxies and using DTOs is clearly visible as you add more data.</p>
<p><img src="http://www.fusioncube.net/wp-content/uploads/2009/02/50rows.png" alt="50rows" title="50rows" width="406" height="223" class="alignnone size-full wp-image-320" /><br />
<br/><br/><br />
<img src="http://www.fusioncube.net/wp-content/uploads/2009/02/250rows.png" alt="250rows" title="250rows" width="412" height="221" class="alignnone size-full wp-image-321" /><br />
<br/><br/><br />
<img src="http://www.fusioncube.net/wp-content/uploads/2009/02/500rows.png" alt="500rows" title="500rows" width="420" height="225" class="alignnone size-full wp-image-322" /><br />
<br/><br/><br />
<img src="http://www.fusioncube.net/wp-content/uploads/2009/02/1000rows.png" alt="1000rows" title="1000rows" width="416" height="229" class="alignnone size-full wp-image-323" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.fusioncube.net/index.php/coldfusion-object-creation-more/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ColdFusion Object Creation Performance Hit</title>
		<link>http://www.fusioncube.net/index.php/coldfusion-object-creation-performance-hit</link>
		<comments>http://www.fusioncube.net/index.php/coldfusion-object-creation-performance-hit#comments</comments>
		<pubDate>Thu, 12 Feb 2009 19:14:46 +0000</pubDate>
		<dc:creator>Steve Brownlee</dc:creator>
				<category><![CDATA[AOP]]></category>
		<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[design patterns]]></category>

		<guid isPermaLink="false">http://www.fusioncube.net/?p=310</guid>
		<description><![CDATA[This article shows the numbers in my testing that shows the performance loss when creating large objects in ColdFusion.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d heard other people in the community mention that, while ColdFusion 8 was faster, object creation was notoriously slow.  I never understood what they were talking about as I was creating up to 3000 DTO objects to pass to Flex and was seeing times between 300ms-2000ms for the entire process.</p>
<p>Well, today I discovered what they were talking about.  While architecting a new piece of functionality in one of our apps, I needed to create a DTO that has 57 properties.  Also, the data set for each user averages 1000+ rows (and, thus, 1000+ DTOs created).  The execution times startled me at first.</p>
<p>Here are my notes.</p>
<pre class="code"><code>With 57 Attributes
==============================================
	300 rows
	==============================================
	4438ms for converting to DTOs (100% slower)
	2216ms for no conversion

	1070 rows
	==============================================
	10485ms for converting to DTOs (198% slower)
	3516ms for no conversion

With 20 attributes
==============================================
	300 rows
	==============================================
	2250ms for converting to DTOs (20% slower)
	1875ms for no conversion

	1070 rows
	==============================================
	5735ms for converting to DTOs (144% slower)
	2344ms for no conversion
</code></pre>
<p>I always knew that the number of objects created would always increase time, but I never realized that the size of the object would have such an impact.  In the past, I&#8217;ve always dealt with DTOs that had less than 10 properties.</p>
<p>Now we have to make a hard decision on when, or perhaps how, to implement DTOs when using ColdFusion to send data to our Flex UIs.  We either have to come up with a way to do pagination in our requests (only request 200 rows at a time for large objects), or abandon DTOs altogether and work with the native ArrayCollection type passed back for ColdFusion queries.  I&#8217;d hate to lose my types data objects in Flex, but if the impact is making a user wait for 10 or 20 seconds for his/her data to load, we&#8217;ll have to find an alternative.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fusioncube.net/index.php/coldfusion-object-creation-performance-hit/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>ColdFusion to Flex Object Type Conversion via ColdSpring</title>
		<link>http://www.fusioncube.net/index.php/coldfusion-flex-object-conversion</link>
		<comments>http://www.fusioncube.net/index.php/coldfusion-flex-object-conversion#comments</comments>
		<pubDate>Thu, 02 Oct 2008 19:26:04 +0000</pubDate>
		<dc:creator>Steve Brownlee</dc:creator>
				<category><![CDATA[AOP]]></category>
		<category><![CDATA[cfml]]></category>
		<category><![CDATA[coldspring]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.fusioncube.net/?p=250</guid>
		<description><![CDATA[This article covers using the ColdSpring AOP feature to automatically convert ColdFusion queries to an array of value objects for consumption in Flex.  Using value objects (a.k.a. transfer objects) ensure proper data type conversion when passing data from ColdFusion to Flex.]]></description>
			<content:encoded><![CDATA[<h2>The Value Objects</h2>
<p>Otherwise known as a transfer object is simply a design pattern for exchanging data between disparate systems.  I&#8217;m working on a application with a Flex UI and a ColdFusion backend, and I initially discovered that if I return a query directly to Flex, it is converted into an ArrayCollection populated with simple Objects.</p>
<p>What I wanted was an array of specifically typed objects that I could cast as value objects in Flex.  Here&#8217;s a very simplistic example of an Employee value object in Flex. It has two properties:</p>
<ul>
<li>Employee ID</li>
<li>Employee Name</li>
</ul>
<h3>Flex Employee Value Object</h3>
<pre class="code"><code>package net.fusioncube.transferObjects
{
   [RemoteClass(alias="net.fusioncube.model.employee.Employee")]
   [Bindable]
   public class Employee
   {
      private var _employeeID:uint;
      private var _employeeName:String;

      /* constructor */
      public function Effort(employee_id:uint = 0,
                  employee_name:String = "")
      {
         this._employeeID= employee_id;
         this._employeeName= employee_name;
      }

        public function get employee_id():uint
        {
        	return _employeeID;
        }

        public function set employee_id(value:uint):void
        {
        	if(value != 0) _employeeID= value;
        }

        public function get employee_name():String
        {
        	return _employeeName;
        }

        public function set employee_name(value:String):void
        {
        	if(value != "") _employeeName= value;
        }
   }
}</code></pre>
<p>You may have noticed the <strong>RemoteClass</strong> metadata applied to the Actionscript class.  What this does is ensure that this Flex class maps directly to a ColdFusion component value object.  To make a ColdFusion component value object is simple: you use the &lt;cfproperty&gt; tag for each property and specify each one&#8217;s type.</p>
<p>In this example, I&#8217;ve also added an init() method, whose use we&#8217;ll discover later.</p>
<h3>ColdFusion Employee Value Object</h3>
<pre class="code"><code>&lt;cfcomponent displayname="Employee" output="false"&gt;
   &lt;cfproperty name="employee_id" type="numeric" default="" /&gt;
   &lt;cfproperty name="employee_name" type="string" default="" /&gt;

   &lt;cffunction name="init" access="public" returntype="net.fusioncube.model.employee.Employee" output="false"&gt;
      &lt;cfargument name="employee_id" type="numeric" required="false"  /&gt;
      &lt;cfargument name="employee_name" type="string" required="false"  /&gt; 

      &lt;cfset this['effort_id']       = arguments.effort_id /&gt;
      &lt;cfset this['employee_name']      = arguments.employee_name /&gt;

      &lt;cfreturn this /&gt;
   &lt;/cffunction&gt;

&lt;/cfcomponent&gt;</code></pre>
<h2>Converting a Query to an Employee Array</h2>
<p>Again, as a very simple example, let&#8217;s say I have an EmployeeService component in my service layer and have a method listEmployees() that simply queries the <em>employees</em> table and returns all rows.</p>
<pre class="code"><code>&lt;cffunction name="listEmployees" access="public" output="false" returntype="query"&gt;
   &lt;cfset var effortQuery = "" /&gt;

   &lt;cfquery datasource="#variables.config.getSetting("datasource")#" name="employeeQuery"&gt;
   select e.* from employees e
   &lt;/cfquery&gt;

   &lt;cfreturn effortQuery /&gt;
&lt;/cffunction&gt;</code></pre>
<p>Since I don&#8217;t want to return a query, and I certainly don&#8217;t want to have two methods in every single component &#8211; one that returns a query and one that returns an array of objects &#8211; I use the handy, dandy AOP feature in ColdSpring to apply an advice that does it at runtime. </p>
<h3>QueryToVOAdvice Advice</h3>
<p>In this advice, I simply loop over the query&#8217;s rows and columns to create a simple structure containing the key/value pairs for each row.  It then creates a new component for each query row by passing that structure to the init() function &#8211; which you saw above &#8211; and adds it to an array.</p>
<p>The result is an array of Employee-typed objects.</p>
<pre class="code"><code>&lt;cfcomponent displayname="QueryToVOAdvice" extends="coldspring.aop.MethodInterceptor"&gt;

   &lt;cffunction name="init" returntype="utility.aop.advice.converters.QueryToVOAdvice" access="public" output="false"&gt;
      &lt;cfreturn this /&gt;
   &lt;/cffunction&gt;

   &lt;cffunction name="invokeMethod" access="public" returntype="any"&gt;
      &lt;cfargument name="methodInvocation" type="coldspring.aop.MethodInvocation" required="false" /&gt;

      &lt;cfscript&gt;
      var local = structNew();
      local.queryRow = 1;

      // Create an array to hold the value objects
      local.valueObjectArray = ArrayNew(1);

      // Store the resulting query object from the method execution
      local.queryObject = arguments.methodInvocation.proceed();

      // Loop over the query rows
      for(local.queryRow=1; local.queryRow lte local.queryObject.recordCount; local.queryRow = local.queryRow+1)
      {
         local.voStruct = structNew();

         // Loop over the query columns and create a key/value pair for each value
         // in this row of the query
         for(local.columnNum=1; local.columnNum lte listLen(local.queryObject.columnList); local.columnNum = local.columnNum+1)
         {
            local.columnName = listGetAt(local.queryObject.columnList, local.columnNum);
            local.voStruct[local.columnName] = local.queryObject[local.columnName][local.queryRow];
         }

         // Create an instance of the value object (obtained from the getValueObject method)
         // initializing it with the structure we created.  Add the value object to the array.
         arrayAppend(local.valueObjectArray, createObject('component', arguments.methodInvocation.getTarget().getValueObject()).init(argumentCollection=local.voStruct));
      }

      return local.valueObjectArray;
      &lt;/cfscript&gt;
   &lt;/cffunction&gt;

&lt;/cfcomponent&gt;</code></pre>
<h3>ValueObject Property</h3>
<p>For each of my beans, in this case the Employee bean, I can simply apply an advice in the definition.  For this to work, I create a <strong>valueObject</strong> property in each component.</p>
<pre class="code"><code>&lt;component name="Employee"&gt;
   &lt;cffunction name="setValueObject" access="public" output="false" returntype="void"&gt;
      &lt;cfargument name="valueObject" type="string" required="true" /&gt;
      &lt;cfset variables.valueObject = arguments.valueObject /&gt;
   &lt;/cffunction&gt;

   &lt;cffunction name="getValueObject" access="public" output="false" returntype="string"&gt;
      &lt;cfreturn variables.valueObject /&gt;
   &lt;/cffunction&gt;
&lt;/component&gt;</code></pre>
<h3>Employee Bean Definition</h3>
<p>Now when I define my Employee bean, I specify the type of the value object (the type that gets returned from the init() function), and then apply the advisor (shown below).</p>
<pre class="code"><code>&lt;bean id="Employee" class="coldspring.aop.framework.ProxyFactoryBean"&gt;
   &lt;property name="target"&gt;
      &lt;bean class="net.fusioncube.model.employee.EmployeeService"&gt;
         &lt;property name="valueObject"&gt;
            &lt;value&gt;net.fusioncube.model.employee.Employee&lt;/value&gt;
         &lt;/property&gt;
      &lt;/bean&gt;
   &lt;/property&gt;
   &lt;property name="interceptorNames"&gt;
      &lt;list&gt;
         &lt;value&gt;queryToValueObjectAdvisor&lt;/value&gt;
      &lt;/list&gt;
   &lt;/property&gt;
&lt;/bean&gt;</code></pre>
<h3>QueryToValueObjectAdvisor Definition</h3>
<pre class="code"><code>&lt;bean id="queryToValueObjectAdvisor" class="coldspring.aop.support.RegexMethodPointcutAdvisor"&gt;
   &lt;property name="advice"&gt;
      &lt;bean class="utility.aop.advice.converters.QueryToVOAdvice"/&gt;
   &lt;/property&gt;
   &lt;property name="pattern"&gt;
      &lt;value&gt;^list.*$&lt;/value&gt;
   &lt;/property&gt;
&lt;/bean&gt;</code></pre>
<h3>The Flex Array of Typed Objects</h3>
<p>Here&#8217;s an image of an example array I get back from ColdFusion in one of my current apps.  You can see that each object was automatically converted into a Teammate (equivalent of an Employee) rather than a simple Object.</p>
<p><a href='http://www.fusioncube.net/wp-content/uploads/2008/10/transferobject_array.png'><img src="http://www.fusioncube.net/wp-content/uploads/2008/10/transferobject_array.png" alt="" title="Array of value objects" width="365" height="189" class="alignnone size-full wp-image-251" /></a></p>
<p>Caveat: If you&#8217;re passing back queries with a thousand or more rows, and are running CFMX 7 or older, then this solution may cause serious performance problems as object creation is notoriously &#8220;slow&#8221; in those versions of ColdFusion.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fusioncube.net/index.php/coldfusion-flex-object-conversion/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
