Hello all,

I have a question about encapsulating the names of the instance variables in a 
component.  Let's take the init() method from Sean's article.cfc example found at 
http://livedocs.macromedia.com/wtg/public/machiidevguide/article.cfc.txt:

<cffunction name="init" access="public" returntype="acr.model.article" output="false"
        displayname="Article Constructor" hint="I initialize an article.">
  <cfargument name="data" type="struct" default="#structNew()#"
        displayname="Initial Data" hint="I am the initial values of the article data 
fields." />
  <cfset variables.instance = arguments.data />
  <cfset variables.missingFields = "" />
  <cfset variables.invalidFields = "" />
  <cfreturn this />
</cffunction>

In order to populate an Article object one needs to pass in a struct that has all the 
required names for the component's instance variables.

An alternative approach is to use a bunch of <cfarguments>:
<cfargument name="Title" >
<cfargument name="Description" >
...

I would love to hear someone's thoughts on the important tradeoffs between the two 
approaches above.  

It seems to me like using a struct to initialize a component (and using an 
argumentCollection) seriously breaks enscapsulation because the user of the component 
must know the name of all your required instance vars.  If you change the name of your 
instance variable inside your component, all instances where a component is being 
initialized must be changed as well.

Using <cfargument> seems to have better encapsulation, but it is more code to write.  
(<cfargument> also gives you some checking of the argument type and whether it is 
required, but Sean has a Validate() method in article.cfc to take care of that and 
more robust validation for any updates to the component - which I think is quite cool).

Thanks,
Jon


----------------------------------------------------------
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