Your views should NEVER be calling anything to modify your business object,
so it's unneccesary (and generally a bad idea) to send a reference to a BO
to the view code.  

However, sending a memento isn't always good enough either.  Say you need to
display a some info about a person, including their age.  Your Person BO has
a getAge() method, which is very convinient, however, internally it doesn't
store an age (for obvious reasons), but stores the birth date of the Person.
If you just pass the BO's memento to your view, then you'll have to manually
compute the age from the birth date in your view code, which completely
breaks encapsulation, which is the whole point of using objects to begin
with.

A third option you didn't mention is to have your controller set a bunch of
variables like this:

<cfset obj = request.personManager.getPerson(id) />
<cfset name = obj.getName() />
<cfset age = obj.getAge() />

This works, but again, is generally a bad idea.

The "ideal" solution is to use a data transfer object (DTO).  It's a
lightweight version of your BO, that only has getter methods.  It takes a BO
memento upon construction, but provides no way to modify it, so it can be
safely passed to the view components.  The downside here is that you have to
duplicate the getter methods in the BO and the DTO, but that's a small price
to pay to keep the encapsulation unbroken.  I think you might even be able
to dynamically construct a DTO with the BO's getter methods (assuming CFCs,
of course), and save yourself the duplication, but I'm not sure about that.


For small/simple applications, it's often perfectly acceptable to pass your
BO to the view, but for larger apps, using DTOs is a good idea.  

Cheers,
barneyb

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Gabriel Roffman
> Sent: Tuesday, December 09, 2003 5:49 AM
> To: Cfcdev
> Subject: [CFCDev] do you return an obj or a memento from your manager?
> 
> Do you like to return the whole object or just the structure 
> of its data
> when you want to display it in a page?  I've started using 
> Sean's approach
> as detailed in the model section of the Mach II development 
> guide.  What I
> have found is that most times I want to display the data of 
> the object, but
> the data isn't there unless I call the getInstanceMemento 
> function.  But I'm
> sure there will be times when I want to have functions inside 
> of the object
> that I want to call as I display.  So now I'm taking this 
> approach.  What do
> you think of this approach?
> 
> <cfset territoryObj = 
> request.territoryService.getTerritory(territoryID)>
> <cfset territory = territoryObj.getInstance()>
> 
> 
> Gabe Roffman
> 
> www.etesters.com
> 
> 
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
> in the message of the email.
> 
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
> 
> 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' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

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

Reply via email to