From what I can tell, the formal name "Mixin" is coming from the Ruby camp, and it's catching on:  it's supposed to be supported in C# 3.0.  Well, we've had them since...the dawn of CFCs, to my knowledge.

I have...umm..."mixed" feelings over Mixins.  In the world of "is-a" and "has-a," mixins are sort of a "can-do," which is why I name mine the way I do.  They're likely to be used and abused, and I can see where overuse may make life hellish - but I think that's true for most programming tools.

Ok, hubbub aside, here's a practical use and example of a Mixin:

In the Model-Glue "Headstart" template I'm working on, I've got five controllers.  They all use at least one CFC that gets autowired to the controller via ColdSpring.  Some of them, like EmailController and ErrorController, use an EmailService CFC.  So, I wind up with code like this duplicated in both Controllers:  

<div class="code"><FONT COLOR=MAROON>&lt;cffunction name=<FONT COLOR=BLUE>"SetEmailService"</FONT> access=<FONT COLOR=BLUE>"public"</FONT> returntype=<FONT COLOR=BLUE>"void"</FONT> output=<FONT COLOR=BLUE>"false"</FONT>&gt;</FONT><br>
&nbsp;&nbsp;&nbsp;<FONT COLOR=MAROON>&lt;cfargument name=<FONT COLOR=BLUE>"EmailService"</FONT> required=<FONT COLOR=BLUE>"true"</FONT> type=<FONT COLOR=BLUE>"any"</FONT> /&gt;</FONT><br>
&nbsp;&nbsp;&nbsp;<FONT COLOR=MAROON>&lt;cfset variables._emailService = arguments.EmailService /&gt;</FONT><br>
<FONT COLOR=MAROON>&lt;/cffunction&gt;</FONT><br>
<FONT COLOR=MAROON>&lt;cffunction name=<FONT COLOR=BLUE>"GetEmailService"</FONT> access=<FONT COLOR=BLUE>"private"</FONT> returntype=<FONT COLOR=BLUE>"any"</FONT> output=<FONT COLOR=BLUE>"false"</FONT>&gt;</FONT><br>
&nbsp;&nbsp;&nbsp;<FONT COLOR=MAROON>&lt;cfreturn variables._emailService /&gt;</FONT><br>
<FONT COLOR=MAROON>&lt;/cffunction&gt;</FONT></div>

Well, that's nice and redundant.  In fact, there are three such services and a configuration management CFC that get used by the various Controllers;  some use just one, some use more than one.  However, they all have something in common:  they're using application-level services to get something done.

Let's say each controller "can-do" use of application services.  Wouldn't it be nice if there was a way I could just have all of repeated code in one place?

One approach is inheritance.  ColdFusion (thankfully!) doesn't have multiple inheritance, so that's out.  All of the controllers currently inherit from modelglue.core.Controller, so I could create a different subclass of modelglue.core.Controller, and point the actual controllers to that new subclass, but that gives me a code smell:  I'm inheriting to share functionality, not represent an is-a relationship.

Instead of using inheritance, I'll just "mix in" that functionality.  

It's, uh, really simple.

First, I take all of that redundant code, and put it in my mixin file.  I'll call it UseApplicationServices.cfm, and put it under model/mixin.

Second, I'll copy all of those redundant UDFs into UseApplicationServices.cfm.

Third (and last), I'll replace all of that redundant code with the tag that started it all, &lt;cfinclude>&gt;:

<div class="code"><FONT COLOR=MAROON>&lt;cfinclude template=<FONT COLOR=BLUE>"/headstart/mixin/UseApplicationServices.cfm"</FONT> /&gt;</FONT></div>

<strong>What we've done</strong>

We've used a Mixin to say that each Controller "can-do" functionality related to public setting and private getting of application-wide services.  If I need to add a new service, I can just define it in my ColdSpring XML, add a SetNewService method to my UseApplicationServices.cfm Mixin, and all of my "Mixed" Controllers now have access to it.  Pretty trick, eh?

<strong>Conclusion</strong>

Mixins are powerful, but they can be somewhat dangerous.  You're adding methods to your CFC on the fly, and it could become a fun jungle of figuring out just where a specific method came from:  imagine multiple mixins with identically named methods!