Article

Create Scalable Applications with ColdFusion Components

Page: 1 2 3

NOTE: Security Considerations
Earlier in the article I mentioned the access attribute that appears on the <cffunction> tag. This attribute controls where our CFC can be invoked from. ColdFusion allows every function in a component to individually set this attribute to one of four different values explained below:

  • Private: the function can only be run from within the component.
  • Package: the function can only be run from within the component or from other components in the same folder.
  • Public: the function can run from any template or component on the server. This is the default.
  • Remote: the function can be accessed directly from external processes, including Ajax scripts, web service requests, Flex and Flash remoting, and even directly in the address bar of the browser.

As a general rule, you should always set the level of access to the lowest possible value that meets your requirements.

Completing BlogManager

At this point the advantages of consolidating our business and data access logic in a single component should start to become clear. Any changes to the underlying data or business rules can be effectively hidden from the rest of the application. If we were asked to filter out unapproved blog entries from all pages, for example, we would simply change the SQL of the getBlogEntries function. Likewise, if we decided to incorporate news feeds from independent book-related blogs using RSS, the change could be made without affecting the other application pages.

To complete the BlogManager component we need to add methods to create, read, and update blog entries. We also need functions to add comments to blog entries and work with the list of available blog categories.

The BookBlogs site contains pages devoted to books, authors, and member information. Each of these sets of information have their own business and data access logic that can benefit from being encapsulated into ColdFusion components such as BookManager and MemberManager.

The entire site, including a completed version of BlogManager, BookManager, MemberManager, and other ColdFusion components can be found within the source code download. The source code also has additional hints and tips included as comments.

So far we have centralized our business logic and data access throughout our application, and probably saved ourselves countless hours in its future support -- but now it's time to think even bigger!

Do More with CFCs

In addition to the advantages we have already explored, the highly structured nature of CFCs allows ColdFusion to integrate very tightly with the CFC definitions within applications to provide impressive capabilities with little effort.

Using CFCs we can:

  • automatically publish web services by changing the access level of a function to remote
  • bind rich Ajax controls directly to our CFCs
  • respond to events like application start, session start, and even missing templates by using an application.cfc file
  • design applications that communicate with external services and agents such as JMS, SMS, Flex Messaging, and Flash Remoting
  • effectively use community CFC-based frameworks that automate database code and more

Covering even the basics of these topics would take several more articles!

Tips for Using CFCs

ColdFusion components are easy to implement and use. Perhaps the toughest part is deciding how, where, and when to use components within your applications. This article has showcased a very narrow usage of CFCs within your applications, but there are no hard and fast rules. Below are my own personal tips on how to use CFCs effectively:

  • Other forms of reuse, such as page templates, custom tags, and individual UDFs still have their uses. Where CFCs really shine is in capturing specific business rules and data access logic. Custom Tags and UDFs, on the other hand, are ideal for generic output and data manipulation tasks that aren't specific to any business need.
  • Organizing CFCs around independent related data and functionality is important. Giving your CFCs names that reflect concrete objects, well-defined business concepts, and even job titles can help keep CFCs focused on their set of responsibilities.
  • Document your CFCs using the hint attribute on both <cfcomponent> and <cffunction> tags. These hints, although optional, are useful for both reviewing source code manually and through the component explorer. Documentation is important when others must work with your code or when you'll need to be able to understand it after development.
  • Learn more. ColdFusion components are your gateway into both a better understanding of object oriented programing and the ability to utilize the most powerful features of ColdFusion.

Where to Go from Here

I recommend the following resources for learning more about ColdFusion components:

Conclusion

During this article we've discovered what ColdFusion components are, how they are defined, and how other pages can be designed to use them. While this is enough to get you started, we've barely scratched the surface of what CFCs are capable of!

Ultimately, CFCs help ColdFusion developers write maintainable and flexible applications that can grow and evolve over time. Even better, they save you work right away through code reuse, and later, through using the advanced features of ColdFusion such as Ajax controls and web service generation. You'll thank yourself for taking the time to get to know them!

Remember you can download the complete code archive for the examples presented in this article.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: