By Peter Bell

My First CFDJ Article: Encapsulating Recordsets

Well, my first CFDJ article is now available online. It is a pretty good introduction to the idea of the Iterating Business Object that I've been promoting and how and where you might consider using it.

Comments
Pete,

This looks very cool. I can't wait to dive into the IBO code and gain a full understanding of how it works. Excellent article and explanation. Looking forward to integrating it into some of my own testing.
# Posted By Ben Nadel | 11/21/06 5:28 PM
Thanks! And should be more code goodness coming shortly on the model, controller and view fronts!
# Posted By Peter Bell | 11/21/06 5:40 PM
Hi Peter, I thought I had posted a comment but I cannot see it on your site, odd??

by the way good article, and the baseObject is very interesting from what i've played around with. A little off topic but I have a query.

Using Lightwire and have a userObject (with the extended baseObject) and what point do I call loadQuery and how should I pass the query in?
# Posted By Andy Jarrett | 11/21/06 8:05 PM
Hi Andy,

Very occasionally comments don't post correctly, so that might have been the problem - sorry about that! I'll be posting a bunch more of integrating LightWire with the IBO (which is part of what will be the LightBase framework - set of base classes for more quickly developing small to mid sized applications.

However, short answer is as follows. I am still experimenting with this, but in current architecture (full implementation), lets say you wanted to load up one or more users and return them in UserObject. You'd get your controller to call UserService - lets say you wanted all users in a single bean, so it'd be UserService.getbyFIlter(). UserService.getbyFilter calls UserDAO.getByFilter() which returns a recordset. UserService calls LightWire.getTransient(UserObject) and then loads the initialized bean.

So UserService.getbyFIlter() looks something like:

var UserQuery = UserDAO.getByFilter();
var UserObject = LightWire.getTransient("UserObject");
UserObject.LoadQuery(UserQuery);
cfreturn UserObject

Of course, this code is really in BaseService.getbyFilter(), so you don't actually have to write anything unless you want to overload it. Same goes for the DAO method.

I'm keeping on playing with this, but this is what I currently have!

Any inputs or improvements appreciated, and expect some bigger sample code over Thanksgiving.
# Posted By Peter Bell | 11/21/06 11:55 PM
Peter, I looked at the code on CFDJ and I created another object that extended the BaseObject. When I tried to load it, it seems that a method is missing "variables.setDefaultValues()" isnt in the sample code. Any chance of an update?

I am trying to show better ways of using OO with large recordsets (see my post: http://www.markdrew.co.uk/blog/index.cfm/2006/11/2...)
# Posted By Mark Drew | 11/24/06 3:15 AM
Strange! I'll up up and post the current IBO later today and in fact in needs a little spring clean, so I'll also clean up the code and post it. Possible I did something silly with the code for CDFJ - will test it out and download as it was a "cleaned up" version of the code I actually run.

Sorry again. Expect code later today as a couple of quick emergencies for clients (as usual!) first.
# Posted By Peter Bell | 11/24/06 12:19 PM
Hi Peter, great article. Any change that you could add the latest code to this blog post, or even add a downloads pod ;)

Many thanks
# Posted By Nick Tong | 3/18/07 2:28 PM
Hi Nick,

I've added a "download" link at the top with the cfc zipped up. Please note I had to cut and paste this together as I have two objects - BaseGetSetObject and BaseBusinessObject - the first of which handles generic getters and setters and the second (which extends the first) handles the iterating. Both also have a bunch of other metadata and related features, so I've tried to cut out all the irrelevant methods but haven't had a chance to test the code, so I don't guarantee there isn't an error or two (like a dependency on a method or composed object I've removed), but the methods all work and are the basis of everything I do, and they're simple enough to hack out and reuse as you like, so hopefully they'll be of some use!

Best Wishes,
Peter
# Posted By Peter Bell | 3/18/07 5:47 PM
Let me get this straight...

loadQuery() takes a query containing a single record and reads/populates the bean? loadStruct() takes a structure which is used for child objects in "has-a" or "has-many" relationships?

Is that right?

I'm not sure because I am also thinking that loadQuery() may be used for collections, in which case the query passed in would contain many records.
# Posted By Aaron Roberson | 3/20/07 5:14 PM
Hey Aaron,

Good question. Load query is for loading a recordset containing 0..n records (a collection). The loadstruct is for loading a single record from a structure such as loading a bean from the form scope.
# Posted By Peter Bell | 3/20/07 11:08 PM
So if I want to populate the bean from the DAO I would use queryToStruct() and then pass the struct into bean.loadStruct() right? Just making sure.

Also, I am getting an error that says MetaData is undefined. MetaData is in the IBO init().
# Posted By Aaron Roberson | 3/21/07 11:31 AM
One more thing...

Would you use loadQuery() for child objects in a one-to-many relationship (i.e. user has many addresses - pass a query of addresses filtered by the userid into the user.loadQuery() method)?
# Posted By Aaron Roberson | 3/21/07 12:01 PM
Hi Aaron,

You can load straight from the DAO using loadQuery - 0..n records includes the special case where n = 1

Dang - the metadata is a piece I tried to pull out as I happen to use a metadata config object, so you might have to pull out a reference or two to that.

For child objects, you'd create another IBO, loadquery into that and then if you had a child called Addresses, you'd User.set("Addresses", Addresses) to load the sub object.
# Posted By Peter Bell | 3/21/07 2:33 PM
Sorry, but how do I use LightWire to inject LightWire into my service layer?
# Posted By Aaron Roberson | 3/22/07 11:12 AM
Actually, it's auto injected. In any object that is created by LightWire, you can just call LightWire.

So in UserService.new() you could call LightWire.getTransient("User") and it'll work.
# Posted By Peter Bell | 3/22/07 11:29 AM
hi peter
i'm approaching now with oop, then sorry for my "newbie" questions...
i have to develop an application and i'd like to use your IBO.
my questions are the following:

1)calling the init() method i receive the same error of Aaron (MetaData is undefined),
but i didn't understand how to fix it.

2)when i'm looping over a query (loaded with the loadQuery() method) should i have a code like this?

<cfloop from="1" to="#obj.recordCount()#" index="i">
...
...
<cfset obj.next()>
</cfloop>

3)when could be useful for my applications the setDefaultValues() method?

thank's

Andrea
# Posted By Andrea | 11/7/07 11:09 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.