Recordset: Create Iterating Domain Objects via Inheritance

2007 May 26
by Paul Marcotte
Now bundled with its distant cousin, the composite Iterator, Recordset provides methods and properties for list traversal via inheritance. Recordset is not a query, but rather a query that is converted to an array of structs. Extending Recordset is useful as an alternative to maintaining an array of objects, or if you want to display a list with calculated fields (an employee has a startdate, but display format is "years of service").I chose Recordset as the name, because it provides similar methods to an ADO Recordset. Recordset has the following public methods: currentRow():numeric - the index of the current record.
next():boolean - the next record is set.
previous():boolean - the previous record is set.
reset():void - reset the index.
end():void - set the index to the last record
pageCount():numeric - if pagination is desired, returns the number of pages in the Recordset.
recordCount():numeric - total number of records.
loadQuery():void - accepts a ColdFusion query
setPage():void - set desired page for next iteration (used only with pagination).
How to use Recordset: If your transient objects do not extend another class, you can simply extend Recordset. <cfcomponent displayname="User" extends="com.fancybread.util.list.Recordset" output="false">
   <cfset variables.instance = StructNew() />
To display a list of users by role, first instantiate the user list and set the user role and load the list in your controller. <cfset userList = variables.UserService.getUser() />
<cfset usersByRoleQuery = variables.UserService.getUsersByRole() />
<cfset userList.loadQuery(usersByRoleQuery) />
Loop through the list in your display view while next() returns true. <cfloop condition="">
{display code}
Many thanks go out to Aaron Roberson for recommending inheritable iteration among other excellent suggestions that are implemented with this release.