Learning CF9: Are Mixins Supported?
Posted on October 21, 2009 at 6:57 PM in ColdFusion
One of the "tricks" I have used for a long time now is to use a mixin with an object. For example, I have a collection of UDFs that are related to date and time manipulation. Certain classes get all or some of these mixed in like so:
- <cfcomponent>
- <cfinclude template="/my/datetime/udf.cfm" />
- <!--- other methods --->
- </cfcomponent>
I noticed today that in CF9 the mixin methods do not appear in a <cfdump> of the object. My first reaction was to hit the panic button and submit a bug report. Thankfully, however, I decided to actually test a mixin method first. Yeah, I know, what a novel idea!
As it turns out, while the mixed-in methods do not appear in the dump, they perform exactly as expected.
So go ahead and mix away. It'll still work as it should. Just don't try looking a dump to find out what methods are there. ;-)
Hmmmm. Perhaps a bug report should be filed nonetheless. ???
Latest Articles
- No recent entries.
Categories
- ColdBox (21) [RSS]
- ColdFusion (92) [RSS]
- Fusebox (3) [RSS]
- General (22) [RSS]
- jQuery (15) [RSS]
- Kalendar (1) [RSS]
- Linux (1) [RSS]
- Mura CMS (1) [RSS]
- Railo (1) [RSS]
- Rants (5) [RSS]
- Transfer (8) [RSS]
- Uni-Form Tag Library (36) [RSS]



On 10/21/09 at 10:41 PM, sparkpool said:
On 10/21/09 at 10:51 PM, Brad Wood said:
On 10/22/09 at 12:17 AM, Nathan Strutz said:
As a side comment, the mix-in thing, how do you think that works for you as far as reducing complexity in your applications? Would you recommend it over other solutions like perhaps a utils package or component?
On 10/22/09 at 12:40 AM, Matt Quackenbush said:
@ Brad - structKeyExists() returns true.
@ Nathan - I think it works quite well. However, I am by no means suggesting that one should exclusively use mixins, nor am I suggesting that they are better than a utility object. As a matter of fact, I use both. My general rule of thumb is, if the object in question needs some of those utility methods, and they will be called from outside the object, I use a mixin. What I don't want to see throughout my application is #getFoo().getUtilObject().doSomething()#.
On 10/22/09 at 1:51 AM, Brad Wood said:
On 10/22/09 at 3:12 AM, Matt Quackenbush said:
Are you saying that in your environment that <cfdump> and getMetadata() both show mixin methods?
On 10/22/09 at 11:33 AM, Tony Nelson said:
On 10/22/09 at 5:10 PM, Brad Wood said:
Of course, I'm not 100% certain what the component cache does, so I'm just guessing.
I am at work now where my CF9 test machine is, so I will play with it. My last comment was made from home with no way to try it out.
On 10/22/09 at 5:38 PM, Matt Quackenbush said:
On 10/22/09 at 6:37 PM, Brad Wood said:
So, my main question is how you would be able to dynamically determine all the methods a CFC has AND take into account any mixins added after instantiation.
I will point out that any fields added to the component DO still show up in CF9 when dumping it an object:
<cfset MyCFC.feild2 = "bar">
The only way I can find to determine all the methods a component has is to loop over it as a collection and test the meta data of each item to see if it is a struct with a name and parameters key.
Method Names:<br>
<cfloop collection="#myCFC#" item="i">
<cfif isStruct(getMetaData(myCFC[i])) and structKeyExists(getMetaData(myCFC[i]),"name") and structKeyExists(getMetaData(myCFC[i]),"parameters")>
#i# <br>
</cfif>
</cfloop>
I just ran some tests it in CF7 and CF8 to refresh my memory on what used to work, and while the mixin method does show up when cfdumping a component in CF8, it still never showed up in the getMetaData().
So, I guess we've _never_ gotten mixin methods in getMetaData(), but we at least use to be able to see them in CFDump. Honestly, I think they should show up in both places. They need to give us our cfdump action back, AND finally fix getMetaData to work correctly.
Sorry to ramble, but another thing on sniffing out the collection items-- the name of the method in the meta data dump of the methods shows the original function name-- not the name of the pointer to it in the CFC. Kind makes sense-- but it's still interesting.
On 11/19/09 at 5:30 AM, Rupesh Kumar said:
CFDump used to show the methods added to the instance by looking up everything from the 'this' scope. In CF9 that was changed so that it shows methods from the metadata. That was done for some reasons but I guess we need to do both.