Replies inline...

On Tue, 28 Dec 2004 15:00:07 -0500, Jeff Small <[EMAIL PROTECTED]> wrote:
> >> It should be noted that you'll get slight variations from people based
> >> on their preferences, but the code below generally covers best
> >> practices that I've seen. Here's the cleaned up CFC and the calling
> >> code is below it:
> >>
> >> <cfcomponent output="false">
> >> <cffunction name="init" access="public" output="false"
> >> returntype="CFCName"
> >> hint="Initializes my object and creates the datasource variable">
> >>    <cfset setDSN(dsName:"myDataSourceName")>
> >>    <cfreturn this>
> >> </cffunction>

All this does is call an internal private method called setDSN() to
set the datasource name. When I originally read your e-mail, I thought
that you wanted to eventually be able to pass back the name of the
datasource to your calling code (i.e., my_template.cfm needs the name
of the datasource). From your follow-up posts, it looks like all you
really want to do is use the datasource name in that CFC, in which
case the getDSN/setDSN method really aren't necessary. Apologies for
any confusion there.

> >>
> >> <cffunction name="getDSN" access="public" output="false"
> >> returntype="string"
> >> hint="Returns the name of the datasource">
> >>    <cfset variables.DSN = "myDataSourceName">
> >>    <cfreturn this>
> >> </cffunction>
> >>
> >> <cffunction name="setDSN" access="private" output="false"
> >> returntype="void"
> >> hint="Sets the name of the datasource">
> >>    <cfargument name="dsName" type="string" required="true" hint="The
> >> datasource name to set">
> >>    <cfset variables.DSN = arguments.dsName>
> >> </cffunction>
> >> </cfcomponent>

Again, if you don't want your calling code (the CFM template that
invokes the CFC) to access the datasource name, then you really don't
need these two methods -- you can use your initial approach of
directly setting the variables.DSN value in your init() method and
chop the methods out. Any other method within your CFC that needs to
use variables.DSN can then directly refer to it rather than having to
call getDSN() to get its value.

> >>
> >> Calling code:
> >>
> >> <cfset variables.objMyCFC = createObject("component",
> >> "path.to.my.CFC").init()>
> >> <cfset variables.myDSN = variables.objMyCFC.getDSN()>

Your calling code (again, the CFM template that invokes the CFC) now
only needs the first line to actually create an instance of your CFC:

<cfset variables.objMyCFC = createObject("component", "path.to.my.CFC").init()>

At this point, you have both created a new instance of the CFC and
called its init() method at the same time. You now have a full-fledged
CFC object and you can simply call other methods in that CFC using
this syntax:

<cfset variables.myQueryResult = variables.objMyCFC.getMyData()>

> >
> > <Keanu>whoa</Keanu>
> >
> > I think I need to print that out and go thru it slowly. Is your calling
> > code
> > in your page? Why do you use your "getDSN" function after you've already
> > created an object and initialized it? If possible...could you please just
> > sort of briefly explain the three functions? You're using some weird
> > syntax
> > in your init (dsName:"myDataSourceName") that I don't
> > kinda...well..."get"...

I understand there's a lot to take in at the beginning!! As to the
first question, the calling code is the CFM template that actually
creates the CFC instance, and is where the variables.objMyCFC line of
code would go -- *not* in the CFC itself. As I said before, I put in
the getDSN line because I thought your *calling code* needed that
value for some reason. Since it turns out it doesn't, then you can
eliminate that line altogether.

Lastly, the init syntax I used for setDSN() is an alternative way of
calling CFC methods that some people don't use and some do -- I do.
When invoking CFC methods, you have the ability to use named arguments
or use positional arguments. Named arguments, as you might guess, is
when you explicitly note the argument name that the value applies to.
So, taking the setDSN() method from earlier, you'll see the I've
created a cfargument called dsName in the setDSN() method itself that
is expecting a string value. In the init() method, where I called the
setDSN() method, I simply put the name of the argument in front of the
value for clarity. Positional notation, like most other programming
languages, is simply putting the values in the correct order without
specifying the argument name that it goes with. ColdFusion then
implicitly takes the first value and associates it with the first
cfargument in the method being called, the second value with the
second cfargument, and so on. So you can write the setDSN() method
call in three ways (two using named arguments and one using positional
arguments:

Named:
<cfset setDSN(dsName:"myDatasourceName")>
or
<cfset setDSN(dsName="myDatasourceName")>

Positional:
<cfset setDSN("myDatasourceName")>

Any of the above is perfectly valid. I personally prefer named
arguments because it makes my calling code more explicit in that I
don't have to go open the CFC to see what the argument name is that
I'm passing in. Further, if you use positional argument notation and
the order of your arguments gets messed up, then you will be setting
the wrong values for each argument! I should note that many prefer
positional notation because it's just like Java, JavaScript, etc.,
where you just pass in the value, and it keeps your coding guidelines
more consistent.

> 
> Man, I still don't really get this...well, I understand pretty much
> everything that everyone's said in this thread pretty much up to here...the
> above code is sort of confusing. I mean, it's not "syntax" confusing, I can
> follow it. I just don't really "get" what it's accomplishing, and what the
> obvious benefits are for the above code. Don't get me wrong, as soon as the
> lightbulb goes off, I'm all about it, but right now, it's mainly just the
> above code that's making me scratch my head....conceptually anyway...

Hopefully this helps a bit more? I know I probably threw a ton at you
and I don't want to put 100 different things to remember in your head
and confuse you even more!

Regards,
Dave.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Special thanks to the CF Community Suite Gold Sponsor - CFHosting.net
http://www.cfhosting.net

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188898
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to