Re: Invoking components, methods and storing instance in application scope
Hello Tony. I am no expert, But I do know about where you are at right now, and I also know what it took for me to learn it, so I will give it a shot to at least set you off in the right direction. Using an init() method, although not required, is considered best practice in the CF world for initializing components because cfc's don't have true constructors like JAVA (yet). Constructors are used to set up default instant variables, or other objects; that are used in the objects lifetime; when the object is created, The init() method always returns the objects instance this. eg: cffunction name=init access=public cfreturn this / /cffunction So using your code with a slight change: application.getters = createObject(component,cfc.GetterQueries).init(); The method then returns the objects instance this to your application scoped variable with everything setup. To further elaborate why you should use constructors. Change the init method to this: cffunction name=init access=public cfargument name=dsn default= required=no / cfset variables[instance] = StructNew() / cfset variables.instance[dsn] = arguments.dsn / cfreturn this / /cffunction and use: mydsn = myDsnName; application.getters = createObject(component,cfc.GetterQueries).init(dsn=mydsn); This now will pass the variable dsn to the constructors arguments and set myDsnName to variables.instance[dsn]. Calling variables.instance[dsn] from any method in the object will now return myDsnName. This will allow you to pass variables or even other objects into your component when you instantiate them. This can be done other ways, but its best to create your instance variables when you create the object, for many reasons that I wont expound on here, but setting up all your components internal variables during init() is the best way. Using init() as your constructor is also required by ColdSpring and other objects frameworks. Have a look at ColdSpring or LightWire, they can help you wire your components together with a framework, this also is considered best practice. http://www.coldspringframework.org/ http://lightwire.riaforge.org/ ColdSpring is framework for you to manage your singleton components, their internal data, and any reference to other components that that any component may need. Even if all init() does is create your object and return it, you should use it, since you never know when you will needed to have a constructor create instance variables. Even if you don't use ColdSpring, using init() will help you manage your objects. If I have made errors in the direction I am providing, anyone else please chime in, I am still in the middle of the curve myself. Hope that helps, Don't give up! Once this all clicks in your brain you will never look back. /K ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:326681 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Invoking components, methods and storing instance in application scope
Hi Tony, you make like to take a look at http://learn.objectorientedcoldfusion.org/wiki/The_Init()_Function instances of an object. I have also seen quite a bit about using init() in every component but am not sure why I would do this if I just want to call a method from the application scope: ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:326682 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Invoking components, methods and storing instance in application scope
On Sun, Sep 27, 2009 at 6:22 PM, Kevin Pepperman chorno...@gmail.com wrote: Using an init() method, although not required, is considered best practice in the CF world for initializing components because cfc's don't have true constructors like JAVA (yet). Just a couple of points to elaborate on this... We use init() on CFCs because it mimics how we have to interact with Java classes: cfset jObj = createObject(java,java.lang.String).init( Hello ) / Calling the constructor method init() in a CFC means it looks the same in use: cfset cfObj = createObject(component,path.to.MyObject).init( Hello ) / Also, in CF9 - and in the CFML2009 spec - the new operator calls init() automatically: import path.to.MyObject; cfObj = new MyObject( Hello ); Based on that syntax - and its behavior - I'd say we *do* have constructors, they just happen to be called init(). Remember: a constructor is simply a method that is called to do initialization (and different languages call their constructors different names). -- Sean A Corfield -- (904) 302-SEAN Railo Technologies US -- http://getrailo.com/ An Architect's View -- http://corfield.org/ If you're not annoying somebody, you're not really alive. -- Margaret Atwood ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:326683 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Invoking components, methods and storing instance in application scope
I am building an application where I want to eliminate instantiating the same component more than once per application. I thought it would be a good idea to setup all of the methods through the application scope: application.getters = createObject(component,cfc.GetterQueries); This way I am using what I believe is Singleton Patterns for single instances of an object. I have also seen quite a bit about using init() in every component but am not sure why I would do this if I just want to call a method from the application scope: contacts = application.getters.getContacts(); Is there any reason why this would not be a good practice? ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:326632 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4