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  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

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 »
20 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 »
6,515 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
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »
Nov 20, 2009 at 5:07 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, I have seen tidbits about the way Railo handles session. I can understand that it lazy-loads sessions, but I also think that I might make some things more complicated. For example, often tim ... read »
Nov 20, 2009 at 4:53 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, you can ramp up the security by turning on J2EE session which gives you a third set of numbers other than CFID/CFTOKEN. There's a reason why ACF put this in place (other than just session replic ... read »
Nov 20, 2009 at 4:52 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Case in point, Ben, you may not be aware of this, but in Railo - OnApplicationStart() & OnSessionStart() act differently than in ACF. ACF does: OnApplicationStart (1st hit) OnSessionStart (1st and e ... read »
Nov 20, 2009 at 4:46 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, That's understandable. I am not sure if this really leaves any more security holes than the fact that using old cookie-based CFID / CFTOKEN values will create a new session using the old CFI ... read »