![]()
Object-Oriented Programming in ColdFusion
Table of Contents
Object-Oriented Programming in ColdFusion
Credits
Foreword
About the Author
Acknowledgement
About the Reviewer
Preface
Who this book is for
What this book covers
What you need for this book
Conventions
Reader feedback
Customer support
Errata
Piracy
Questions
1. Introducing ColdFusion Components
Why use CFCs?
Grouping your functions
Organizing your components
The ColdFusion component tags
Our first component
Defining a method
Returning the data
ColdFusion 9 scripted components
Creating your object
Restricting your functions to scopes
Using arguments within your methods
The Arguments scope
Redefine the function parameters
Combining your methods
Protecting your local variables
Using the Var scope
Placing your Var scoped variables
Naming your Var scoped variables
Accessing your CFC
Instantiating the object
Using the createObject function
Using the cfobject tag
Using the NEW operator
Using cfinvoke
The cfinvoke tag
Using cfinvokeargument
Using attributes as arguments
Using an argument collection
Passing arguments into an instance method call
As a list
As named values
As an argumentCollection
Arguments in action
Merging your functions into one
Using cfargument to combine your methods
Creating an object constructor
Creating an init() function
The Variables scope
Calling your init() function
The This scope
Summary
2. Effective Component Development
Pseudo-constructors
Using the pseudo method
Suppressing whitespace
Output attribute
Pseudo-constructor or init() method
Returning values and variables
returnType
Access properties
Private
Package
Public
Remote
Getting information about your CFC
Introspection
CFC Explorer
Component Doc
Document your code
Benefits of documentation
Displayname attribute
Hint attribute
Description attribute
User-defined metadata
Obtaining CFC metadata
getMetaData
getComponentMetaData
Returning metadata
Detailed introspection
Summary
3. Building Your First Bean
What is a Bean?
But what is it really?
The benefit of using beans
Creating our first Bean
An introduction to UML
Person object in UML
What makes a Bean a Bean
A default/no-argument constructor
Easily accessible for introspection
Setters/Mutators
Getters/Accessors
Completing our Projects Bean
Calling our project Bean
Populating the Bean
Read/Write Bean
Read-only Bean
Helpful objects
Implicit accessors
Summary
4. Inheritance and Object-Oriented Concepts
What is Inheritance?
Avoiding code duplication
Inheriting our products
The Super keyword
Overriding methods
Instantiating our products
The inheritance hierarchy
Specialization
The "IS A" relationship
Polymorphism
Composition
The "HAS A" Relationship
Implied ownership
Aggregation
Summary
5. Data Access Objects
What is a Data Access Object?
Creating a Data Access Object
The create method
Storing a new user
The read method
Handling select results
The update method
The delete method
An alternative save method
The save method
The exists method
Caching the Data Access Objects
Dependency Injection
Summary
6. Gateways
What is a Gateway?
A typical ColdFusion Gateway
Creating a Gateway object
Naming conventions
Adding gateway methods
Minimising code duplication
Revising the gateway object
Caching the Gateway object
The Gateway discussion
No hard and fast rules
Table Data Gateway
Similarities to Data Access Objects
Combining the two patterns
Which option is best?
Summary
7. Creating a Service Layer
What is a Service Layer?
Facade patterns in a nutshell
Creating a service
Defining the User Service
Adding the CRUD methods
Adding the Gateway methods
Adding an abstract class
Defining the address service
onApplicationStart (revisited)
Summary
Index
![]()
Object-Oriented Programming in ColdFusion
Copyright © 2009 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: October 2010
Production Reference: 1081010
Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK.
ISBN 978-1-847196-32-3
www.packtpub.com
![]()
Author
Matt Gifford
Reviewer
Raymond Camden
Acquisition Editor
David Barnes
Development Editor
Dhiraj Chandiramani
Rakesh Shejwal
Technical Editor
Arun Nadar
Copy Editor
Laxmi Subramanian
Indexer
Rekha Nair
Editorial Team Leader
Aditya Belpathak
Project Team Leader
Priya Mukherji
Project Coordinator
Leena Purkait
Proofreader
Lynda Sliwoski
Production Coordinator
Alwin Roy
Cover Work
Alwin Roy
![]()
ColdFusion Components (or CFCs) were introduced in ColdFusion MX back in 2002, and since then they have become one of the most important and fundamental building blocks for ColdFusion developers. In fact, recent releases of ColdFusion have made CFC use mandatory, by introducing features that are only available through ColdFusion Components.
As you'd expect from ColdFusion, CFCs are easy to learn and even easier to use. But the beautiful simplicity of ColdFusion Components hides a powerful, sophisticated, and proven Java-based framework on which developers can build highly scalable and manageable applications. Like all ColdFusion features, CFCs can be as simple or as sophisticated as your application requires, and developers can start reaping their benefits easily and painlessly, adding functionality and complexity later as needed.
But this duel personality, highly accessible simplicity on the one hand and all that power on the other, has proven to be a bit of a double-edged sword. The same ease of use that makes ColdFusion so easy to learn and so productive, also makes it too easy to never fully explore and leverage all that ColdFusion has to offer.
This is why I am so glad to see Matt Gifford's hard work take the form of this book. Matt starts with the basics of ColdFusion Components, but then quickly delves into the subtleties and complexities of using them for object-oriented development, demonstrating both the how's and why's, as well as the tips and tricks needed to truly appreciate how ColdFusion Components can (and should) change the way you build your next ColdFusion application.
Matt, this book is long overdue, and your contribution to the ColdFusion community is greatly appreciated. Thanks!
Ben Forta
Director of Platform Evangelism
Adobe Systems
When you start using ColdFusion components, you know that you're on the right track; you can feel this change at some core, instinctual level as if you're about to embrace something that holds tremendous, yet untapped potential. Unfortunately, your instincts won't take you much further beyond this point. The cold reality is, Object-Oriented Programming (OOP), ...