Chaining objects in Reactor, an idea
August 14, 2008 ·
I have been doing a fair amount of work recently with Reactor, and not so related to reactor, I have also been using JQuery.
This has led me to a little thought experiment I was going to propose to Doug Hughes for the project, which is the idea of chaining. This is something that JQuery does fairly well and Reactor does to some level.
The idea is this, that in any object that has setters, or functions that return "void", they should actually return the newly modified object itself ( the "this" scope).
"Why?" you might ask, well, here are a couple of examples from my code:
<cfscript>
orm = getModelGlue().getOrmService();
myObj = orm.createRecord("Element");
myObj.setId("124");
myObj.load();
</cfscript>
Well, that is ok, but I would prefer to do the creating the object and loading it in one line:
<cfscript>
orm = getModelGlue().getOrmService();
myObj = orm.createRecord("Element").setId("124").load();
</cfscript>
There we go, in one line. This could also go for doing all the setting of the other properties:
<cfscript>
orm = getModelGlue().getOrmService();
myObj = orm.createRecord("Element")
.setId("124")
.load()
.setName("Elvis")
.setAge("34")
.save();
</cfscript>
What do people think of this idea? It is fairly easily implemented, doesn't break any existing code and would remove a lot of <cfset>'s and if nicely formatted, make just as much sense.
EDIT: As an aside, Doug let me know (and I should have known really but its nice to learn a new thing every day) that you can shorten the loading even further:
<cfscript>
orm = getModelGlue().getOrmService();
myObj = orm.createRecord("Element").load(id="124");
</cfscript>
Tags: coldfusion · reactor · webdev
10 responses
1 Adam Tuttle // Sep 22, 2008 at 4:14 PM
2 Mark Drew // Sep 22, 2008 at 4:14 PM
The only thing to REALLY get this working would be deleting the files under reactor/project/<your project>/Record/ since they need to be re-generated.
I am going to get some of my team working on this and we shall see, but I don't forsee any problems (then again, I am sure I will correct myself in hindsight)
3 ike // Sep 22, 2008 at 4:14 PM
Anyway the onTap framework components have generally chained like this from early on, so my code for writing queries tends to be a combination of series' of chained function calls. On one line I'll chain together the init() with the table name and a sort() and on the next line I'll chain together several joins and filters and then I'll return either the statement object or an .execute() of it, often from another chained function. It's worked pretty well for me.
Maybe one of these days if more interest is generated in it, I'll do an ORM version of my frameworks comparison with Ray's Galleon Forums. :) I've thought about it, but the response to the original article seems to have been so underwhelming that I'm not in a rush to do the ORM sequel.
4 marc esher // Sep 22, 2008 at 4:14 PM
5 Mark Drew // Sep 22, 2008 at 4:14 PM
A lot of frameworks could actually benefit from this idea, even plain old java stuff, or ANY object that you create that has a set method, It just makes me think to re-look at methods that return "void" in a new light
6 Mark Drew // Sep 22, 2008 at 4:14 PM
somehow liking it... dont ask me why... I am perverse like that...
7 ike // Sep 22, 2008 at 4:14 PM
8 Tom Chiverton // Sep 22, 2008 at 4:14 PM
If you get in the habit of making your own code do it, it normally pays of somewhere down the line :-)
9 me // Sep 22, 2008 at 4:14 PM
myObject
doThis
doThat
doTheOther
it's OO goodness
10 Boyloasse // Jul 21, 2011 at 10:26 PM
Excellent website. Plenty of helpful info here. I¡¦m sending it to a few friends ans also sharing in delicious. And naturally, thanks in your sweat!
hi!,I like your writing so so much! percentage we keep up a correspondence extra approximately your article on AOL? I need an expert on this area to solve my problem. May be that is you! Having a look ahead to look you.