Nov 21 2008
ColdFusion, Max, Object Oriented Design (OOP), Performance
I just got out of a session with Jason Delmore talking about some of the new features being looked at in ColdFusion 9. He mostly talked about the ORM features being baked into the next version of ColdFusion courtesy of Hibernate. We also squeezed a few other tidbits of information out of him too.Right off the bat, I should put out the disclaimer that everything here is subject to change as Centaur proceeds into public beta and Adobe continues to develop it. ORM stands for Object Relational Mapping and is a layer of your application which handles all database access for you in a DBMS-agnostic way that allows you to access your data directly as business objects without having to write any SQL. It is configured to specify what tables map to what objects and how each object relates to the other objects. Main benefits of ORM include:
- DB Independence
- No more CRUD or SQL
- Automatic ORM session creation and management
ORM via HibernateJava Hibernate is being built into ColdFusion which isn't new news, but today we finally saw some sample code up close to see how it will work.
New Application.cfc gets some new propertiesWhen you want to use the ORM functionality in your application there some new application settings like ormenabled and ormsetting.dataource. Here is a sample Jason put on the power point. Hopefully I copied it down correctly: Application.cfc
[code]<cfcomponent> this.name="artgallery"; this.ormenabled=true; this.ormsetting.Datasource="artgallery"; this.ormsetting.savemapping=true; this.ormsetting.Dbcreate="update"; this.ormsetting.Autorebuild=true; this.ormsetting.dialect="derby"; </cfcomponent>[/code]Now if you are still using Application.cfm I can only assume there will be some additional attributes (and perhaps a sub-tag) to the cfapplication tag to capture this data.
<cfproperty /> is getting more usefulThe next thing Jason showed us was an example of an art object from the artgallery Derby database that comes in CF. Notice that this CFC has nothing more than a handful of cfproperty tags. Art.cfc
[code]<cfcomponentpersistent="true" schema="app"> <cfproperty name="artid"> <cfproperty name="artname"> <cfproperty name="Price"> <cfproperty name="largeimage"> <cfproperty name="mediaid"> <cfproperty name="issold"> <cfproperty name="artist" fieldtype="many-to-one" column="artistid" cfc="artists" missingrowignored="true"> </cfcomponent>[/code]Notice that one of the property tag creates composition by defining an artist property with a many-to-on relationship to our art object. Additional attributes in the cfproperty tag include fieldtype, column, cfc, and missingrowignored. These tags roughly equivocate to the Hibernate configuration XML. One of the main points Jason made over and over was that they were taking special care NOT to disable or hide any of Hibernate's built-in features. This is a pretty common approach by Adobe--try to make the most common use cases turn-key easy, but allow access to all the underlying goodies so you can always dive deeper.
New functions implementedHere is a basic example Jason shows us to access an array of Art objects via our ORM layer:
[code]<cfset art.entityload("art")> <table> <cfloop from="1" to="#arraylen(Art)#" index="i"> <cfoutput> <tr> <td> #art[i].getArtname()# </td> <td> #art[i].getPrice()# </td> </tr> </cfoutput> </cfloop> </table> [/code]Jason mentioned that the exact names of the functions Adobe implements may change over time. As you can see, the entityload() function created the array of art objects. With lazy loading enabled only the records used will be fetched from the database. Here are some additional functions I saw as Jason scrolled through the code:
[code]<cfscript> Art = entityloadbypk("art",url.artid); Entitydelete() Entitysave() Ormexecutequery () // takes hql returns objects </cfscript> [/code]Another thing worth mentioning is the automatic tracking of dirty data. This means that when you go to save data, only the data you changed gets saved the to the database. Also, there is the option of having Hibernate via CF9 automatically add and remove columns from your database when you add or remove properties in your objects. I don't know how I feel about that one. I like the idea of only making your changes in one place, but I'd rather have my database be the source of those changes as opposed to my ORM modifying my database. Thoughts? Another common thing you find yourself doing a lot with objects, is taking a collection of form or URL fields and calling a bunch of setters to get that data into an object. ColdFusion 9 will likely contain a way to simply pass a struct into an object and have everything populated for you. Obviously, this would rely on some consistency in your naming conventions.