Presentation - CF Conf Central

Download Report

Transcript Presentation - CF Conf Central

CFCs in Practice
Raymond Camden ([email protected])
Senior Developer, Mindseye Technologies
Agenda

Quick Intro to ColdFusion Components (CFCs)

CFC Coding Details and Tips

CFC “Issues”

CFC Examples

Q&A
CFCs in a Nutshell

A new way to encapsulate code.

Collection of methods with built in security

Quick and Easy Web Services

Pseudo-Object Oriented (No, it’s not really OO)

New tags: <cfcomponent>, <cfproperty>,
<cffunction>, <cfreturn>
“Life Through a Browser”
3
CFC Example
<cfcomponent>
<cffunction name="sayHello">
<cfif hour(now()) lt 12>
<cfreturn "Good Morning">
<cfelseif hour(now()) lt 18>
<cfreturn "Good Afternoon">
<cfelse>
<cfreturn "Good Evening">
</cfif>
</cffunction>
</cfcomponent>
“Life Through a Browser”
4
CFC Example (2)
<cfinvoke component="simple" method="sayHello"
returnVariable="greeting">
<cfoutput>#greeting#</cfoutput>
<p>
<cfset greeter = createObject("component","simple")>
<cfoutput>#greeter.sayHello()#</cfoutput>
“Life Through a Browser”
5
CFC Tips: Constructors



CFCs do not have a formal constructor…
However – any code not inside a method is run when
the CFC is instantiated (created or called remotely).
Generates white space! Use output=false in
<cfcomponent> (also in <cffunction>)
“Life Through a Browser”
6
CFC Tips: Let’s Talk about Data

This



Accessible to caller code
Shows up in dump, but not getMetaData()
Unnamed “private” scope



Not accessible to caller code
Accessible to children
Not like Variables, can’t be dumped. (Fixed in RedSky)

Method arguments

Method temporary variables (var scope)
“Life Through a Browser”
7
CFC Methods

Default access is public – change to private

Specify all attributes, even optional ones:





access (important for security!)
returnType (important for validation!)
output (controls white space!)
hint
roles
“Life Through a Browser”
8
CFC Methods (2)


returnType should be one of:
any
numeric
array
query
binary
string
boolean
struct
date
uuid
guid
variableName
Or must be the name of a CFC type

Watch out for structure instead of struct,
number instead of numeric, etc.
“Life Through a Browser”
9
CFC Methods (3)

Output versus Return

Outputting from the method will not work for Flash Remoting

Use the var scope

This is intentional: Use the var scope

Applies to UDFs as well!
“Life Through a Browser”
10
<cfproperty> - What does it really do?

It does NOT create variables.

It does NOT validate variables.

It does create entries in the meta data.

It helps define the WSDL returned by a web service
“Life Through a Browser”
11
CFC “Issues”

<cfinclude> inside a method will not have access to Arguments

<cftransaction> and CFCs

ArgumentCollection and <cfinvoke>

Cached CFCs lose access to output, other scopes

You can’t duplicate (correctly) or serialize (correctly) a CFC

Undefined arguments show up in arguments struct
“Life Through a Browser”
12
Web Services Gotchas…

No optional arguments allowed for methods


AXIS limitation
Caching of Arguments

Fixed by using CF Admin to refresh the WS
“Life Through a Browser”
13
CFC Examples

Session Tracker

Unit Tester (from DRK3)

Nathan Dintenfass’ Descriptor
“Life Through a Browser”
14
Questions and Resources

www.cfczone.org

cfcdev mailing list

Macromedia DevNet Articles


http://www.macromedia.com/devnet/mx/coldfusion/cfcs.html
CFDJ Articles
“Life Through a Browser”
15