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

Posted August 6, 2008 at 3:30 PM by Ben Nadel

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:

  • <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:

  • <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.


You Might Also Be Interested In:



Reader Comments

Aug 6, 2008 at 5:47 PM // reply »
35 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 »
11,238 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 A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools