Blog

Injecting Functions with WireBox

Brad Wood January 31, 2017

Spread the word

Brad Wood

January 31, 2017

Spread the word


Share your thoughts

This is a guest post by Eric Peterson.

I had a unique use-case the other day, and I wanted to share the solution with you all.

I use a custom Collection component to handle many higher-order functions like map, reduce, filter, etc. Basically, it wraps an array or a query and lets you continue chaining these functions, kind of like wrapping a value with lodash in Javascript.

The challenge comes in creating a Collection. There are a few options. The first one is creating it directly:

function test() {
	var numbers = new modules.cfcollection.models.Collection( [ 1, 2, 3, 4 ] );
}

Pretty verbose. Not very portable. It would break if the module ever changed the location of Collection internally. There are a lot of improvements to be made here.

With modules in ColdBox and WireBox, we can improve this code.

function test() {
	var numbers = wirebox.getInstance(
		target = "Collection@CFCollection",
		initArguments = { collection = [ 1, 2, 3, 4 ] }
	);
}

This code is still pretty verbose, but now it's portable. Plus, WireBox will automatically manage any dependencies inside Collection.

Personally, I still find this code pretty long. Also, it's not very descriptive. There's a method on the Collection class that handles this very use case called collect. It takes a query or an array and returns a new Collection. Using it looks like this:

component {

	property name="Collection" inject="Collection@CFCollection";

	function test() {
		var numbers = Collection.collect( [ 1, 2, 3, 4 ] );
	}

}

That's a nice step up, but there is also a weird thing here. We are injecting the Collection in to our component, which is a transient component, only to call a constructor method off of it. In other languages this might be a static method (which CFCollection has for you Lucee 5 folks, by the way). If we are in a handler, layout, or view in ColdBox, we can use an Application Helper. For our use case, I might add a method to our Application Helper like this:

function collect( collection ) {
	return application.wirebox.getInstance(
		target = "Collection@CFCollection",
		initArguments = { collection = arguments.collection }
	);
}

Now, in any handler, layout, or view we can use this collect function. Our handler now looks like this:

component {

	function test() {
		var numbers = collect( [ 1, 2, 3, 4 ] );
	}

}

That, to me, reads beautifully. It has a couple drawbacks, still. The dependency on collect is implicit. If you don't know how Application Helpers work, then it looks like magic. Also, it only works in handlers, layouts, and views. What if I want to use this in a different component?

With some help on the CFML Slack, I was pointed to a WireBox mapping destination of toFactoryMethod. With this mapping destination, the result of calling a factory function is what ends up being injected. Normally this would be used to wire up a component, like so:

// Color.cfc
component {

	function init( required string color ) {
		variables.color = arguments.color;
		return this;
	}

	function blueFactory() {
		return new Color( "blue" );
	}

}

// config/WireBox.cfc OR ModuleConfig.cfc
binder.map( "BlueColor" ).toFactoryMethod( "Color", "blueFactory" );

(There are other ways to inject that same combination as above. This is just an example.)

So, all I needed was a function that returned the collect function I showed off above. Something like:

component {

	// other methods....

	public Collection function collect( any items = [] ) {
        return new Collection( items );
    }

	public any function getCollectFunction() {
	    return collect;
	}

}

And a mapping to a factory method. Something like:

binder.map( "collect@CFCollection" )
    .toFactoryMethod( "#moduleMapping#.models.Collection", "getCollectFunction" );

And now, with these two pieces in place, we achieve our ultimate goal:

component {

	property name="collect" inject="collect@CFCollection";

	function test() {
		var numbers = collect( [ 1, 2, 3, 4 ] );
	}

}

We have our terse, readable method. We have a clear dependency being injected. And we can use that dependency in any injected component.

So, the next time you wish you could inject a function instead of a component, reach for this WireBox pattern.

Add Your Comment

Recent Entries

BoxLang 1.0.0 Beta 27 Launched

BoxLang 1.0.0 Beta 27 Launched

We are excited to bring you Beta 27 with enhanced XML handling, new CLI app support, improved error handling, and expanded interoperability with Java and CFML runtimes. We've also added new HTTP event hooks, improved caching strategies, and a streamlined class resolution process to make your applications more performant, even in debug modes.

Luis Majano
Luis Majano
January 31, 2025
Into the Box 2025 | Plan Your Trip With Us!

Into the Box 2025 | Plan Your Trip With Us!

Are you ready to join us for Into the Box 2025 from April 30th to May 2nd in Washington, D.C.? Let’s make your trip planning as smooth as possible. Here you’ll find Airfare discounts, Hotel Deals and fun things to do to the the best out of your trip to D.C.

Maria Jose Herrera
Maria Jose Herrera
January 30, 2025