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 »
- <cfset objGallery.Set( "Name", "Best Julia Stiles Pics Ever" ) />
-
- <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 »
- <cfset objGallery.SetName( "Best Julia Stiles Pics Ever" ) />
-
- <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.">
-
- <cfset VARIABLES.Instance = {} />
-
-
- <cffunction
- name="Get"
- access="public"
- returntype="any"
- output="false"
- hint="I return data from the instance properties.">
-
- <cfargument
- name="Property"
- type="string"
- required="true"
- hint="I am the property that is being returned."
- />
-
- <cfset var LOCAL = {} />
-
-
- <cfif StructKeyExists( VARIABLES, "Get#ARGUMENTS.Property#" )>
-
- <cfinvoke
- method="Get#ARGUMENTS.Property#"
- argumentcollection="#ARGUMENTS#"
- returnvariable="LOCAL.Return"
- />
-
- <cfreturn LOCAL.Return />
-
- <cfelseif StructKeyExists( VARIABLES.Instance, ARGUMENTS.Property )>
-
- <cfreturn VARIABLES.Instance[ ARGUMENTS.Property ] />
-
- <cfelse>
-
- <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.">
-
- <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."
- />
-
- <cfset VARIABLES[ ARGUMENTS.Property ] = ARGUMENTS.Dependency />
-
- <cfreturn THIS />
- </cffunction>
-
-
- <cffunction
- name="OnMissingMethod"
- access="public"
- returntype="any"
- output="false"
- hint="I handle calls to methods that do not exist.">
-
- <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."
- />
-
- <cfset var LOCAL = {} />
-
- <cfif REFindNoCase( "^Get.+", ARGUMENTS.MissingMethodName )>
-
- <cfset LOCAL.Property = REReplace(
- ARGUMENTS.MissingMethodName,
- "^.{3}",
- "",
- "one"
- ) />
-
- <cfreturn THIS.Get( LOCAL.Property ) />
-
- <cfelseif REFindNoCase( "^Set.+", ARGUMENTS.MissingMethodName )>
-
- <cfset LOCAL.Property = REReplace(
- ARGUMENTS.MissingMethodName,
- "^.{3}",
- "",
- "one"
- ) />
-
- <cfreturn THIS.Set(
- LOCAL.Property,
- ARGUMENTS.MissingMethodArguments[ 1 ]
- ) />
-
- <cfelse>
-
- <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.">
-
- <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." />
-
- <cfset var LOCAL = {} />
-
-
- <cfif StructKeyExists( VARIABLES, "Set#ARGUMENTS.Property#" )>
-
- <cfinvoke
- method="Set#ARGUMENTS.Property#"
- value="#ARGUMENTS.Value#"
- />
-
- <cfelseif StructKeyExists( VARIABLES.Instance, ARGUMENTS.Property )>
-
- <cfset VARIABLES.Instance[ ARGUMENTS.Property ] = ARGUMENTS.Value />
-
- <cfelse>
-
- <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>
-
- <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
Comments (0) |
Post Comment |
Ask Ben |
Permalink |
Other Searches |
Print Page
What Other People Are Searching For
[ local search ]
creating a base model object
[ local search ]
using generic getters and setters in object oriented programming
[ local search ]
generic getters and setters in coldfusion
[ local search ]
learning object oriented programming
[ local search ]
how to write oop code in coldfusion
There are no comments posted for this web log entry.
Post Comment |
Ask Ben