OOPhoto - Starting With My Base Model Object

Posted July 29, 2008 at 10:11 AM

Tags: ColdFusion

The latest OOPhoto application can be experienced here.

The latest OOPhoto code can be seen here.

This morning, I started to code the domain model for OOPhoto, my latest attempt at learning object oriented programming in ColdFusion. And, just as with my service objects, I wanted my primary domain model objects to have a base object to extend. This way, I could write the core functionality just once and have it available throughout the domain without repeating myself. If you will recall from my previous post on the domain model, each of my objects is going to use generic getters and setters to access and mutate data. For example, to set and get the photo gallery name, I might use something like this:

 Launch code in new window » Download code as text file »

  • <!--- Set the gallery name. --->
  • <cfset objGallery.Set( "Name", "Best Julia Stiles Pics Ever" ) />
  •  
  • <!--- Get the gallery name. --->
  • <cfset strName = objGallery.Get( "Name" ) />

I like this functionality a lot; it leverages the dynamic, type-flexible nature of ColdFusion and saves me from having to write out every single getter and setter method. However, I happen to hate invoking the method calls this way. I also find the code to be slightly less readable. Ideally, I would want to write the above lines as such:

 Launch code in new window » Download code as text file »

  • <!--- Set the gallery name. --->
  • <cfset objGallery.SetName( "Best Julia Stiles Pics Ever" ) />
  •  
  • <!--- Get the gallery name. --->
  • <cfset strName = objGallery.GetName() />

This just looks cleaner to me and is much more pleasing to write. Without the extra spaces and quotes, there's simply less noise, less data to parse and interpret.

I want the beauty and readability of the second set of code and the ease of implementation provided by the first set. Luckily, with advances made available in ColdFusion 8, I don't have to compromise on either. What I can do is use a combination of the generic getter and setter methods and the new event handler, OnMissingMethod(). The OnMissingMethod() method will simply catch invalid method calls (ie. GetName()) and reroute them to the appropriate generic methods.

Generic getters and setters are nice, but sometimes, I am going to need more logic than they can provide. For example, when someone attempts to Get() the array of Photos from a PhotoGallery object, I need to load those photos before I return them; I can't simply grab them out of the instance data as they might not exist yet. As such, my generic getters and setters provide hooks for overriding methods. To create specialized getters and setters, all you have to do is create a PRIVATE method with a name like:

  • Get#Property#()
  • Set#Property#( value )

The generic getters and setters are smart enough to look for these methods first before they reach directly into the instance data. This gives us the ability to, at will, override property access and mutation without having to add any extraneous code; you simply plug it in and it works.

You will also notice that, like the BaseService.cfc, there is an InjectDependency() method. This will allow our ObjectFactory.cfc to inject dependency objects like references to other Service objects that the domain model will need to know about.

All that said, here is what I have so far for the BaseModel.cfc. I am sure that as I start to flesh out the domain, updates will need to be made; but, so far in testing this seems to be working quite nicely:

 Launch code in new window » Download code as text file »

  • <cfcomponent
  • output="false"
  • hint="I provide base functionality for all primary model objects.">
  •  
  • <!---
  • Create a variables instance scope. All "object-specific" data will be
  • stored in this scope. All other items will be stored directly in the
  • VARIABLES scope.
  • --->
  • <cfset VARIABLES.Instance = {} />
  •  
  •  
  • <cffunction
  • name="Get"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I return data from the instance properties.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Property"
  • type="string"
  • required="true"
  • hint="I am the property that is being returned."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = {} />
  •  
  •  
  • <!---
  • Check first to see if there is an overriding method for this value.
  • All overriding methods must be private (scoped to VARIABLES) and must
  • start with 'Get.'
  • --->
  • <cfif StructKeyExists( VARIABLES, "Get#ARGUMENTS.Property#" )>
  •  
  • <!--- Store the return value of the override invokation. --->
  • <cfinvoke
  • method="Get#ARGUMENTS.Property#"
  • argumentcollection="#ARGUMENTS#"
  • returnvariable="LOCAL.Return"
  • />
  •  
  • <!--- Return the intermediary value. --->
  • <cfreturn LOCAL.Return />
  •  
  • <!--- Check to see if this property is valid. --->
  • <cfelseif StructKeyExists( VARIABLES.Instance, ARGUMENTS.Property )>
  •  
  • <!--- Return the property. --->
  • <cfreturn VARIABLES.Instance[ ARGUMENTS.Property ] />
  •  
  • <cfelse>
  •  
  • <!---
  • Something went wrong - a property was requested that cannot be
  • located in any fashion. Throw an error.
  • --->
  • <cfthrow
  • type="OOPhoto.BaseService.InvalidProperty"
  • message="The property you requested could not be found."
  • detail="The property you requested, #UCase( ARGUMENTS.Property )#, could not be accessed."
  • />
  •  
  • </cfif>
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="InjectDependency"
  • access="package"
  • returntype="any"
  • output="false"
  • hint="I inject dependency objecst into the VARIABLES scope of the extending Model object.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Property"
  • type="string"
  • required="true"
  • hint="The key at which the dependency will be stored."
  • />
  •  
  • <cfargument
  • name="Dependency"
  • type="any"
  • required="true"
  • hint="The dependency object that is being injected into the extending service object."
  • />
  •  
  • <!--- Inject the dependency object. --->
  • <cfset VARIABLES[ ARGUMENTS.Property ] = ARGUMENTS.Dependency />
  •  
  • <!--- Return this object for method chaining. --->
  • <cfreturn THIS />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="OnMissingMethod"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I handle calls to methods that do not exist.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="MissingMethodName"
  • type="string"
  • required="true"
  • hint="I am the name of the method that was called."
  • />
  •  
  • <cfargument
  • name="MissingMethodArguments"
  • type="struct"
  • required="true"
  • hint="I am the arguments that were passed to the missing method."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = {} />
  •  
  • <!--- Check to see if the method name is a valid Get method. --->
  • <cfif REFindNoCase( "^Get.+", ARGUMENTS.MissingMethodName )>
  •  
  • <!--- Get the property name from the method. --->
  • <cfset LOCAL.Property = REReplace(
  • ARGUMENTS.MissingMethodName,
  • "^.{3}",
  • "",
  • "one"
  • ) />
  •  
  • <!--- Return the "Get" of this property. --->
  • <cfreturn THIS.Get( LOCAL.Property ) />
  •  
  • <!--- Check to see if the method name is a valid Set method. --->
  • <cfelseif REFindNoCase( "^Set.+", ARGUMENTS.MissingMethodName )>
  •  
  • <!--- Get the property name from the method. --->
  • <cfset LOCAL.Property = REReplace(
  • ARGUMENTS.MissingMethodName,
  • "^.{3}",
  • "",
  • "one"
  • ) />
  •  
  • <!--- Return the "Set" of this property. --->
  • <cfreturn THIS.Set(
  • LOCAL.Property,
  • ARGUMENTS.MissingMethodArguments[ 1 ]
  • ) />
  •  
  • <cfelse>
  •  
  • <!--- If we got this far than the method was truly invalid. Throw an error. --->
  • <cfthrow
  • type="OOPhoto.BaseService.InvalidMethod"
  • message="The method you requested could not be found."
  • detail="The method you requested, #UCase( ARGUMENTS.MissingMethodName )#, could not be accessed."
  • />
  •  
  • </cfif>
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="Set"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I store data to the instance properties.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Property"
  • type="string"
  • required="true"
  • hint="I am the property that is being set."
  • />
  •  
  • <cfargument
  • name="Value"
  • type="any"
  • required="true"
  • hint="I am the value being stored." />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = {} />
  •  
  •  
  • <!---
  • Check first to see if there is an overriding method for this value.
  • All overriding methods must be private (scoped to VARIABLES) and must
  • start with 'Set.'
  • --->
  • <cfif StructKeyExists( VARIABLES, "Set#ARGUMENTS.Property#" )>
  •  
  • <!--- Use the override invokation. --->
  • <cfinvoke
  • method="Set#ARGUMENTS.Property#"
  • value="#ARGUMENTS.Value#"
  • />
  •  
  • <!--- Check to see if this property is valid. --->
  • <cfelseif StructKeyExists( VARIABLES.Instance, ARGUMENTS.Property )>
  •  
  • <!--- Set the property. --->
  • <cfset VARIABLES.Instance[ ARGUMENTS.Property ] = ARGUMENTS.Value />
  •  
  • <cfelse>
  •  
  • <!---
  • Something went wrong - a property was requested that cannot be
  • located in any fashion. Throw an error.
  • --->
  • <cfthrow
  • type="OOPhoto.BaseService.InvalidProperty"
  • message="The property you requested could not be found."
  • detail="The property you requested, #UCase( ARGUMENTS.Property )#, could not be accessed."
  • />
  •  
  • </cfif>
  •  
  • <!--- Always return the THIS scope so that this method can be chained. --->
  • <cfreturn THIS />
  • </cffunction>
  •  
  • </cfcomponent>

Sorry if that didn't display very well. Ordinarily I write my code with the purpose of displaying it on the blog (narrower lines ~ 66 characters wide); but, as this code is part of an actual application, I use a more relaxed coding format with slightly wider lines of code.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:




Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »
Nov 6, 2009 at 4:53 PM
How To Unformat Your Code (Like A Pro)
I tried to go *back* the other way. Adding formatting is actually a much more complicated problem than removing formatting. Anyway, here is what I could put together with a minimal amount of time: ... read »
Asaf
Nov 6, 2009 at 2:35 PM
ColdFusion GetPageContext() Massive Exploration
Hi, I actually found this post useful. I recently acquired a SSL certificate for my website and when I switched over to HTTPS Internet Explorer would throw an error when trying to download a dynamic ... read »