Skip to main content
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Adam Presley
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Adam Presley ( @adampresley )

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

By on
Tags:

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.

Want to use code from this post? Check out the license.

Reader Comments

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

26 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)

15,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.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel