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:

  1. <cfcomponent>
  2. <cfinclude template="/my/datetime/udf.cfm" />
  3. <!--- other methods --->
  4. </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. ???

Comments
(Comment Moderation is enabled. Your comment will not appear until approved.)

On 10/21/09 at 10:41 PM, sparkpool said:

What do you see with GetMetadata() ? Do the mixed-in methods show, and if they do, "where" are located in the object structure?

On 10/21/09 at 10:51 PM, Brad Wood said:

I wonder if this is due to the metaData being cached. Does the method show up if you do structKeyExists? What about getMetaData()?

On 10/22/09 at 12:17 AM, Nathan Strutz said:

Yep, I agree, sounds like something missing in the dump. Drop it in the bug tracker!

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:

@ sparkpool and Brad - The mixin methods do *not* appear in the getMetadata() struct, either.

@ 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:

Ok, now that CF9 has the new component cache, there is a new button in the CF Admin next to the Clear Template Cache one to clear the component cache. Find if the admin API has a corresponding method for that button and call it after making your modification to the CFC but prior to calling getMetaData or dumping it. If the new method shows up it might point to a bug in the component cache not picking up run-time changes.

On 10/22/09 at 3:12 AM, Matt Quackenbush said:

@ Brad - Perhaps I am misunderstanding what you're saying, but I am not making any changes to the CFC. This is a component that has existed as-is for years. It simply does not show the mixed-in methods in the <cfdump> or the getMetadata() call.

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:

I was running into similar problems a couple days ago. Good to know mixins still work but just don't appear in the dump.

On 10/22/09 at 5:10 PM, Brad Wood said:

@Matt: I understand you are not actually modifying the CFC file itself, but _are_ modifying an instance of that component once it is in memory. What I was suggesting was that CF9's new Component Cache setting might be causing run-time modifications to instantiated components to not be reflected in the metaData.
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:

@ Brad - Aaaaahhhhh. My bad. I'm on the same page with you now. I was obviously in need of sleep. :-) I've not yet had a chance to play with that. If you get the chance, I'd definitely be interested in your results.

On 10/22/09 at 6:37 PM, Brad Wood said:

@Matt: The component cache seems to have no affect on this. After reading the description on the setting-- it actually seems to apply to WHERE the component is found in your directory structure rather than its meta data.

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:

GetMetaData is the meta data of the CFC and not of a particular instance. This is shared by all the instances. So this will not include methods that were added to a particular CFC instance at runtime.
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.
CodeBassRadio

Latest Articles

Eventually something really brilliant and witty will appear right here.

Calendar

May 2026
S M T W T F S
« Apr  
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            

Subscribe

Enter a valid email address.

The Obligatory Wish List