Re: My Init() in my CFC...am I on the right track?
Per Hal Helms himself- Yes, Troy. I've begun work on a new book, Designing OO Applications with ColdFusion CFCs that I hope will be helpful. -t On Thu, 30 Dec 2004 12:25:21 -0500, Jeff Small [EMAIL PROTECTED] wrote: Sean, Someone should contact Hal and see if he has plans to update the book for Blackstone. That book got me started on CFC's and I bet that a lot of people on this list would purchase the sequel. Rick Mason Dude...sign me UP. I'm getting tired of scouring bookmarks...lol. Even with Blackstone coming out, there will still be people using 6.1 for a long time, and the *principles* behind the book should be sound for 6.1 and Blackstone...syntax would change of course, but I would imagine you could take the ideas and still apply them to 6.1 from a Blackstone book...at least I'd hope. ~| Discover CFTicket - The leading ColdFusion Help Desk and Trouble Ticket application http://www.houseoffusion.com/banners/view.cfm?bannerid=48 Message: http://www.houseoffusion.com/lists.cfm/link=i:4:189257 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
Re: My Init() in my CFC...am I on the right track?
On Wed, 29 Dec 2004 13:18:43 -0500, Jeff Small [EMAIL PROTECTED] wrote: Good lord this is a lot of...ahem...stuff to remember... Yeah, that's why it takes most folks years to become 'fluent' in OO design... Cool, something else to master...lol... There's some OO basics in the first few sections of the Mach II Development Guide: http://livedocs.macromedia.com/wtg/public/machiidevguide/ (not all of the sections are Mach II specific!) And, as Dave Watts says, any primer on OO techniques (which are mostly for Java developers) should tell you most of this. There are currently no really good books on OO in CF... Hal's Discovering CFCs has some good background material but the book is out of date and the code examples show CFMX 6.0 stuff that is a long way from best practice on CFMX 6.1. Just a quick Question, then I'm off to go program for days...do you or anyone else know if Hal's planning on updating that book? Or if someone else has a 6.1 MX OOP book in the works? I'd imagine a LOT of this is going to change again once Blackstone is released (don't even get me started on Application.cfc...I don't even know WHAT that thing is...) but it would be really cool if SOMEONE were planning a book on OOP principles as applied to ColdFusion and CFCs. This is a wicked cool way to write apps in ColdFusion, and I'd love to think I was leveraging all I could from it. It seems like all my questions are really just thinly disguised best practices questions that aren't even really best practices for coding, just design... ~| 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:188992 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
Re: My Init() in my CFC...am I on the right track?
Sean, Someone should contact Hal and see if he has plans to update the book for Blackstone. That book got me started on CFC's and I bet that a lot of people on this list would purchase the sequel. Rick Mason On Thu, 30 Dec 2004 09:53:20 -0500, Jeff Small [EMAIL PROTECTED] wrote: On Wed, 29 Dec 2004 13:18:43 -0500, Jeff Small [EMAIL PROTECTED] wrote: Good lord this is a lot of...ahem...stuff to remember... Yeah, that's why it takes most folks years to become 'fluent' in OO design... Cool, something else to master...lol... There's some OO basics in the first few sections of the Mach II Development Guide: http://livedocs.macromedia.com/wtg/public/machiidevguide/ (not all of the sections are Mach II specific!) And, as Dave Watts says, any primer on OO techniques (which are mostly for Java developers) should tell you most of this. There are currently no really good books on OO in CF... Hal's Discovering CFCs has some good background material but the book is out of date and the code examples show CFMX 6.0 stuff that is a long way from best practice on CFMX 6.1. Just a quick Question, then I'm off to go program for days...do you or anyone else know if Hal's planning on updating that book? Or if someone else has a 6.1 MX OOP book in the works? I'd imagine a LOT of this is going to change again once Blackstone is released (don't even get me started on Application.cfc...I don't even know WHAT that thing is...) but it would be really cool if SOMEONE were planning a book on OOP principles as applied to ColdFusion and CFCs. This is a wicked cool way to write apps in ColdFusion, and I'd love to think I was leveraging all I could from it. It seems like all my questions are really just thinly disguised best practices questions that aren't even really best practices for coding, just design... ~| 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:188998 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
Re: My Init() in my CFC...am I on the right track?
Sean, Someone should contact Hal and see if he has plans to update the book for Blackstone. That book got me started on CFC's and I bet that a lot of people on this list would purchase the sequel. Rick Mason Dude...sign me UP. I'm getting tired of scouring bookmarks...lol. Even with Blackstone coming out, there will still be people using 6.1 for a long time, and the *principles* behind the book should be sound for 6.1 and Blackstone...syntax would change of course, but I would imagine you could take the ideas and still apply them to 6.1 from a Blackstone book...at least I'd hope. ~| 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:189005 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
Re: My Init() in my CFC...am I on the right track?
On Thu, 30 Dec 2004 09:53:20 -0500, Jeff Small [EMAIL PROTECTED] wrote: Just a quick Question, then I'm off to go program for days...do you or anyone else know if Hal's planning on updating that book? Or if someone else has a 6.1 MX OOP book in the works? I have heard that someone is writing a book about CF OO but until they provide more details publicly I can't say who... -- Sean A Corfield -- http://www.corfield.org/ Team Fusebox -- http://www.fusebox.org/ Breeze Me! -- http://www.corfield.org/breezeme Got Gmail? -- I have 6 invites to give away! If you're not annoying somebody, you're not really alive. -- Margaret Atwood ~| 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:189039 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
RE: My Init() in my CFC...am I on the right track?
Personally I really hate the notion of chaining setter calls (but I can see way others may like it). I do find that much harder to read. But other chaining is great. Being able to chain the create and the init() together seems perfectly sound to me. As does any call which returns a CFC as a property then calls a property of that return as in something like this: cfset Name = Session.SessionCFC.getUserCFC().getName() / I agree. I was only referring to setter methods (though I may not have been very clear about that). In the above example, you're using getters. A getter's purpose is to return something. Conseqently, the purpose of the line above seems very clear to me. This is in contrast to an example posted by Sean in another thread: person.setFirstName(Sean).setLastName(Corfield) I find it more difficult to read. I would be very surprised if it were any faster than two lines of code. So, I was wondering if there were any other benefits or tricks that I'm missing. Ben Rogers http://www.c4.net v.508.240.0051 f.508.240.0057 ~| 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:188925 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
RE: My Init() in my CFC...am I on the right track?
-Original Message- From: Ben Rogers [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 29, 2004 10:04 AM To: CF-Talk Subject: RE: My Init() in my CFC...am I on the right track? I agree. I was only referring to setter methods (though I may not have been very clear about that). In the above example, you're using getters. A getter's purpose is to return something. Conseqently, the purpose of the line above seems very clear to me. This is in contrast to an example posted by Sean in another thread: person.setFirstName(Sean).setLastName(Corfield) I find it more difficult to read. I would be very surprised if it were any faster than two lines of code. So, I was wondering if there were any other benefits or tricks that I'm missing. No, I agree first! ;^) I've heard one argument with setters in that it might allow for more structured sets of complex data to be read more easily. For example if you have a form accepting first, middle and last names you could say: person.setFirstName(Sean).setMiddleName(Elizabeth).setLastName(Corfield ) (I am, however, guessing on the actual middle name in this case.) In this case I can see the argument that when you put on syntax blinders you see the name of the person as it should be seen. However I agree that the following seems much more readable: person.setFirstName(Sean) person.setMiddleName(Elizabeth) person.setLastName(Corfield) The other argument I've read is when you use a setter for an object, then immediately grab something from that object. Something like: profile.setPerson(Sean).getFirstName() I see this as a little more useful than the first argument (which to me is really simplistic and should be multiple lines). At the same time cases like this seem pretty rare to me. I've also heard (what I consider completely asinine) arguments that smaller code is better or runs faster or whatever. This of course is complete bullshit (it was bullshit even in the old CF days when the language was tokenized and is even moreso now in the compiled Java world). Still - returning this in a setter doesn't force you use this, it just provides the potential. Jim Davis ~| Special thanks to the CF Community Suite Silver Sponsor - CFDynamics http://www.cfdynamics.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188931 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
Re: My Init() in my CFC...am I on the right track?
Jim Davis wrote: As for returning this from a setter... I never thought about it, but I might start (mine all return void now). I may personally never use it, but if it doesn't affect performance it leaves to possibility open for others that might want to. I return this from pretty much everything. I don't typically use it to chain methods because I find it harder to read, but if the next maintainer likes it the code is there for him to use :) One small additional benefit is where people new to the whole CFC thing use code like: cfset object = object.setVar(val) This actually works if you return this, even if it is not good practice. Jochem ~| Special thanks to the CF Community Suite Silver Sponsor - RUWebby http://www.ruwebby.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188933 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
Re: My Init() in my CFC...am I on the right track?
Jim Davis wrote: As for returning this from a setter... I never thought about it, but I might start (mine all return void now). I may personally never use it, but if it doesn't affect performance it leaves to possibility open for others that might want to. I return this from pretty much everything. I don't typically use it to chain methods because I find it harder to read, but if the next maintainer likes it the code is there for him to use :) One small additional benefit is where people new to the whole CFC thing use code like: cfset object = object.setVar(val) This actually works if you return this, even if it is not good practice. So am I to understand correctly...that you can ONLY chain methods that return this? Or that if your init function returns an object then all methods can be chained? I'm reading all of this conversation as the former (the first one) meaning that if you WANT to be able to chain a method, that method needs to return this. ~| Special thanks to the CF Community Suite Silver Sponsor - New Atlanta http://www.newatlanta.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188935 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
RE: My Init() in my CFC...am I on the right track?
Don't break the chain of love. -Original Message- From: Jeff Small [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 29, 2004 11:40 AM To: CF-Talk Subject: Re: My Init() in my CFC...am I on the right track? Jim Davis wrote: As for returning this from a setter... I never thought about it, but I might start (mine all return void now). I may personally never use it, but if it doesn't affect performance it leaves to possibility open for others that might want to. I return this from pretty much everything. I don't typically use it to chain methods because I find it harder to read, but if the next maintainer likes it the code is there for him to use :) One small additional benefit is where people new to the whole CFC thing use code like: cfset object = object.setVar(val) This actually works if you return this, even if it is not good practice. So am I to understand correctly...that you can ONLY chain methods that return this? Or that if your init function returns an object then all methods can be chained? I'm reading all of this conversation as the former (the first one) meaning that if you WANT to be able to chain a method, that method needs to return this. ~| 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:188936 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
RE: My Init() in my CFC...am I on the right track?
So am I to understand correctly...that you can ONLY chain methods that return this? Or that if your init function returns an object then all methods can be chained? I'm reading all of this conversation as the former (the first one) meaning that if you WANT to be able to chain a method, that method needs to return this. If you want to chain method calls on the same object, all of the methods in question must return this: person.setFirstName(Sean).setLastName(Corfield) In this example, you have a person component with methods setFirstName and setLastName. Each of those methods is returning this. The long way to accomplish the above is: person.setFirstName(Sean) person.setLastName(Corfield) In the other example posted, multiple CFCs are involved: Name = Session.SessionCFC.getUserCFC().getName() In this example, the getUserCFC() method belongs to the SessionCFC component. It is returning a UserCFC component, not this. The getName() method belongs to the UserCFC component, not the SessionCFC component. The longer way to accomplish the above would be: User = Session.SessionCFC.getUserCFC() Name = User.getName() Ben Rogers http://www.c4.net v.508.240.0051 f.508.240.0057 ~| Special thanks to the CF Community Suite Silver Sponsor - CFDynamics http://www.cfdynamics.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188937 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
Re: My Init() in my CFC...am I on the right track?
Jeff Small wrote: So am I to understand correctly...that you can ONLY chain methods that return this? Yes. I'm reading all of this conversation as the former (the first one) meaning that if you WANT to be able to chain a method, that method needs to return this. Correct. Jochem ~| Special thanks to the CF Community Suite Silver Sponsor - RUWebby http://www.ruwebby.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188938 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
RE: My Init() in my CFC...am I on the right track?
So am I to understand correctly...that you can ONLY chain methods that return this? Or that if your init function returns an object then all methods can be chained? I'm reading all of this conversation as the former (the first one) meaning that if you WANT to be able to chain a method, that method needs to return this. You can only chain two methods if the first method returns an object containing the second method. If you want to chain two methods that belong to a single object, the first method needs to return the object containing that method (this). If you want to chain a third method, the second method has to return an object containing the third method, and so on. So, for your purposes, all your methods need to return this if you want to chain them. Dave Watts, CTO, Fig Leaf Software http://www.figleaf.com/ phone: 202-797-5496 fax: 202-797-5444 ~| Special thanks to the CF Community Suite Silver Sponsor - RUWebby http://www.ruwebby.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188940 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
Re: My Init() in my CFC...am I on the right track?
You can only chain two methods if the first method returns an object containing the second method. If you want to chain two methods that belong to a single object, the first method needs to return the object containing that method (this). If you want to chain a third method, the second method has to return an object containing the third method, and so on. So, for your purposes, all your methods need to return this if you want to chain them. ... Good lord this is a lot of...ahem...stuff to remember... Where's a good reference for ALL of this conceptual stuff? Not the syntax, or what you can and can't write...but general conceptual here's what you're trying to accomplish by using CFCs and here's why you want to do some of the things you want to do sort of document? I've seen ALL sorts of here's how to invoke a CFC and here's how to write your first CFC but nothing on, so you want to design a good, robust CFC?...what's a good reference that's syntax independent? ~| 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:188947 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
RE: My Init() in my CFC...am I on the right track?
Good lord this is a lot of...ahem...stuff to remember... Fortunately, you don't really need to memorize it all as if it were a multiplication table. You just need to think about how objects, methods and properties work. For example, if you want to chain methods, you can see how things work within equivalent code that doesn't chain methods. Let's say you have an object foo with methods bar and baz. It doesn't matter what those methods do, just how you'd write the code to call them. cfset myfoo = CreateObject(component, foo) cfset myfoo.bar() cfset myfoo.baz() In the above code, we're creating an object instance called myfoo, then calling the bar and baz methods, each on a single line. On the second two lines, we're not doing anything to catch the returned value from each method call, so presumably it doesn't matter what values are returned by each method. On the other hand, let's say we want to be able to chain these methods together: cfset myfoo = CreateObject(component, foo).bar().baz() We already know that the CreateObject call returns an instance of foo, which contains a method bar so we can call that. To be able to call the baz method within the chain, we can see that bar needs to return the same object instance. Where's a good reference for ALL of this conceptual stuff? Not the syntax, or what you can and can't write...but general conceptual here's what you're trying to accomplish by using CFCs and here's why you want to do some of the things you want to do sort of document? I've seen ALL sorts of here's how to invoke a CFC and here's how to write your first CFC but nothing on, so you want to design a good, robust CFC?...what's a good reference that's syntax independent? Any primer on object-oriented programming, or any book introducing an object-oriented programming language such as Java, will do for this sort of thing. I don't know of anything that's CF-specific other than Hal Helms' book on components, which is a bit problematic in that it spends quite a bit of effort showing how to avoid problems that are specific to CFMX 6.0 if I recall correctly. Dave Watts, CTO, Fig Leaf Software http://www.figleaf.com/ phone: 202-797-5496 fax: 202-797-5444 ~| 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:188952 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
Re: My Init() in my CFC...am I on the right track?
We already know that the CreateObject call returns an instance of foo, which contains a method bar so we can call that. To be able to call the baz method within the chain, we can see that bar needs to return the same object instance. I wish you could see me here at my desk with that little lightbulb going off over my head... ~| Special thanks to the CF Community Suite Silver Sponsor - CFDynamics http://www.cfdynamics.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188956 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
Re: My Init() in my CFC...am I on the right track?
On Wed, 29 Dec 2004 13:18:43 -0500, Jeff Small [EMAIL PROTECTED] wrote: Good lord this is a lot of...ahem...stuff to remember... Yeah, that's why it takes most folks years to become 'fluent' in OO design... Where's a good reference for ALL of this conceptual stuff? Not the syntax, or what you can and can't write...but general conceptual here's what you're trying to accomplish by using CFCs and here's why you want to do some of the things you want to do sort of document? There's some OO basics in the first few sections of the Mach II Development Guide: http://livedocs.macromedia.com/wtg/public/machiidevguide/ (not all of the sections are Mach II specific!) And, as Dave Watts says, any primer on OO techniques (which are mostly for Java developers) should tell you most of this. There are currently no really good books on OO in CF... Hal's Discovering CFCs has some good background material but the book is out of date and the code examples show CFMX 6.0 stuff that is a long way from best practice on CFMX 6.1. -- Sean A Corfield -- http://www.corfield.org/ Team Fusebox -- http://www.fusebox.org/ Breeze Me! -- http://www.corfield.org/breezeme Got Gmail? -- I have 6 invites to give away! If you're not annoying somebody, you're not really alive. -- Margaret Atwood ~| 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:188972 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
Re: My Init() in my CFC...am I on the right track?
On Tue, 28 Dec 2004 11:39:22 -0500, Jeff Small [EMAIL PROTECTED] wrote: cfset init() Be wary of calling functions from the pseudo-constructor - if the CFC is accessed in any way, including the doc browser, it will execute this and call the function. In general I recommend *not* calling init() automatically in the CFC but requiring users of the CFC to call init() at construction time: cfset obj = createObject(component,mycfc).init()/ cffunction name=init access=private output=false returntype=string displayname=Initializes my object and creates the datasource variable Normally, people would expect init() to be public and return an instance of the CFC itself: cffunction name=init access=public output=false returntype=mycfc hint=Initializes my object and created the datasource variable cfset variables.DSN = myDataSourceName cfreturn variables.DSN cfreturn this/ /cffunction I know I could make it so that you have to pass the datasource variable name in to the function, but I figured, I can always just change it in one place in my CFC, and it's good to go...right? Yes, that's a reasonable argument. You could even give init() an optional argument to allow users to override the DSN without code changes: cfargument name=DSN type=string default=myDataSourceName/ cfset variables.DSN = arguments.DSN/ That should suit my needs, right? If I want a variable that's available to all the functions in my CFC...right? Am I thinking the right way? With the caveats about expectations for the init() function, yes, you're on the right track. -- Sean A Corfield -- http://www.corfield.org/ Team Fusebox -- http://www.fusebox.org/ Breeze Me! -- http://www.corfield.org/breezeme Got Gmail? -- I have 6 invites to give away! If you're not annoying somebody, you're not really alive. -- Margaret Atwood ~| 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:188866 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
RE: My Init() in my CFC...am I on the right track?
Well I would say you maybe working a bit too hard. If that is all you want to do you can just do this. cfcomponent cfset variables.DSN = myDataSourceName cffunction ... This sets a variable that can be used by all functions. Now the limitation of this is that you can not pass parameters to this constructor. But since you stated that you did not want to pass parameters that should not be a problem. The reason for the defacto standard of an init() method is to be able to pass parameters to some kind of constructor like mechanism. If parameters are not needed, I would feel the init() method is not needed. But I'm sure that is debatable. -- Ian Skinner Web Programmer BloodSource www.BloodSource.org Sacramento, CA C code. C code run. Run code run. Please! - Cynthia Dunning cfset init() cffunction name=init access=private output=false returntype=string displayname=Initializes my object and creates the datasource variable cfset variables.DSN = myDataSourceName cfreturn variables.DSN /cffunction I know I could make it so that you have to pass the datasource variable name in to the function, but I figured, I can always just change it in one place in my CFC, and it's good to go...right? That should suit my needs, right? If I want a variable that's available to all the functions in my CFC...right? Am I thinking the right way? Confidentiality Notice: This message including any attachments is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender and delete any copies of this message. ~| Special thanks to the CF Community Suite Silver Sponsor - CFDynamics http://www.cfdynamics.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188867 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
Re: My Init() in my CFC...am I on the right track?
A few comments here: *Typically i make my init function public so I can call it directly. This lets me do this in code: cfset mycfc = createObject(component,mycfc).init(arg1, arg2, arg3) You need to make your method do a cfreturn this for it to work right. *Your init function didn't have an argument to it. Therefore, it is kinda pointless. You can replace all of that code with just: cfset variables.dsn = However, your best bet is to make the init function take an argument so you can customize the DSN on the fly. You can still use the line above (with a real value instead of three dots of course ;) so that the CFC has a default DSN at least. *And yes, if you want a variable to availavble to all methods of a CFC, use the Variables scope. On Tue, 28 Dec 2004 11:39:22 -0500, Jeff Small [EMAIL PROTECTED] wrote: cfset init() cffunction name=init access=private output=false returntype=string displayname=Initializes my object and creates the datasource variable cfset variables.DSN = myDataSourceName cfreturn variables.DSN /cffunction I know I could make it so that you have to pass the datasource variable name in to the function, but I figured, I can always just change it in one place in my CFC, and it's good to go...right? That should suit my needs, right? If I want a variable that's available to all the functions in my CFC...right? Am I thinking the right way? ~| Special thanks to the CF Community Suite Silver Sponsor - New Atlanta http://www.newatlanta.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188868 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
RE: My Init() in my CFC...am I on the right track?
-Original Message- From: Jeff Small [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 28, 2004 11:39 AM To: CF-Talk Subject: My Init() in my CFC...am I on the right track? cfset init() cffunction name=init access=private output=false returntype=string displayname=Initializes my object and creates the datasource variable cfset variables.DSN = myDataSourceName cfreturn variables.DSN /cffunction This should work, but I would definitely accept the name of the datasource as an argument if it might change. I don't know what the purpose of this CFC is, but I can't see any reason that you would want the CFC to be modified in that case - it should be modified at the point the CFC is instantiated (I think). The only other point is that it's common (and I think very good) practice to return this from the init() method. This returns the instance of your CFC to the caller, which is most often what you want to do in the case of initialization. Something like this: ] cfset Application.MyDSNCFC = CreateObject(ThatCFC_Up_There).init() / The CreateObject() function returns the CFC instance, so you can chain the init() call it to it. Since the init() call also returns this (the CFC instance) you can assign it directly to a variable container as is. I hope I'm making sense, I've just woken up... Jim Davis ~| Special thanks to the CF Community Suite Silver Sponsor - RUWebby http://www.ruwebby.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188869 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
Re: My Init() in my CFC...am I on the right track?
On Tue, 28 Dec 2004 11:39:22 -0500, Jeff Small [EMAIL PROTECTED] wrote: cfset init() cffunction name=init access=private output=false returntype=string displayname=Initializes my object and creates the datasource variable cfset variables.DSN = myDataSourceName cfreturn variables.DSN /cffunction I know I could make it so that you have to pass the datasource variable name in to the function, but I figured, I can always just change it in one place in my CFC, and it's good to go...right? That should suit my needs, right? If I want a variable that's available to all the functions in my CFC...right? Am I thinking the right way? Not quite. 1) The access type on an init() function should be public, or you'll get an error saying the method doesn't exist when you invoke it in your calling code.. 2) If you really want to make the value of variables.DSN public (to pass back to calling code), make the cfreturn its own getDSN method, as shown below. 3) Is the cfset init() in the CFC itself? If so, it should not be. It should be in your calling code. 4) You should be using the hint attribute rather than the displayname attribute for your description. 5) One practice that has become a best practice of sorts is to return this from your init method so that you can method chain, as shown below. Granted, not everybody follows this practice, as it's more personal preference, but it's just what I'm used to now. 6) Another practice that I use, but I know many don't, is to place my CFC instance variables into a instance structure so that they're neatly stored (i.e., variables.instance.DSN instead of just variables.DSN). 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 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 Calling code: cfset variables.objMyCFC = createObject(component, path.to.my.CFC).init() cfset variables.myDSN = variables.objMyCFC.getDSN() Here's a link to a community-driven document of best practices for further reference: http://www.dintenfass.com/cfcbestpractices/ Hope this helps? 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:188873 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
Re: My Init() in my CFC...am I on the right track?
Be wary of calling functions from the pseudo-constructor - if the CFC is accessed in any way, including the doc browser, it will execute this and call the function. In general I recommend *not* calling init() automatically in the CFC but requiring users of the CFC to call init() at construction time: cfset obj = createObject(component,mycfc).init()/ Okay, this makes sense. I'm reading about 4 different sources for how to build CFCs correctly (this is the first site where we get to program from the ground floor up and not add on to an existing website, so I'm excited to try and do what I would like to see as a CFC best practice where I'm setting things up the most correct way possible. I see what you, Ian, and Raymond were saying about how I don't even really need that init function, and I was struggling with that as well, but when I consider allowing it to take an optional paramter (DSN) it starts to make much more sense. So in my code where I'm using my CFC, I'm using CFOBJECT like so: cfobject name=myObject component=mySite.myComponent So how would I use that init function using the cfobject tag? Or should I just not use that and use CreateObject() instead? cffunction name=init access=private output=false returntype=string displayname=Initializes my object and creates the datasource variable Normally, people would expect init() to be public and return an instance of the CFC itself: This part somewhat confuses me, but again, I'm using CFOBJECT and not CreateObject so I'm not really setting anything equal to anything. I'm just creating an object on the page, then manipulating it via its methods further on down the page...for example, after my CFOBJECT, the very next line is: cfset qGetNews = myObject.GetNews() cfset qGetEvents = myObject.GetEvents(5) So I'm thinking that I've instantiated an object with the CFOBJECT call, why would my init() function return the object (I mean, it makes total sense, I'm just wondering how it works, which is what I think I'm fuzzy on). Yes, that's a reasonable argument. You could even give init() an optional argument to allow users to override the DSN without code changes: cfargument name=DSN type=string default=myDataSourceName/ cfset variables.DSN = arguments.DSN/ Okay, this is definitely the first *immediate* change I'm making. ~| Special thanks to the CF Community Suite Silver Sponsor - RUWebby http://www.ruwebby.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188874 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
Re: My Init() in my CFC...am I on the right track?
Not quite. 1) The access type on an init() function should be public, or you'll get an error saying the method doesn't exist when you invoke it in your calling code.. Makes *total* sense... 2) If you really want to make the value of variables.DSN public (to pass back to calling code), make the cfreturn its own getDSN method, as shown below. 3) Is the cfset init() in the CFC itself? If so, it should not be. It should be in your calling code. I think Sean explained this pretty well, I see that now. 4) You should be using the hint attribute rather than the displayname attribute for your description. Okay, I've been using them both actually. I've been trying to make sure I use both, and I've been trying to go back thru my code every so often and make sure I'm using all the appropriate attributes. 5) One practice that has become a best practice of sorts is to return this from your init method so that you can method chain, as shown below. Granted, not everybody follows this practice, as it's more personal preference, but it's just what I'm used to now. I'm struggling with this. I don't understand even from Seans's post what purpose it serves to return this when you're using something like CFOBJECT or CreateObject() which creates an instance of an object anyway... 6) Another practice that I use, but I know many don't, is to place my CFC instance variables into a instance structure so that they're neatly stored (i.e., variables.instance.DSN instead of just variables.DSN). Hey, now that's pretty cool. 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 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 Calling code: cfset variables.objMyCFC = createObject(component, path.to.my.CFC).init() cfset variables.myDSN = variables.objMyCFC.getDSN() Keanuwhoa/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... Here's a link to a community-driven document of best practices for further reference: http://www.dintenfass.com/cfcbestpractices/ Hope this helps? Big Time, thanks so much! ~| Special thanks to the CF Community Suite Silver Sponsor - RUWebby http://www.ruwebby.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188875 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
Re: My Init() in my CFC...am I on the right track?
A few comments here: *Typically i make my init function public so I can call it directly. This lets me do this in code: cfset mycfc = createObject(component,mycfc).init(arg1, arg2, arg3) You need to make your method do a cfreturn this for it to work right. *Your init function didn't have an argument to it. Therefore, it is kinda pointless. You can replace all of that code with just: cfset variables.dsn = This makes sense now too, after seeing Sean and Dave's replies...I see what they're (and you) are talking about. However, your best bet is to make the init function take an argument so you can customize the DSN on the fly. You can still use the line above (with a real value instead of three dots of course ;) so that the CFC has a default DSN at least. cfargument required=no? *And yes, if you want a variable to availavble to all methods of a CFC, use the Variables scope. Cool. I did *something* right... ~| Special thanks to the CF Community Suite Silver Sponsor - New Atlanta http://www.newatlanta.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188877 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
Re: My Init() in my CFC...am I on the right track?
Be wary of calling functions from the pseudo-constructor - if the CFC is accessed in any way, including the doc browser, it will execute this and call the function. In general I recommend *not* calling init() automatically in the CFC but requiring users of the CFC to call init() at construction time: Oh yeah...so this reminds me... If you place the init() function in your CFC, and its job is to, let's say, create a datasource variable to use thruout the CFC (available only to the internal CFC methods) and you DON'T chain it when you create an object, you're hosed, right? If you go to the trouble of creating an init() function like that, is it then basically the idea that it's the coder's responsibility to make sure that the init() function is chained onto the end of any object creation? So if I get hit by a bus, and my replacement comes in to code after me, he (or she) would have to make sure they called that init() function anytime they were instantiating an object from my CFC...no? ~| 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:188878 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
Re: My Init() in my CFC...am I on the right track?
Jeff Small wrote: 5) One practice that has become a best practice of sorts is to return this from your init method so that you can method chain, as shown below. Granted, not everybody follows this practice, as it's more personal preference, but it's just what I'm used to now. I'm struggling with this. I don't understand even from Seans's post what purpose it serves to return this when you're using something like CFOBJECT or CreateObject() which creates an instance of an object anyway... Because that works with cfobject and createobject(), but not with cfinvoke. (read on) 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 If you have your setters return this too, you can chain them together: cfset variables.myCFC.setDSN(dsn).setMode(READONLY) Jochem ~| 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:10 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
RE: My Init() in my CFC...am I on the right track?
-Original Message- From: Jeff Small [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 28, 2004 12:18 PM To: CF-Talk Subject: Re: My Init() in my CFC...am I on the right track? I'm struggling with this. I don't understand even from Seans's post what purpose it serves to return this when you're using something like CFOBJECT or CreateObject() which creates an instance of an object anyway... It helps me (but isn't completely correct) to think of the createObject() call as a blank CFC. The init() call populates it to make it useful. A metaphor might be a bunch of blank paper - all different sizes and shapes. You first choose one (deciding that piece has the properties you need), then your write your note on it (deciding what, specifically that appropriate piece will do). Assume a Recipe CFC. The CFC instance begins as a blank index card. Your init() adds (via arguments, a database call or whatever) three properties: Name, Ingredients and Directions. In pseudo code it might look like: cfset MyRecipe = CreateObject(Get One Blank 4x5 Index Card).init(Write Recipe for Ice Cubes on Index Card) / cfoutput h1#MyRecipe.getName()#/h1 p#MyRecipe.getIngrediants()#/p p#MyRecipe.getDirections()#/p /cfoutput (The above brought to you by the Coalition to Prevent the Proliferation of Automobile Metaphors in Technical Discussion.) Jim Davis ~| 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:11 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
Re: My Init() in my CFC...am I on the right track?
From: Jochem van Dieten Jeff Small wrote: 5) One practice that has become a best practice of sorts is to return this from your init method so that you can method chain, as shown below. Granted, not everybody follows this practice, as it's more personal preference, but it's just what I'm used to now. I'm struggling with this. I don't understand even from Seans's post what purpose it serves to return this when you're using something like CFOBJECT or CreateObject() which creates an instance of an object anyway... Because that works with cfobject and createobject(), but not with cfinvoke. (read on) I read on, and I still kinda don't understand when you say, that works with cfobject and createobject(), but not with cfinvoke.. So you're saying that if I wanted to use CFINVOKE, that I wouldn't be able to unless my init() method was returning this? 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 If you have your setters return this too, you can chain them together: cfset variables.myCFC.setDSN(dsn).setMode(READONLY) Jochem Okay, that makes sense. You're able to do that because both methods (init() and setMode() both return this? ~| 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:188891 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
Re: My Init() in my CFC...am I on the right track?
cfset variables.DSN = myDataSourceName This might be a little off the best CFC practice topic, but why not create a second CFC to govern all contact with your database(s)? This way you don't have to worry about each CFC that lives in your world understanding database lingo and knowing what the datasource names are -- there's only one spot where it needs to happen. We've done this (the component is called database.cfc), and it's really helped to streamline our code. All queries and other DB-specific interactions are done through this component. Other CFCs don't need to know anything about it other than invoking the proper method and passing the right arguments. Matt ~| Special thanks to the CF Community Suite Silver Sponsor - CFDynamics http://www.cfdynamics.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188892 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
Re: My Init() in my CFC...am I on the right track?
Jeff Small wrote: From: Jochem van Dieten Jeff Small wrote: I'm struggling with this. I don't understand even from Seans's post what purpose it serves to return this when you're using something like CFOBJECT or CreateObject() which creates an instance of an object anyway... Because that works with cfobject and createobject(), but not with cfinvoke. (read on) I read on, and I still kinda don't understand when you say, that works with cfobject and createobject(), but not with cfinvoke.. So you're saying that if I wanted to use CFINVOKE, that I wouldn't be able to unless my init() method was returning this? Yes. If you use cfinvoke you use 'throwaway' objects. In one call, you create the object, initiaize it, use it and discard it again. The only way to call multiple methods of an object is to have the first one return an instance of the object, and then continue to use that instance. If you have your setters return this too, you can chain them together: cfset variables.myCFC.setDSN(dsn).setMode(READONLY) Okay, that makes sense. You're able to do that because both methods (init() and setMode() both return this? Yes. Jochem ~| Special thanks to the CF Community Suite Silver Sponsor - CFDynamics http://www.cfdynamics.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188893 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
Re: My Init() in my CFC...am I on the right track?
On Tue, 28 Dec 2004 14:18:14 -0400, Matthew Drayer [EMAIL PROTECTED] wrote: cfset variables.DSN = myDataSourceName This might be a little off the best CFC practice topic, but why not create a second CFC to govern all contact with your database(s)? This way you don't have to worry about each CFC that lives in your world understanding database lingo and knowing what the datasource names are -- there's only one spot where it needs to happen. We've done this (the component is called database.cfc), and it's really helped to streamline our code. All queries and other DB-specific interactions are done through this component. Other CFCs don't need to know anything about it other than invoking the proper method and passing the right arguments. You absolutely could (and some might say should for maintenance reasons) do that. I just didn't want to muddy the waters with OO concepts when all he was looking for was feedback on his syntax and basic usage. While it's good to be cognizant of OO patterns, that's probably a bit beyond the scope of any beginner's use of CFCs. I'd suggest getting the syntax and basic usage down, and then refactor using OO-patterns (via DAOs) later. 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:188895 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
Re: My Init() in my CFC...am I on the right track?
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 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 Calling code: cfset variables.objMyCFC = createObject(component, path.to.my.CFC).init() cfset variables.myDSN = variables.objMyCFC.getDSN() Keanuwhoa/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... 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 headconceptually anyway... ~| Special thanks to the CF Community Suite Silver Sponsor - CFDynamics http://www.cfdynamics.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188896 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
Re: My Init() in my CFC...am I on the right track?
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() Keanuwhoa/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
Re: My Init() in my CFC...am I on the right track?
Named: cfset setDSN(dsName:myDatasourceName) or cfset setDSN(dsName=myDatasourceName) Aha! This I got immediately. I understand it all now and once we were both clear on what I was doing, it makes perfect sense both in what you were doing, and syntactically what you were writing. I've seen the lower named method in 99.99% of the examples that use that method, I don't honestly recall ever seeing the upper method used. But once I see what it is, it makes perfect sense. 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. ...and this makes a LOT of sense. It's something I'm going to start trying to look at closer. 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! ...again, another terrific reason... 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. Exactly! For me it was simply force of habit... Hopefully this helps a bit more? TONS more...thanks so much! ~| 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:188899 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
Re: My Init() in my CFC...am I on the right track?
On Tue, 28 Dec 2004 12:08:54 -0500, Jeff Small [EMAIL PROTECTED] wrote: Okay, this makes sense. I'm reading about 4 different sources for how to build CFCs correctly I hope that includes both of the Macromedia Web Team docs :) http:/livedocs.macromedia.com/wtg/public/ Note that even tho' the second one is mostly specific to Mach II, there's a lot of advice about CFC and application design in terms of OO. So in my code where I'm using my CFC, I'm using CFOBJECT like so: cfobject name=myObject component=mySite.myComponent I'd strongly advise switching to cfset and createObject() - it's more flexible: cfset myObject = createObject(component,mySite.myComponent)/ This allows you to chain method calls, as others have noted, and will probably help get you more into the habit of using cfset and method calls (rather than cfinvoke which I also advise against). Why? I think there's a psychological hint behind cfobject and cfinvoke - they look like old-school tags and, hence, old school thinking; whereas if you make a conscious effort to switch to cfset and use createObject() / method calls directly, you'll get a hint that this isn't just old-school tags and it'll help you switch gears into new school (OO / CFC) thinking. This part somewhat confuses me, but again, I'm using CFOBJECT and not CreateObject so I'm not really setting anything equal to anything. Which just reinforces my point about psychological hints above... -- Sean A Corfield -- http://www.corfield.org/ Team Fusebox -- http://www.fusebox.org/ Breeze Me! -- http://www.corfield.org/breezeme Got Gmail? -- I have 6 invites to give away! If you're not annoying somebody, you're not really alive. -- Margaret Atwood ~| 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:188900 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
RE: My Init() in my CFC...am I on the right track?
This allows you to chain method calls, as others have noted, and will probably help get you more into the habit of using cfset and method calls (rather than cfinvoke which I also advise against). Just to be clear, are you advising people to chain method calls? Personally, I'm not crazy about this practice. I find it much more difficult to read code that does several different things on one line. Additionally, it just seems unintuitive to have a setter method return a variable. If a setter returns a variable, I have to decide if it's meaningful in some way or if the guy before me was just being cute to save himself from having to type a few extra characters. Anyway, I'm just wondering if there's something I'm missing here. Ben Rogers http://www.c4.net v.508.240.0051 f.508.240.0057 Why? I think there's a psychological hint behind cfobject and cfinvoke - they look like old-school tags and, hence, old school thinking; whereas if you make a conscious effort to switch to cfset and use createObject() / method calls directly, you'll get a hint that this isn't just old-school tags and it'll help you switch gears into new school (OO / CFC) thinking. This part somewhat confuses me, but again, I'm using CFOBJECT and not CreateObject so I'm not really setting anything equal to anything. Which just reinforces my point about psychological hints above... -- Sean A Corfield -- http://www.corfield.org/ Team Fusebox -- http://www.fusebox.org/ Breeze Me! -- http://www.corfield.org/breezeme Got Gmail? -- I have 6 invites to give away! If you're not annoying somebody, you're not really alive. -- Margaret Atwood ~| 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:188902 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
RE: My Init() in my CFC...am I on the right track?
-Original Message- From: Ben Rogers [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 28, 2004 4:35 PM To: CF-Talk Subject: RE: My Init() in my CFC...am I on the right track? This allows you to chain method calls, as others have noted, and will probably help get you more into the habit of using cfset and method calls (rather than cfinvoke which I also advise against). Just to be clear, are you advising people to chain method calls? Personally, I'm not crazy about this practice. I find it much more difficult to read code that does several different things on one line. Personally I really hate the notion of chaining setter calls (but I can see way others may like it). I do find that much harder to read. But other chaining is great. Being able to chain the create and the init() together seems perfectly sound to me. As does any call which returns a CFC as a property then calls a property of that return as in something like this: cfset Name = Session.SessionCFC.getUserCFC().getName() / Without chaining I would find that much harder to read and use (especially when pulling many properties like this). I think it's personal preference. As for returning this from a setter... I never thought about it, but I might start (mine all return void now). I may personally never use it, but if it doesn't affect performance it leaves to possibility open for others that might want to. I like those kind of there if you want it kind of compromises. ;^) Jim Davis ~| Special thanks to the CF Community Suite Silver Sponsor - RUWebby http://www.ruwebby.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188903 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54
Re: My Init() in my CFC...am I on the right track?
If parameters are not needed, I would feel the init() method is not needed. But I'm sure that is debatable. Hey Ian, Not trying to debate, just giving my point of view: I'd use the init() method regardless of the need for parameters. This way, if parameters need to be added, the external interface for the CFC doesn't need to change. -Joe -- For Tabs, Trees, and more, use the jComponents: http://clearsoftware.net/client/jComponents.cfm ~| Special thanks to the CF Community Suite Silver Sponsor - New Atlanta http://www.newatlanta.com Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188908 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=11502.10531.4 Donations Support: http://www.houseoffusion.com/tiny.cfm/54