ColdFusion 9 Feature Request: cfquery resultType
This is one I really want to see happen as it is HIGHLY useful, at least to me.
Idea
When querying a database I want to return a query but a query of a cfc instances (a value object [or bean...pending your terminology]). I don't want to return an Array of Structs though.
Code Proposal
On cfquery, add a resultType attribute. This attribute would take a path to a CFC. The resultType CFC should be nothing more than a value object (bean) based on the spec required by Adobe.
**Code Example **
[cfquery.cfm]
<cfquery name="mydata" resultType="cfcs.Product">
SELECT productID, name
FROM products
</cfquery>
<cfoutput>
#mydata.getProductName()[1]#
</cfoutput>
[cfcs/Product.cfc]
<cfcomponent>
<cfscript>
variables.productID = 0;
variables.name = "";
</cfscript>
<cffunction name="init" access="public" returntype="string">
<cfreturn this />
</cffunction>
<cffunction name="getProductID" access="public" returntype="numeric">
<cfreturn variables.productID />
</cffunction>
<cffunction name="setProductID" access="public" returntype="void">
<cfargument name="productID" type="numeric" required="yes" />
<cfset variables.productID = arguments.productID />
</cffunction>
<cffunction name="getName" access="public" returntype="string">
<cfreturn variables.name />
</cffunction>
<cffunction name="setName" access="public" returntype="void">
<cfargument name="name" type="numeric" required="yes" />
<cfset variables.name = arguments.name />
</cffunction>
<cffunction name="getDisplayName" access="public" returntype="string">
<cfreturn variables.name & " (" & variables.productID & ")" />
</cffunction>
</cfcomponent>
Time Saving Tip
This could be annoying to some (having to write extra code) but a query would still be a query if you didn't specify the resultType. You also could use onMissingMethod to get/set your variables instead of writing every single one of the getters/setters. Another option is to use or write a code generator.
Let's go Adobe...what'cha think?
Categories
ColdFusion0 TrackBacks
Listed below are links to blogs that reference this entry: ColdFusion 9 Feature Request: cfquery resultType.
TrackBack URL for this entry: http://mt.johncblandii.com/mt-tb.cgi/104


While it would be very handy, a lot of people just solve this problem in a common reusable part of their project, such as by using AOP.
In that sort of tiered environment I'm not sure you want the DB access layer to know about the value objects.
True for those using frameworks. Remember, this is 100% optional. I have a custom framework which has a different approach and this would be an absolute gem for me. My "dao" could easily take a resultType argument in my read/view functions which would return my "objectized" query.
As usual with CF features, you won't please everyone. Remember interfaces in CF? :-D How long did that take to get into CF? :-)
Thanks for the framework point of view though.
Sorry, disagree.
This is muddying the abstraction between data retrieval and business objects, so I think it's a poor idea; it's also dead easy to implement already; and it's a pretty niche requirement.
I realise the idea of CF is "feature automation", but I think the Adobe position is more focused at front-end user stuff like PDFs, charts, AJAX UI elements etc. Personally, I never use any of that nonsense, but equally if I need "back-end" code written, I'd prefer to either write it myself, or use something someone else has already written to effect the same thing (Reactor, for example).
I don't think this is a valuable feature to recommend for CF9.
Good on you for floating suggestions though: one never knows how many people might think the same thing, unless one puts the suggestion forward in the first place.
--
Adam
If I may...are you a person who prefers to use frameworks?
I talked with Camden earlier (via email) and he feels the same (regarding the abstraction). My thoughts are a bit different.
I don't know the underpinnings (java code) but they could probably use a RowMapper to map a query to a specific object. I know in Java you can but maybe that was just a Spring implementation (I am presently a Java novice at best). A RowMapper, IMO, doesn't blur the lines but provides a faster approach to a common problem.
So, frameworks have this licked...I def' get that but I think there is something to gain here for a group of dev's.
- Speed wise doing it on the Java side should be faster.
- Not everyone uses frameworks or likes OPC (other people's code; lol). I'm in this group.
- This already happens if you use Flash Remoting to call a CFC and have the meta mapped properly so it isn't like this is foreign or overall a "poor idea" for Adobe to consider.
I simply don't like the idea of getting a query back, looping over all records, creating an instance of my CFC, and setting the properties for it then stuffing it in an array. That's just my opinion and preference.
Thx for the comment. Feel free to respond to the above. :-)