The Dopefly Tech Blog

<< The Dopefly Tech Blog Main page

Fun with Functions and CFCs VI - Mixin Method Methods

posted under category: ColdFusion on December 16, 2007 at 6:00 am by MrNate

We have been playing with the concept of mixing in methods, or injecting them into CFCs voluntarily or not. Here is a list of ways you can dynamically assign methods to components.

Include method The easiest way to get common functions into multiple CFCs is to place them into a .cfm file and simply cfinclude the file. Place the cfinclude basically anywhere within your cfcomponent tags. The constructor area (anywhere not in a function) is the best place.

The greatness of the cfinclude mix-in method is that you set it basically in stone when you code it. It's a concrete thing and it doesn't change as it is executed, just like typing the functions into the CFCs themselves. I have used this for common setters, like a setUtilities method, where I wanted to inject it on a number of different unrelated objects, but didn't want to type it more than once, and it didn't make sense to extend a common component.

Cheap injection method This is one I covered previously - you can just set another function onto the public interface of a CFC. That function will now have access to the private variables of the CFC. You can, for example, make a getter for an internal variable that shouldn't have a getter. It's a cheap hack, and works great.

Sneaky injection I call this one private scope injection. Much like the above method, but you create a getVariables method and inject the function into the private scope.

The greatness of these two methods is that they are dynamic ways to hack into components. It feels dirty to me, but it's possible.

Single function mixin() method Somewhat more of an official way to do it would be to create a mixin function inside a CFC that takes a function name and function as arguments. The method will then set the function into the appropriate scopes. This is pretty clean, and hey, you can somehow inject the mixin() function itself.

Entire component mixin() method This is where mixins actually start to become useful. You can merge one CFC into another. Make a function that accepts a component as the argument, set the public "this" struct items to the current "this"scope and force some introspection to set the private variables into the current "variables" scope.

We'll talk more about this last one next time.

Too old to comment!
On Dec 19, 2007 at 7:00 PM Michael Brennan-White (info has underestimated the power of said:

What would be the advantage to doing the include rather than the extends in the creation of the component.

I currently don't do either method but I was planning to extend some of my components in the future.

On Dec 19, 2007 at 7:00 PM Nathan Strutz ( said:

The advantage of using an include is that you can place a function that you would normally use more than once from two different types of objects into a static file. Including is a very simple, very basic, easy to understand way to practice the DRY principle.

Extending is a wonderful invention for practicing DRY, but is meant specifically for sub-typing. An Explorer is a type of SUV, is a type of motor vehicle.

But what happens when your SUV class needs a method that your User object needs? They're not related, but you know those business people and their crazy ideas.

Ok, bad examples aside, it can help you not repeat yourself across completely different and disparate types of objects. Sometimes it's convenient.

On Dec 27, 2007 at 7:00 PM Adrian Lynch ( said:
As an example of the include method, I include my onMissingMethod implementation in my CFCs that need it as it's consistent throughout.
Too old to comment!