Transfer and Flex: Part 1

I started a project a couple of months ago that has and is going to continually have a very complex and growing architecture. The technology stack in place is Flex 3, Cairngorm 2.2, ColdFusion 8, ColdSpring, Transfer, and SQL Server 2005. This is my first Flex application that will be using Transfer to handle the database abstraction layer. Normally, I would use Brian Rinadi's Illudium PU-36 CFCGenerator to handle the initial generation of all my CFCs and from there would either modify the XSLT files or simply append to the generated CFCs as I saw fit.

The problem with this type of approach lies namely in the early stages of application design and development (especially since I traditionally don't go through a line of specs working on a solo project) when the database is going through many changes. Each time you make a change to the database, you either have to regenerate your files which will overwrite any changes you made, or you have to manually hand-code the database changes. On top of this, it doesn't take care of any of your table relationships as per a 3rd normal form database structure. You then have to design your own home-brew setup of such a system if you require it, or simply provide functions that return the structs/VOs to Flex as you need them by manually building up the hierarchical tree of the model.

For this project, I knew from the beginning that the database schema was going to be pretty complex and that it was also going to be going through changes as I developed and tried things out. I knew that I wanted to get Transfer to work with Flex. I had tried this once in the past and failed because I was designing Flex applications differently at that time. I now know better to design with a service layer in mind.

Although, it is not that easy either. There are actually quite a few steps involved when using Transfer to interact with your database and to return the right kind of objects back to Flex. Also, let me state, that I am talking about this in terms of using the AMF protocol with RemoteObjects between ColdFusion and Flex so that datatypes are appropriately and quickly translated.

This post will first introduce the Transfer ORM framework and why it is not a basic task to integrate it into your ColdFusion / Flex application. In the next part of this article, I will discuss the technical details of getting this all to work.

Transfer ORM framework

Visit project website

ORM, or Object-Relational Mapping, in simplistic terms is abstracting a representation of your data inside a database as objects in your application. An ORM framework does all of this abstraction semi-automatically by means of introspection to your database and generating the necessary files, or by a configuration file that describes the datatypes and the relationships of the tables to each other. Transfer falls into the latter category.

While some say that a fault of Transfer (esp when compared to a competing framework, Reactor) is that you have to write the table structure and it's relationships manually in an XML file. Whereas Reactor is a framework that reads the database metadata. At first, I thought this same thing and after working with Transfer later, have found it much better to be in control and to define the relationships myself. With this method I really feel like I have a much better understanding of what Transfer is creating when it generates it's objects. Plus when debugging your code it is easier to see what columns/fields are causing issues in your relationships.

The benefits of using Transfer over generating the CFCs yourself using tools like Brian Rinaldi's Illudium PU-36 CFCGenerator is that once you have defined your XML file, the actual creation of the CFCs is completely automatic. To give you an example, let's say you have a table in your database called Employee that has the following fields: employeeId, firstName, lastName, email. Once you have instantiated the TransferFactory, you simply call the object the you would any other VO, only you haven't previously written it:

<cfset firstName = variables.Transfer.get('employee', 1).getFirstName() />

Transfer.get() retrieves a specific Transfer object (which is defined in your Transfer XML) with a Primary Key ID of "1". Then it simply calls the generated "getFirstName()" method on that object and returns it to you. That's it! It is really simple.

This is hardly even skimming the surface of what Transfer does for you. Let's say that an employee can have multiple phone numbers on file that you store in a separate table called EmployeePhone. This is of course a One-to-Many relationship. Once you have simply defined this relationship in your Transfer XML you would then access the phone numbers this way:

<cfset emp = variables.Transfer.get('employee', 1) />
<cfset empPhoneArray = emp.getPhoneArray() />

From there you can simply loop through that array which in turn provides different EmployeePhone Transfer objects. You can configure in your XML whether that relationship will return an array or query. Notice that I mentioned it would return an array of Transfer Objects. This is where it gets tricky. The default return type of any relational object is typed as a Transfer Object. This is instantiated from the generic Transfer Object interface in the framework and is what all the relational object inherit from. However, you can change this by applying a decorator to any of your objects which will change the type of the object to the decorator path. The decorator will also allow you to add methods as well as change existing ones that the Transfer Object defines.

As you can see, Transfer can be very very powerful in your applications. It doesn't require very much setup time and configuration. As with most advanced concepts like this, it takes practice to really understand the underlying methodologies and to use them wisely in your application. Also, do not think that Transfer is simply a glorified code generator. Mark Mandel has spent a lot of time utilizing ColdFusion's unique strengths to truly make this a fast and effecient framework to keep your application at top speed.

That's it for now. Next time I will actually get more technical about the configuration file and start discussing how to prepare your CFC middle-tier for Flex consumption.

Related reading:

Nominated for a CFEmmy!

Wow, I didn't even expect this, but I was nominated for a CFEmmy for Best Newcomer. Thank you to those of you that nominated me. Vote Now for your favorite blog, host, OS project, community site, and more.

Thank you to Todd Sharp for conducting this once again this year. And thanks again to those who nominated me for Best Newcomer. Whether I win or not, I can surely say that this has truly been a great experience. When I graduated from college I set a goal for myself to be active in this incredible community. Without all of your help I would not be where I am today. You all are always so willing to help and I truly appreciate it and the only way I can repay is to give back what I have learned to the community.

Have a great Christmas and Holiday season!

Using Custom Typed Arrays as Arguments

If you are writing a function in ColdFusion that accepts a custom typed array (com.hayes.project.component[] for example) and you don't want to make it required because it may not be provided, you can still use the default argument with an untyped array:

<cffunction name="myFunc">
<cfargument name="compArr" type="com.hayes.project.comp[]" required="false" default="#arrayNew(1)#" />
<cfreturn arguments />
</cffunction>

<cfdump var="#myFunc()#" />

I was worried that it may error out when it used the default function to create a new untyped Array. Once I thought about it, I decided to give it a try and sure enough it works great.

One thing to note, however, that Ray mentioned to me was that ColdFusion is lazy when it comes to validating typed Arrays. It only checks the first element in the array to make sure it is of the type you specified.

To overcome this shortfall, when you are looping through the array and accessing the elements' functions, simply wrap it in a try/catch in case the elements after the first one are not of the appropriate type.

A Yo-Yo After My Own Heart

This morning as I was catching up on the daily news, I decided to re-visit a fond childhood memory of the good ol' Duncan yo-yo brand. Sure enough they still exist! While pondering the site looking for my fav, the Fireball, I came across one that caught my eye and my programmer's soul.

The Duncan Cold Fusion:

Pay the premium of $100 to get your very own aircraft-grade aluminum, ball-bearing world record spinner for your very own geeky enjoyment and to remind you of the speed of our beloved Adobe ColdFusion.

Check out the Duncan Cold Fusion product page.






A Challenge for ColdFusion Developers...

This is a callout to all ColdFusion developers young and old whether your brand new to ColdFusion or you have been working with it for years--whether you are a seasoned programmer from another language or you have only ever written a Hello World application, I have a challenge for you.

[More]

Mach-II 1.5 is Gold

I am late in posting this announcement because of the slow unreliable internet at my hotel and has been spotty at the conference. We officially released the Mach-II 1.5 final build a couple of days ago and is available for download.

If you have been waiting to try out some of the new great features such as modules, SES URLs and multiple XML config files (to name a few of the features) until the release was out of beta, then now is the time!

In addition with this release, we are happy to announce workshops! If you have been in the dark about how to get your team up and running with the concepts of object-oriented programming, MVC, and how it relates to Mach-II and ColdFusion, you can now get expert instruction from the developers of the framework itself. What better way to get your team up and running with standardized, modularized code that is more efficient and cheaper to maintain.

Register at Mach-II.com for these workshops that are brought to you by GreatBizTools LLC.

Orange County CFUG Reunited

The Orange County CFUG is finally back with meetings on the 3rd Thursday of every month. The next meeting is scheduled to happen on September 20th at Traveland USA in Irvine.

Check their website for more info.

Awesome Image Effects with ColdFusion 8 - Part 1

With the release of ColdFusion 8, came a whole new set of long-awaited tags and functions for image manipulation. Developers can now easily read, manipulate, create images as well as create captchas.

Thanks to the release of the brand new CFImageEffects CFC by Foundeo, you can now do even more with images that blows other languages' image capabilities even further out of the water.

I am going to be starting a mini-series on the use of this fantastic addition to your CF 8 library of code.

The first example that I cooked up fairly quickly (thanks to the excellent documentation) was a page to test the reflection effect as well as the rounded corners effect on a photo.

Lab Url: http://www.kylehayes.info/imageeffects/

The demonstration allows you to choose an effect to apply to the photo and to tweak the basic effect settings. In this case, the reflection height, and the rounded corners radius. Give it a try and leave your feedback and what you think about the speed of the product. I think Pete has done an excellent job on this component. If you are interested in getting this for your own website, check out Foundeo's ImageEffects product page.

My next implementation idea for this is to allow the user to select multiple effects at a time and choose the order in which the effects are applied.

ColdFusionBloggers Badges Ready for the Taking

Yesterday, I suggested to Ray that he have cool badges for ColdFusionBloggers since all the aggregators had some. I hoped he would elect me to do them when I suggested this, and he did. So here are a couple of badges that I made yesterday for you to plop down on your blog and support the fastest aggregator on the web:

178x41 pixels

88x31 pixels

Mach-II 1.5b Earns Release Candidate Status

In anticipation of a final release of Mach-II 1.5 before MAX in October, the latest beta has earned release candidate status. Many bugs and issues have been fixed in this build as well as another feature. You can now use relative paths when using the new include feature (new to 1.5) or when referencing module configs.

The Mach-II team continues to make progress in development towards version 1.6, providing developers with such features as specialized advanced caching as well as a new Mach-II dashboard to allow reloading parts of your applications easier.

Excellent progress is being made and will continue to be made. Head on over to Mach-II.com to learn more and to download the latest release 1.5RC.

I am ColdFusion 8-ified

Thanks to Ayera Inc. my blog and all my clients are successfully running ColdFusion 8 now. I can really see the difference in speed increase on this website alone.

I am anxious to use all of ColdFusion 8's feature packed goodness for real world projects in the coming months.

More Entries