ColdFusion 9 ORM and reusing code

One of things that I love about the new features of ColdFusion 9, is of course the ORM and it will be no secret how much I love this feature.

In this post I am going to give a very quick run down an using the ORM to take some of the things we would do in the model or run in other application code.

Let's revisit the userAccount code I posted in a previous post.

component persistent="true" {
   property name="username" generator="assigned" fieldtype="id" datatype="string" unique="true";
   property name="firstName" datatype="string";
   property name="lastName" datatype="string";
   property name="password" datatype="string";
   property name="enabled" datatype="boolean" notnull="true";
   property name="email" datatype="string";
   property name="dateJoined" datatype="timestamp" notnull="true";
   property name="dateActivated" datatype="timestamp";
}

One of the more common aspects of code that we might wish to remove from our Application, is to encrypt a password. Now on the surface it might not be much of a time saver, but this is just an example of how removing a line of code can save a bit of retyping in your application. Or even if you want / need to reuse the component for another application.

So lets add another line to our userAccount.

component persistent="true" {
   property name="username" generator="assigned" fieldtype="id" datatype="string" unique="true";
   property name="firstName" datatype="string";
   property name="lastName" datatype="string";
   property name="password" datatype="string";
   property name="enabled" datatype="boolean" notnull="true";
   property name="email" datatype="string";
   property name="dateJoined" datatype="timestamp" notnull="true";
   property name="dateActivated" datatype="timestamp";
   SecretPassword="my secret";
}

Not much there except what we have now added a variable to be used within the component, so lets now add the following function that will override the standard setter to show some magic.

function setPassword(Password) {
   variables.password = Encrypt(Password, SecretPassword);
}

So now what we have done is that anytime we use the setter setPassword('some password'); it will encrypt the password before it is saved to the database. How cool is that?