OOPhoto - Refactoring "With Transaction" Methods To BaseService.cfc

Posted August 6, 2008 at 3:30 PM

Tags: ColdFusion

The latest OOPhoto application can be experienced here.

The latest OOPhoto code can be seen here.

After posting my object oriented solution for handling database Transactions in OOPhoto, my latest attempt at learning object oriented programming in ColdFusion, I had some great feedback that precipitated this next change. I realized that the "With Transaction" methods were so generic that they could be pulled out of the individual service objects and moved into the base service object, BaseService.cfc. Due to the fact that they merely call other methods of similar name, there was nothing about them that needed to be object-specific.

It would have been easy and straight forward to create the following methods in the BaseService.cfc:

  • SaveWithTransaction()
  • DeleteWithTransaction()

These could have turned around and called THIS.Save() and THIS.Delete() respectively; but what if later on I wanted different methods to have transactions? What if I wanted something like UploadPhotoWithTransaction() or InsertWithTransaction()? What I really want is not a way to Save and Delete with transactions; what I really want is a way to call any method with a database transaction wrapper.

As such, I decided to call upon the power of ColdFusion 8's OnMissingMethod() to create generic transactional power. Now, none of my concrete service objects have a "WithTransaction()" method. They only have non-transactional database interaction. I then updated my BaseService.cfc to have the OnMissingMethod() functionality:

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

  • <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 we have any special methods. --->
  • <cfif REFindNoCase(
  • "WithTransaction$",
  • ARGUMENTS.MissingMethodName
  • )>
  •  
  • <!---
  • Get the name of the method that we want
  • to wrap.
  • --->
  • <cfset LOCAL.MethodName = REReplace(
  • ARGUMENTS.MissingMethodName,
  • "WithTransaction$",
  • "",
  • "one"
  • ) />
  •  
  • <!--- Execute transaction. --->
  • <cfreturn THIS.ExecuteWithTransaction(
  • LOCAL.MethodName,
  • ARGUMENTS.MissingMethodArguments
  • ) />
  •  
  • <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>

As you can see, if a method is called with the suffix "WithTransaction", the OnMissingMethod() method turns around and passes control off to another BaseService.cfc method named ExecuteWithTransaction(). The ExecuteWithTransaction() method is simply a generic wrapper for method calls that need a database transaction:

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

  • <cffunction
  • name="ExecuteWithTransaction"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I wrap the given method execution in a database transaction.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="MethodName"
  • type="string"
  • required="true"
  • hint="I am the method that is going to be executed."
  • />
  •  
  • <cfargument
  • name="MethodArguments"
  • type="struct"
  • required="true"
  • hint="I am the arguments being passed to the method."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = {} />
  •  
  • <!--- Wrap the method call in a transaction. --->
  • <cftransaction action="begin">
  •  
  • <!---
  • Invoke the given method, passing along the
  • arguments.
  • --->
  • <cfinvoke
  • components="#THIS#"
  • method="#ARGUMENTS.MethodName#"
  • argumentcollection="#ARGUMENTS.MethodArguments#"
  • returnvariable="LOCAL.Return"
  • />
  •  
  • </cftransaction>
  •  
  • <!--- Return value. --->
  • <cfreturn LOCAL.Return />
  • </cffunction>

Now, any time I need to execute a CRUD method (or any other future method) with database transaction support, all I need to do is append "WithTransaction" to the name of the method.


 
 
 

 
Using The OnMissingMethod() Function Of The Base Service Object To Create Database Transaction Functionality  
 
 
 

At first, I was worried that doing something like this would be overly dynamic for no reason. After all, I am essentially cutting down on documented features for very little gain (in terms of typing). But then, I had another thought: this is dealing with database transactions. Transactions, to me, are not really the primary concern of the domain model. In a perfect world, we wouldn't even need databases. As such, I like the fact that we have almost moved the transaction concerns out of the concrete service objects and into a base service object which handles it only at a very generic level..... or maybe I am just trying to justify my actions.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page


You Might Also Be Interested In:




Reader Comments

Aug 6, 2008 at 5:47 PM // reply »
30 Comments

Ben,
Thanks again for this great series.
Keep up the good work!

I like what you have done here.
When I read your previous post regarding have a transaction method for each normal method, I kept thinking "there has to be a better way", and this is it.


Aug 6, 2008 at 6:08 PM // reply »
24 Comments

What I was trying to say in the previous comment, was:

'With some clever state settings, you could set it up so you could NEST transactions'.

So one method that contained a saveInTranaction() could call another method that also contained a saveInTransaction() and it wouldn't matter, it would just escape the 2nd one.

But I'll leave you to work out how ;o)


Aug 6, 2008 at 7:49 PM // reply »
7,572 Comments

@Ken,

Thanks man. This is the first thing I have been half-way happy for a while in this project :)

@Mark,

Does it involve GetBaseTagList()? Exception handling? I am slowly learning here, but I kind of like the fact that only the top-most call to the domain model will cause a transaction. Still feeling it out though, small project and all.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »