More On Using ColdFusion Components


You've now had firsthand experience with ColdFusion Components, and you'll be using them extensively as you work through this book. ColdFusion Components make it easy to tier applications, and this results in:

  • Cleaner code

  • More reusable code

  • More maintainable code (code that is less prone to breakage when changes are made)

But before closing this chapter, there are a few additional points about ColdFusion Components worth mentioning.

Where to Save CFCs

The ColdFusion Components created in this chapter (and indeed, throughout this book) are stored within the work folder. This is ideal when learning ColdFusion, but in practice this isn't what you'd want to do.

Most developers create a specific cfc folder (or several of them) and store all common ColdFusion Components in them. This will make it easier to locate and maintain them. As you have seen, Dream weaver automatically accommodates for path considerations when generating <cfinvoke> tags.

Unit Testing

One important benefit of ColdFusion Components not mentioned thus far is testing. As you build applications you'll want to test your work regularly. And the larger and more complex an application becomes, the harder testing becomes. This is even more problematic when code gets in the way. For example, if you were testing the SQL in a <cfquery> you wouldn't want HTML layout issues to unnecessarily complicate the testing.

Breaking code into tiers greatly simplifies testing. Once you've written your ColdFusion Component you can (and should) create a simple test page, one that doesn't have complex display code and simply invokes methods and dumps their outputmuch like we did in the geometry example earlier in this chapter. Experienced developers typically have simple test front-ends for each ColdFusion Component they create. This practice is highly recommended.

Documenting ColdFusion Components

As your ColdFusion Component collection grows, so will the uses you find for them. So will the number of developers who will want to take advantage of them, assuming you're working with other developers. As such, it is really important to document your ColdFusion Components, explaining what they do, what each method does, and what you expect passed to any arguments.

Documenting ColdFusion Components is so important that self-documenting features are built right into the tags used to create them. Each of the CFC tags used in this chapter, <cfcomponent>, <cffunction>, and <cfargument>, take an optional attribute named hint. As you can see in Listing 11.12, the hint attribute has been used to add little snippets of documentation to our movies.cfc file.

Listing 11.12. movies.cfcProviding CFC Hints
 <!--- Movie component ---> <cfcomponent hint="Movie database abstraction">  <!--- List method --->  <cffunction name="List" access="public"              returnType="query" output="false"              hint="List all movies">   <!--- Get movie list from database --->   <cfquery name="movies" datasource="ows">   SELECT FilmID, MovieTitle, PitchText,          Summary, DateInTheaters   FROM Films   ORDER BY MovieTitle   </cfquery>   <cfreturn movies>  </cffunction>  <!--- GetDetails method --->  <cffunction name="GetDetails" access="public"              returnType="query" output="false"              hint="Get movie details for a specific movie"> <cfargument name="FilmID" type="numeric"             required="true" hint="Film ID">   <!--- Get a movie from database --->   <cfquery name="movie" datasource="ows">   SELECT FilmID, MovieTitle,          PitchText, Summary,          DateInTheaters, AmountBudgeted   FROM Films   WHERE FilmID=#ARGUMENTS.FilmID#   </cfquery>   <cfreturn movie> </cffunction> </cfcomponent> 

So what do these hints do? They have absolutely no impact on the actual processing of the Cold Fusion Components. Rather, they are used by ColdFusion to generate documentation on the fly, as seen in Figure 11.14.

Figure 11.14. ColdFusion auto-generates ColdFusion Component documentation using the information gleaned from the tags used to create it.


Earlier in this chapter I told you not to run the .cfc directly, and said that if you did, the result might not be what you'd expect. Well, the result is actually documentation, like the example shown in Figure 11.14. To access this documentation you can:

  • Specify the URL to the .cfc in your browser.

  • Browse the .cfc in Dreamweaver.

  • From within the Dreamweaver Application panels Component tab, right-click on any Component and select Get Description.

NOTE

When you browse CFC documentation you may be asked for your ColdFusion Administrator's password.


You can type hints manually, if you like. In addition, the Create Component wizard used earlier in this chapter allows hint text to be provided while building the CFC. However you decide to do it, providing hint text is highly recommended.



Macromedia Coldfusion MX 7 Web Application Construction Kit
Macromedia Coldfusion MX 7 Web Application Construction Kit
ISBN: 321223675
EAN: N/A
Year: 2006
Pages: 282

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net