return an array of objects as opposed to a query object

1 view
Skip to first unread message

Dutch Rapley

unread,
Dec 17, 2008, 4:49:02 PM12/17/08
to reacto...@googlegroups.com
This is my first ColdFusion feature request ever - http://is.gd/ccsK. Relevant to Reactor since it's a db/cfquery request.

I think I'm going to do some experimenting with the Reactor source to perform requested functionality from within reactor.

I'll define two different methods:
  1. Return an array of value objects
  2. Return an array of structures with a key of __type__
    __type__ is used by ActionScript to tell it which vo AS should convert it to
    this is way faster than creating an array of objects in CF
Can you imagine what kind of potential this has when you combine Reactor with ColdBox's remote proxy and Flex?

-Dutch

Tom Chiverton

unread,
Dec 18, 2008, 8:24:05 AM12/18/08
to reacto...@googlegroups.com
2008/12/17 Dutch Rapley <dutch....@gmail.com>:

> I'll define two different methods:
> Return an array of value objects
> Return an array of structures with a key of __type__
> __type__ is used by ActionScript to tell it which vo AS should convert it to
> this is way faster than creating an array of objects in CF

I'm not sure this should be Reactor's job. Frameworks like ColdSpring
can already use AOP to achieve this transparently.
However, I do like the idea of pushing it down into the CF core.

--
Tom

Dutch Rapley

unread,
Dec 18, 2008, 9:35:49 AM12/18/08
to reacto...@googlegroups.com
I should have clarified my other intentions instead of just returning a record set that is flex ready. The idea is that you would be able to specify additional/custom properties for each record in the context of the record set.

Currently, when we use createRecord(), we can go into DbData/Record/TableNameRecord.cfc and add aditional properties to an object that are available to us when we use the object.

With createGateway(), we don't have the option to add additional properties to each record on the fly for use in our views. For custom properties, you have to define logic as you use it in your views.
 
I'm not sure this should be Reactor's job. Frameworks like ColdSpring
can already use AOP to achieve this transparently.

I'll disagree here. I don't think our answer should be, "yes, you can do this, but it has to be through AOP in ColdSpring," or "yes, you can do this, but it has to be done this way in Model-Glue or like this in ColdBox."

There are a great deal of developers who might not use ColdSpring, let along understand how to implement AOP with it. Or, they may be using Lightwire instead. Instead of Model-Glue, Mach-II or ColdBox, some developers might use Fusebox. I think some funcntionality is worth including so that we can reach beyond the small Reactor + IoC Framework A + MVC Framework B niche.

I think it's worthy, at least for me, to explore the potential here. I've taken a look at the source and it would be fairly eay to implement this. It could come down simply passing DbData/Record/TableNameRecord.cfc to a function that iterates though the returned query object, looping over a record set and creating the array of objects.

This makes a lot of sense, the TableNameRecord.cfc already has all the setters in place and you only have to add the getter for the custom property once in your code. I'm not saying this should be the default way Reactor returns a recodset, but maybe an additional function like getAllAsObjects. What we're getting in the end ins't just an array of objects, but an array of reactor record objects.

-Dutch

Dutch Rapley

unread,
Dec 18, 2008, 4:11:48 PM12/18/08
to reacto...@googlegroups.com
Ok, I've done this and without actually touching the reactor source code.

I've attached a file called gatewayHelper.cfm. You can place this file in 1 of 2 places:
  1. Place it in ScratchData/Gateway and include it in the TableNameGateway.cfc files you wan to use getAllAsObjects()
  2. Place it in reactor/base and include gatewayHelper in abstractGateway.cfc to let all tables have this function by default.
Example Code

<cfset userRecord= Reactor.createRecord("Users") />
<cfset records = Reactor.createGateway('Users').getAllAsObjects(userRecord) />

<cfloop from="1" to="#Arraylen(records)#" index="i">
<div>
    #records[i].getID()# -
    #records[i].getFirstName()#
    #records[i].getLastName()#
</div>
</cfloop>

What you get as a result is an array of Reactor record objects.

WHAT'S THE ADVANTAGE?

In ScratchData/Record/userRecord.cfc, you can add a function called getAge() that may calculate a person's age based on their birthday in the database. When you iterate through the record set, getAge() is avalialbe for you to use as a read-only property (no setter needed).

And for those of you who use ColdSpring or any other IoC framework, the first line in the example code won't even be necessary.

To all of you who have worked on Reactor, you've done a really nice job! Enjoy!

-Dutch


gatewayHelper.cfm

Dutch Rapley

unread,
Dec 19, 2008, 11:02:04 AM12/19/08
to reacto...@googlegroups.com
Thinking about it last night, my example only covers the getAll() function on createGateway(). I'll take a look into it next week to see about extending to cover filtered record sets.

I feel what I do have is a good starting point.

-Dutch
Reply all
Reply to author
Forward
0 new messages