Mach-II Call Method Command, How Do I Love Thee?

Posted 19 February 2010

Now that Mach-II 1.8 is out, I figured I’d write a little bit about one of my favorite new features of the release, the <call-method> command.

Here’s the problem that this new feature is designed to solve:

In a well-designed Mach-II application, you don’t want to have a lot of business logic in your http://greatbiztoolsllc.trac.cvsdude.com/mach-ii/wiki/IntroToListeners. As a result, a lot of the methods in Mach-II listeners wound up being simple pass-throughs to a method in the service layer. This was repetitive, and tedious. Here’s an example:

<cffunction name="getAllUsers" access="public" output="false" returntype="query">
  <cfargument name="event" type="MachII.framework.Event" required="yes" />
  <cfreturn variables.userService.getAllUsers() />
</cffunction>

We had to write a function that simply called a service layer method and returned the results. It’s not hard to do. It is, however, clutter.

The <call-method> command eliminates the need for these kinds of placeholder method calls in your listener. Now you can do this in your Mach-II XML configuration file:

<call-method bean="userService" method="getAllUsers" resultArg="qryAllUsers" />

The “bean” referenced in this tag is a ColdSpring-managed bean that has already been auto-wired by the ColdSpring property in Mach-II. So this feature only works if you are also using ColdSpring (and — I’ve said it many times before — if you’re not using ColdSpring or some other IoC container in your ColdFusion applications, you’re costing yourself time and money.)

By using the <call-method> command, you’ve turned four lines of code into one. Not bad!

What if your service layer methods require arguments to be passed to them? That’s easy, and Mach-II handles this.

<call-method bean="userService" method="getUser" args="${event.userID}" resultArg="user" />

The args attribute of the <call-method> command can take single or multiple, comma-separated values, or even an argument collection structure.

But what’s the ${ } syntax? That’s the expression language syntax that’s built in to Mach-II. This feature arrived in Mach-II 1.5 under the name “Bindable Property Placeholders,” and has evolved since then and is even more useful. It allows you to provide variables evaluated at runtime within the Mach-II configuration file. You can even provide default values in case the variable you’re looking for in the expression doesn’t exist (ie; ${event.userID:0}). So in the example above, it’s going to to pass the result of evaluating event.userID (getting the value of the userID variable in the current event) to the userService.getUser() method.

I’m using the <call-method> command in my latest project using 1.8. It’s saving me time, makes the event flow as defined in the Mach-II XML config file clearer, and eliminates a duplicate layer of abstraction in the app.

The official documentation for the <call-method> command can be found in the Mach-II wiki.

Categories: ColdFusion