You cannot make a copy of an object in CF!

I posted this to the COAL mailing list last night: I had an epiphany of sorts today after a conversation on cf-talk. Its something that I have known for a long time in the back of my mind, but never fully realized it. First a little background. You cannot duplicate or copy components. Components (objects) are passed by reference, not by value. And the duplicate() method doesn't work on objects. If you aren't with me, consider the following code. <cfset variables.x = createObject("component","myObject").init() /> <cfset variables.y = variables.x /> If objects were passed by value, variables.y would hold its own copy of the object. But since objects are passed by reference, variables.y and variables.x hold the exact same object. So If we make a change in variables.y, the change was also made in variables.x. So why is this a problem? Because in COAL, I implemented part of the service locator pattern that is the caching agent. Which makes sense in the service locator pattern, because the pattern is working with _services_, not objects. Services are stateless. You can create objects out of them, but they should have no instance data. They are also singletons, and only one copy should exist per application. So if the code was to request a service that has already been created, it should give them the exact same copy of the service, not a new one. But this doesn't work with the objects that COAL is supposed to create. So basically the caching agent in COAL is useless, and should not be used. I haven't run into any problems with COAL yet though, because I always turn caching off anyway. But I am sure if I was using the caching, it would have caused problems by now. So, sorry for that long explanation, but this is whats going to happen. The service locator pattern is still great for COAL in the way that you can request just the object name (that you have given it in the config xml document) and get it to know where the component is stored and create the object for you. So we are just going to take the caching agent out of the "framework" code. I am going to leave the cache attribute in the coal.cfc init() for now for backwards compatibility, but it will basically be disabled, no matter what you give it there will be no cache. So the implementations you already have wont break, just that functionality (which is and should be transparent at this point anyway) will just cease to exist. You should go ahead and take that attribute out if you are using it now because it will be taken out completely in future versions. Sorry about this dumb mistake guys. But it should all be fixed pretty easily by this weekend. Any comments or suggestions on the matter are welcome.

Posted on Tue. September 27, 2005 by Ryan Guill
 

1

Yes, however Query Objects are copied using deep-copy whenever a Query Object is assigned from one Scope to another such as in this example: <cfscript> Request.q1 = q1; // assumes q1 is a Query Object </cfscript>

Posted on Mon. September 15, 2008 by Ray Horn #
Design, Photograph, Work © Ryan Guill, aDeepBlue 2013: All Rights Reserved. | Contact | RSS Feed