This content has been marked as final. Show 4 replies
In general, I use cfscript to invoke my objects and their methods.
objMyComponent = createObject("component","cfcs.myComponent");
getStuff = objMyComponent.getStuff();
Personally, I think the choice in your approach is a "6 in one hand, half dozen in the other" sort of thing. Both approaches work well. I did some tests some time ago and found that using cfscript invocation techniques tended to be slightly faster than tags. This is, admittedly, highly debatable and the performance differences I saw were negligible. Ultimately, I find cfscript a more comfortable way to code. It jives with other languages I use.
For improving performance, I typically use Application.cfm or Application.cfc to invoke my commonly used objects, store them in a persistent scope for quicker access later. In Application.cfc, I often use something along the lines of:
application.objMyComponent = createObject("component","cfcs.myComponent");
request.objMyComponent = application.objMyComponent;
From there, I can just call the method in my desired template
getStuff = request.objMyComponent.getStuff();
P.S. Love your site's mission. My wife and I have two dogs we rescued and volunteer at our local shelter...love to see sites like this!!!
> However, if only using one
> method of a component, its it better to just invoke the component directly?
> <cfinvoke component="cfc.my.component" method="getStuff"
Yes, I think this is better. It means the object gets destroyed as soon as
it's not needed any longer.
> And while on the topic, which is better?
> <cfobject name="MyComponent" component="cfcs.my.component">
> Again, what are the benefits and drawback of each. I am more familiar with
> the first using <cfobject> but I see the second more and more.
For the purposes of your example? There's very little difference. I'd
NEVER use <cfobject> for CFC stuff, in favour of <cfinvoke>, as it's more
The one benefit createObject() has over either of the tags as it enables
method-calling to be chained to the object creation call:
myResult = createObject("component",
From an aesthetic perspective, I think CFML tags only really belong in
situations where they are intrinsically working with HTML tags (looping out
output repeating HTML, for example), which I think <cfinvoke> or <cfobject>
very rarely are. I like to keep my code well away from my presentation, so
most of the code I write never has anything to do with HTML, so tags are
almost always the "wrong" answer.
I would agree with Adam as well. Many CF programmers use cfcs as more of a code organization tool and essentially a file full of related functions. Even though CF is not an OO Language, it does borrow some of the concepts at least that is what I think.
Things like inheritance, encapsulations, etc can be achieved, you can get into having the standard beans and data access objects, so you might have a Person.cfc and a PersonDAO.cfc that would handle Create, Read, Update, Delete functions for that bean.
On a display page I would use a <cfloop> tag as opposed to doing some "for loop" in cfscript and using writeOuput() to display in an html table. <cfscript> can also be shorter. If you have to set a bunch of default variables in a cfc I think a cfscript block is cleaner, its shorter, just looks better, and may perform faster in some cases, but I am not sure if that is proven, a lot of factors can affect what would appear to be application performance.
I rambled, but hopefully you can find some value in the comment and the others as well. This forum is not as active as it used to be, but it has helped me a great deal and I am still learning.
In fact, now that I think about it, Adam had posted responses to some of my questions when I was initially getting into CF. (Not suggesting your getting into CF)
Thank you for your update. I am in the process of implementing various
patterns to some of my development (Factory, DAO, Gateways, etc). I see the
value of scripting. I am constantly script AS2 and 3, but never made the
connection to CF until recently. I was using mostly services based CFCs but
am moving more always from that separating the business logic from the
presentation layer more and more.
Storing Objects in the application is a great new way for me to work. I
would like to thank Craig for that. I have been storing certain persistent
variable and data in the application for a while and never made the
connection to objects till his post.
Thank you for your post. I have been ravaging people's forums and blogs to
get better at implementing patterns into my CF development.