Transfer ORM Quickstart

Transfer ORM is, as the name implies, an object relational mapper for Coldfusion. Transfer has a myriad of advanced features which can be intimidating for newcomers. Fortunately, Transfer can be installed and used very easily without learning some of the more advanced features. What follows are instructions you can use to install, configure and use transfer in a few simple steps.

Installation

UPDATE: Release Candidate 1.0 is now available.

The last official release of transfer is 0.6.3. This version is now quite old and it is recommended that you use the bleeding edge release (BER) from subversion. If you have any misgivings about using the BER, rest assured that many production systems are running the transfer BER. It's stable.

To install the transfer BER, point your subversion client to http://svn.riaforge.org/transfer and checkout or export the source from /trunk/transfer. You can either set a mapping for "/transfer" in the cf administrator, or drop the source transfer folder in your web root.

Configuration

To use transfer in an application you'll need to setup two configuration files. One for the datasource that transfer will use and another to describe the objects that transfer maps from your relational database. These files are typically named Datasource.xml and Transfer.xml, but you can use any name you like and/or append a .cfm extension to prevent the files from being visible in a web browser.

Datasource.xml

The transfer datasource.xml config specifies the values for the following:

- a datasource name that currently exists in the cf administrator - a username (if required - leave blank otherwise) - a password (if required - leave blank otherwise)

<?xml version="1.0" encoding="UTF-8"?>
<datasource xsi:noNamespaceSchemaLocation="../xsd/datasource.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>dsn_name</name>
<username></username>
<password></password>
</datasource>

Transfer.xml

The transfer.xml file is used to define the properties and relationships of transfer objects that are mapped to the relational tables in your database. Objects can have onetomany, manytoone, or manytomany relationships. The following xml is the object definitions from the tBlog sample app.

<?xml version="1.0" encoding="UTF-8"?>
<transfer xsi:noNamespaceSchemaLocation="../xsd/transfer.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<InvalidTagDefinitions>
<package name="user">
<!-- User details -->
<InvalidTag name="User" table="tbl_User">
<id name="IDUser" type="numeric"/>
<property name="Name" type="string" column="user_Name"/>
<property name="Email" type="string" column="user_Email"/>
</object>
</package>
<package name="system">
<!-- Different categories for Blog Posts -->
<InvalidTag name="Category" table="tbl_Category">
<id name="IDCategory" type="numeric"/>
<property name="Name" type="string" column="category_Name"/>
<property name="OrderIndex" type="numeric" column="category_OrderIndex"/>
</object>
</package>
<package name="post">
<!-- A Blog Post, with a Decorator of type com.Post -->
<InvalidTag name="Post" table="tbl_Post" decorator="tblog.com.Post">
<id name="IDPost" type="numeric"/>
<property name="Title" type="string" column="post_Title"/>
<property name="Body" type="string" column="post_Body"/>
<property name="DateTime" type="date" column="post_DateTime"/>
<!-- Link between a Post and the User it who wrote it -->
<manytoone name="User">
<link to="user.User" column="lnkIDUser"/>
</manytoone>
<!--
Link between a post and it's array of Comments
-->

<onetomany name="Comment">
<link to="post.Comment" column="lnkIDPost"/>
<collection type="array">
<order property="DateTime" order="asc"/>
</collection>
</onetomany>
<manytomany name="Category" table="lnk_PostCategory">
<link to="post.Post" column="lnkIDPost"/>
<link to="system.Category" column="lnkIDCategory"/>
<collection type="array">
<order property="OrderIndex" order="asc"/>
</collection>
</manytomany>
</object>
<!-- A comment for a blog post -->
<InvalidTag name="Comment" table="tbl_Comment">
<id name="IDComment" type="numeric"/>
<property name="Name" type="string" column="comment_Name"/>
<property name="Value" type="string" column="comment_Value"/>
<property name="DateTime" type="date" column="comment_DateTime"/>
</object>
</package>
</objectDefinitions>
</transfer>

Integration

Using Transfer with Coldspring

<bean id="transferFactory" class="transfer.TransferFactory">
<constructor-arg name="datasourcePath"><value>/config/transfer/Datasource.xml</value></constructor-arg>
<constructor-arg name="configPath"><value>/config/transfer/Transfer.xml</value></constructor-arg>
<constructor-arg name="definitionPath"><value>/path/to/definitions</value></constructor-arg>
</bean>

<bean id="datasource" factory-bean="transferFactory" factory-method="getDatasource" />

<bean id="transfer" factory-bean="transferFactory" factory-method="getTransfer" />

Using transfer as an application scope "singleton".

<cfset transferConfig = StructNew() />
<cfset transferConfig["datasourcePath"] = "/config/transfer/Datasource.xml" />
<cfset transferConfig["configPath"] = "/config/transfer/Transfer.xml" />
<cfset transferConfig["definitionPath"] = "/path/to/definitions" />
<cfset application.transferFactory = CreateObject("component","transfer.TransferFactory").init(transferConfig) />
<cfset application.transfer = application.transferFactory.getTransfer() />

Usage

To grasp the full breadth of the transfer API, you really need to review the Transfer docs. I'll cover the four methods you will likely work with the most in the beginning. They are, get(), new(), save() and delete().

Using the tBlog object definitions above, to retrieve a post object use:

<cfset application.transfer.get("post.Post",1) />

To have transfer return a new instance:

<cfset post application.transfer.new("post.Post") />

To modify and save a new post:

<!--- create a new post --->
<cfset post = application.transfer.new("post.Post") />
<!--- retrieve category with id 1 --->
<cfset category = application.transfer.get("system.Category",1) />
<!--- set properties (not shown) --->
<!--- set category for post --->
<cfset post.setCategory(category) />
<!--- save the post --->
<cfset application.transfer.save(post) />

A small, but significant, benefit of using Transfer ORM, is that when you save an new object the object's state reflects it's new persisted state. So you can find out the new id by simply calling.

<cfset newid = post.getID() />

Lastly, transfer delete() method works like save(), you pass the transfer object to the method.

<cfset application.transfer.delete(post) />

Much, much, more

This quick start is intended to help impatient types (like myself) get a taste for how simple it is to start using Transfer ORM. As you become comfortable with the Transfer API, you will definitely want to explore features like Transfer Object Decorators, the Transfer Event Model and Transfer Query Language (TQL). Mark Mandel has put a great deal of effort to into Transfer since it turned POSS. If you are not comfortable using the bleeding edge source code from subversion, keep an eye out for a 1.0 Release Candidate.

My cf.Objective() Schedule - Focus on Frameworks and Automation

I've finally decided to stick with a theme for my first CF conference. The goal is to increase my frameworks acumen and better my development methodology. So with that in mind, here's my cf.objective() schedule. Let's hope it sticks.

Fancy Bread Blog Turns One Today!

Thanks to all the folks who have read and commented on this blog over the past year. I've met a lot great people and learned more than I imagined.

A quick review of my stats for the year shows 44 posts with an average of 865 views and just under 2 comments per post. The top three categories are ColdFusion, ColdSpring and Transfer. I started blogging to share my learning experiences and ideas. Year two will be a lot more of the same. :)

ColdFusion 8 Application Mappings and Sub-folders

Now that I'm on a CF8 hosting plan, I've started leveraging some of that goodness in my applications. The first thing I have implemented is application level mappings to keep my configuration files and components out of the web root. Here's how my current project layout rolls.

/project
  /config <-- Coldspring and Transfer config files
  /model <-- model components
  /root <-- web root
    /admin  <-- secure admin screens

Basically, I want to create mappings to config and model. I did a little googlin' and found a nice snippet on Sean Corfield's blog on looping over a list to build the mappings. I aped that code with a slight mod for my setup.

<cfif (ListLast(CGI.HTTP_HOST,".") eq "local")>
<cfset mapPath = "../">
<cfelse>
<cfset mapPath = "../" & this.name & "/">
</cfif>

<cfloop index="p" list="config,model">
<cfset this.mappings["/" & p] = expandPath(mapPath) & p />
</cfloop>

I dev on Mac and deploy on Windows and my local folder structure is slightly different from my host. The first part of the code is just a test of whether the site is being served up locally. When on the production server, the mapped folders are in a folder with the same name as the app.

This is a great alternative to using Apache Alias or Virtual Directories in IIS for mappings. That is, until I tried using the mapping from within the admin folder. Suddenly the path was not recognized. I initially thought this was a transfer issue, but that was not the case. I tried several variations and workarounds. Eventually, I figured out that the mappings were fine for files in the web root where Application.cfc resides. So why was the path not available to files in a folder below the web root? The answer was pretty simple. The admin folder had no Application.cfc, thus no mappings.

Remembering another gem by Mr. Corfield regarding extending Application.cfc via a Proxy, I created an Application.cfc in the admin folder and the required proxy in the web root. Instead of repeating the code for creating the mappings with only the path prefix changed, I wrapped the creation of mappings up in a method.

<cffunction name="createMappings" access="private" output="false" returntype="void">
   <cfargument name="pathPrefix" type="string" required="false" default="../">
   <cfif (ListLast(CGI.HTTP_HOST,".") eq "local")>
      <cfset variables.mapPath = arguments.pathPrefix>
   <cfelse>
      <cfset variables.mapPath = arguments.pathPrefix & this.name & "/">
   </cfif>
   <cfloop index="p" list="config,model">
<cfset this.mappings["/" & p] = expandPath(variables.mapPath) & p />
   </cfloop>
</cffunction>

And call it from the Application.cfc in the admin folder using the appropriate prefix.

<cfcomponent displayname="Application" extends="ApplicationProxy">

<!--- set mappings in this directory --->
<cfset createMappings(pathPrefix="../../")>

</cfcomponent>

Application level mappings are handy, but the per directory limitation does not make for a tad more code. Overall, I think it's a small price to pay for the benefit gained.

UPDATE:

After testing this on my Windows machine, it appears that using "this.mappings" inside a method does not properly set the mappings. I've changed things around so that the createMappings() method returns a struct and I set it as a pseudo-constructor.

<cfset this.mappings = createMappings(pathPrefix="../") >

Of Views and Models and Working With Transfer ORM

Yesterday, I asked Mark Mandel for some advice on tuning my object model for performance. Mark, being an ever-so-helpful (P)OSS author, gave me a few suggestions. Turns out that someone asked basically the same question on the transfer list today. Jaime Metcher made a couple of excellent points on choosing between queries and object composition. Paraphrasing, Jaime, when working with large data sets (the type of thing you might be tempted to use arrays of objects for), you will get better performance using a query. If you want to display a single instance of some object and the view is truly a composite, then your model should reflect that composition. Understanding this helps with choosing between onetomany or manytoone relationships for your object compositions.

[More]

Two Utility Classes to Help Keep Your Controllers Lean

I have really enjoyed reading the perspectives on application architecture that have sprouted up on the mailing lists lately. It comes a at good time for me, since I'm beginning my first fully OO MVC project. One of the best recommendations, I feel, is to keep your controllers as dumb as possible. While this is a fine aspiration, there are plug-ins and convenience methods available to frameworks that just make life easier. In particular, the availability of "beaner" methods. I'm currently using Model-Glue, so I'll use the makeEventBean() method as an example. MakeEventBean() is really useful for populating a bean with any setter methods that match the keys in the Model-Glue event. While convenient, I think it provides a slippery slope for business logic to slide into controller methods.

[More]

My Learning Agenda: What's on yours?

I've had this list on my white board for a couple of months. It's time to make some space, so I'm posting this so I can look back and see how long it takes to achieve a modest degree of proficiency for each subject (by my own measure). Some of the subjects I have a beginner to intermediate knowledge of. I'm listing them here because I want to reach an expert level.

Apache Ant

I've run a build.xml file, but I do not know a thing about how Ant works or how to leverage it for development. Sad, but true...

Subversion

I've used Subversion for a long time, but have always been petrified of using branches, tags and merging. I need to overcome that to reach the next level. Even if I start with a local repository first.

Unit Testing/TDD

I've made some headway in this department with CFCUnit, but my effort has been less that stellar. I'm radically changing my development work flow and this is one area that I definitely want to integrate without backsliding or excuses.

Regular Expressions

Oh, the shame. For I am lame when it comes to regex. It's just one of those things I've passed on learning in favour of something else.

Aspect-oriented Programming (AOP)

Like subversion, I'm familiar with ColdSpring, but mostly on a simple level. Over the next few months, I'm pretty serious about breaking out of the comfort zone and really learning how to harness the more powerful features ColdSpring has to offer.

Design Patterns

I grok a few patterns, but the majority of patterns, and OO concepts, are elusive to me. I might need to really focus on the basics before venturing into the pattern hinterland.

Flex

I have an AIR app in the HTML/Javascript category, but I have not sat down and worked at learning Flex. "I'll get to it", I keep telling myself. And with the demand for Flex developers these days, I'd be a damn fool not to add those skills to the arsenal sooner than later. ;)

Actionscript 3

Oh how I love and cherish my Essential Actionscript 2 book by Colin Moock. I know I'm going to dig working with AS3 as much, if not more. Why haven't I mastered it yet? Still searching for a good project that I can put a focused effort into.

jQuery

jQuery gets a lot of love and rightfully so. I've had a taste over the last few weeks and it can be an addictive.

UML

I have only scratched at UML, but I'd really like to be able to build UML into a standard part of my development process. Even if I only master class diagrams I'll be happy.

A programming language I don't know

Ruby and Python are high on the list. Regardless, I want to spend a little time working in a new language (static or dynamically typed) to improve my understanding of object oriented programming.

This agenda is subject to change. I haven't place a date or priority on any subject as it is likely that I'll focus on two or more at any given time. And it's entirely possible that I immerse myself deeply into one subject until I'm satisfied with my level of proficiency.

Avoid the Instant Gratification Trap. Trust your Instinct and Ask Questions.

Ben Nadel's recent post, Code Assertively, really struck home for me today. In it, he discusses the use of non-assertive methods, like Evaluate() and IsDefined(). I definitely prefer to use StructKeyExists() over IsDefined() as a test for variable existence, but a large portion of my code for the scaffolds in my transfer based "generic admin" made liberal use of Evaluate(). I found it ironic that I could code "assertively" in one regard and be very dogmatic about it, but could also justify stooping to hacked solutions . At the time that I was implementing my Evaluate() hack, I had that uneasy feeling that there was a better way, but my desire to make it work urged me to forget best practices in favor of the instant gratification of being able to say "voila!". Looking back at the code for that series, there were three specific places where I could improve things. One, I knew how to fix. The other two I will have to thank Mark Mandel for hipping me to some things I had forgotten or wasn't even aware of...

[More]

BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner