Jason,

I looked at this for a minute, and began to get a feel for the problem. Then
i remembered running into it and being stuck for awhile how to solve it. On
the few occasions i've worked with composed objects so far, guess what ...
my DAO's wound up being composed in a parallel way. I liked the way that
worked out. It feels very clean as a design.

Here's some sample code, which although from a very different model, might
help out. The code is from PageDAO, and in this model a Page can be in
multiple languages (languages are configurable). So i've got an array of
PageLan's composed within Page that handle the language specific stuff.

PageDAO has an init() where i pass in Page and then get the array of PageLan
objects that are composed within Page, and it has persist(), create(),
update(), read(), and delete() methods. create(), update(), read(), and
delete() first deal with the properties specific to page, then they delegate
the PageLan work to each child dao in the array of PageLanDAO's.

You'll note that i'm using a queryRowToStruct() function here so that i can
conveniently hold the object instance in a struct (that's how i'm doing
it) - you'll find the UDF on cflib.org in case you want to examine the
workings of this in depth.


Here's what PageDAO.persist() looks like

<cffunction name="persist" access="public" returntype="VOID" output="false">

        <cfif IsNumeric(variables.Page.getIdPage())>
                <cfset update() />
                <cfloop query="variables.activeLans">
                        <cfset 
createObject('component','someMapping.dao.PageLanDAO').init(
                                variables.PageLan[strLang],
                                variables.adminManager).update() />
                </cfloop>
        <cfelse>
                <cfset create() />

                <cfloop query="variables.activeLans">
                        <cfset 
variables.PageLan[strLang].setIdPage(variables.Page.getIdPage())
/>
                        <cfset 
createObject('component','someMapping.dao.PageLanDAO').init(
                                variables.PageLan[strLang],
                                variables.adminManager).create() />
                </cfloop>
        </cfif>

</cffunction>

Here's what the read in PageDAO looks like:

<cffunction name="read" access="public" returntype="VOID" output="false">
        <cfargument name="idPage" type="numeric" required="yes" />

        <cfset var qRead = "" />

        <cfquery name="qRead" datasource="#variables.dsn#">
                SELECT *
                FROM tblPage
                WHERE idPage = <cfqueryparam cfsqltype="cf_sql_integer"
value="#arguments.idPage#" />
        </cfquery>
        <cfif qRead.RecordCount GT 0>
                <cfset 
variables.Page.setInstanceFromStruct(queryRowToStruct(qRead)) />
        </cfif>

        <cfloop query="variables.activeLans">
                <cfset 
createObject('component','someMapping.dao.PageLanDAO').init(
                        variables.PageLan[strLang],
                        variables.adminManager).read(arguments.idPage,strLang) 
/>
        </cfloop>

</cffunction>

I hope that helps. I really do! I'm a beginner at this, but at each step i
really try and take the time to get something "right". And this approach to
handling composed objects w/ DAO's felt like it clicked.

ciao,
Nando

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of [EMAIL PROTECTED]
Sent: Tuesday, July 19, 2005 3:13 PM
To: [email protected]
Subject: [CFCDev] Best way








I have my objects divided into  BO, DAO, Gateway and Manager Object.  Many
of my objects are composed of other objects.  For instance I have a User
object which is composed of a Person object and an array of Company objects
they sponsor.  I'm trying to figure out the best place to load my Person
object from the database when I instantiate the User.

In my UserManager I do the following

      <cffunction name="load"  returntype="objects.user" hint="I load a
user into memory" >
            <cfargument name="person_id" type="numeric" hint="I am the
person_id of the user">
            <cfset var myUser =
createObject("component","objects.user").init() />  <!--- returns a blank
User Object --->
            <cfset myUser.setPerson_id(arguments.person_id)>
            <cfset variables.userDAO.read(myUser) /> <!--- loads some data
such as their security profile --->

            <cfreturn myUser />
      </cffunction>

Now, I need to load the person information from the database.  Ultimately
the code that knows how to do this is in the personDAO.  I have a method
that returns a Person object from the person_id in my personManager (which
is instantiated in the application scope).  It could be called with the
following:

application.personManager.getPersonByPersonId(arguments.person_id)

Should I call this method in the UserManager above, in the UserDAO's read
method or in the User object's setPerson_id() method?
Should my UserManager, UserDAO or User object know about the personManager
in the application scope or should I just go ahead and instantiate it
locally in the method or in the variables scope?

Suggestions?


Jason Cronk
[EMAIL PROTECTED]







----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[email protected] with the words 'unsubscribe cfcdev' as the subject of the
email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
(www.cfxhosting.com).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]






----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to 
[email protected] with the words 'unsubscribe cfcdev' as the subject of the 
email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting 
(www.cfxhosting.com).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

An archive of the CFCDev list is available at 
www.mail-archive.com/[email protected]


Reply via email to