Skip to main content
Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.

ColdFusion Component Setters / Accessors Are Chainable For Easy Dependency-Injection

By Ben Nadel on
Tags: ColdFusion

This is primarily a note-to-self; but the other day, I stumbled upon / remembered that the auto-generated accessors in a ColdFusion component are chainable. At work, I never think about this because we use a dependency-injection framework which performs all the setter-injection for us. However, in my blogging platform, all the components are wired-up manually in my onApplicationStart() event-handler. As such, the fact that I can chain my setter accessors leads to a lovely, fluent API.

To see this in action, here's a simple ColdFusion Component that has accessors=true and a few CFProperty tags:

	accessors = true
	output = false

	// Define properties for dependency-injection.
	property logger;
	property myGateway;
	property stats;

	// .... we're just focusing on accessors in this demo .... //


With accessors=true on the CFComponent tag, ColdFusion will automatically generate both a getter and a setter for each of the CFProperty tags:

  • getLogger()
  • setLogger( logger )
  • getMyGateway()
  • setMyGateway( myGateway )
  • getStats()
  • setStats( stats )

All of the set{name}() methods can be chained:


	fakeGateway = { displayName: "fakeGateway" };
	fakeLogger = { displayName: "fakeLogger" };
	fakeStats = { displayName: "fakeStats" };

	// Note that our SETTERS can be chained for easy dependency-injection.
	myService = new MyService()
		.setLogger( fakeLogger )
		.setMyGateway( fakeGateway )
		.setStats( fakeStats )

	cfdump( var = myService );


As you can see, each the setter methods returns the ColdFusion component instance. Which means, each setter can be chained on the previous setter; and, the last setter call can then store the resultant value into a component reference variable.

When we run this ColdFusion code in Lucee CFML, we get the following output:

ColdFusion component with generated accessors and properties.

As you can see, there are 6 generated accessors for the 3 properties. And, the "fake" dependencies that we injected show up as the property values. Internally, these properties have been stored into the private variables scope of the ColdFusion component.

For large ColdFusion application, some sort of Inversion of Control (IoC) container should be used (think Framwork/One, think WireBox); but, for small sites, manually dependency-injection (DI) is more than sufficient. And, being able to chain setters is a lovely little property (no pun intended) of the language.

Reader Comments

What has two thumbs and hopes you leave a comment? This Guy! (Ben Nadel).

Post A Comment

You — Get Out Of My Dreams, Get Into My Blog
Live in the Now
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.