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 »
10,640 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »