Mark Drew (Redux)- cf_etc...

a compendium of railo, cfml, cfeclipse and technology topics

Mark Drew (Redux)- cf_etc...

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();
      
      //do stuff with the object now
   
   </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();
      //do stuff with the object now
   
   </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

    Sounds like a great idea, and also sounds like it would be backwards compatible too, which would be important. I'm all for it!
  • 2 Mark Drew // Sep 22, 2008 at 4:14 PM

    It is totally backwards compatible, and actually it took like 5 minutes to implement in Reactor's base code.

    The only thing to REALLY get this working would be deleting the files under reactor/project/&lt;your project&gt;/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

    Well I'm all for it of course. :) I actually haven't looked closely at recent versions of reactor... I looked at it a couple years ago and at the time it didn't *look* to me like it did as much as the onTap framework ORM (which is now DataFaucet), but then again, I was basing that judgement on the Word doc and at the time it was still incomplete.

    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

    this chaining is one of the things i really like about working with hibernate. good idea, mark.
  • 5 Mark Drew // Sep 22, 2008 at 4:14 PM

    @Ike: this post is not to convert people to reactor, I have been using it a lot lately, we have a lot invested in it and I am trying to cut out too much coding!

    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 &quot;void&quot; in a new light
  • 6 Mark Drew // Sep 22, 2008 at 4:14 PM

    @Marc: I didn't know that hibernate does this chaining too? Oh well, we have far too much of our business logic in the Reactor objects.

    somehow liking it... dont ask me why... I am perverse like that...
  • 7 ike // Sep 22, 2008 at 4:14 PM

    Oh I hadn't interpreted it as a sales pitch. :) I think I was just trying to not come across myself as being overtly trying to pitch people on DataFaucet... sure, I'd love more exposure, but after finding out about having Asperger Syndrome I'm trying to be less &quot;in your face&quot; about it when I mention it... I know there have been times that I've hacked people off that way in the past. So I'm trying to work on my people skills since I found out that I really do have a deficit in that area. :)
  • 8 Tom Chiverton // Sep 22, 2008 at 4:14 PM

    It's one of those 'sometimes useful to have' things that's also zero overhead (a lot of the time).
    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

    that's how Smalltalk used to work,

    myObject
    doThis
    doThat
    doTheOther

    it's OO goodness
  • 10 Boyloasse // Jul 21, 2011 at 10:26 PM

    Someone essentially lend a hand to make significantly articles I might state. That is the first time I frequented your web page and up to now? I surprised with the analysis you made to create this particular publish incredible. Wonderful task!
    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.