CFCs in Practice Raymond Camden ([email protected]) Senior Developer, Mindseye Technologies.

15
CFCs in Practice Raymond Camden ([email protected]) Senior Developer, Mindseye Technologies

Transcript of CFCs in Practice Raymond Camden ([email protected]) Senior Developer, Mindseye Technologies.

Page 1: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

CFCs in Practice

Raymond Camden ([email protected])

Senior Developer, Mindseye Technologies

Page 2: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

Agenda

Quick Intro to ColdFusion Components (CFCs)

CFC Coding Details and Tips

CFC “Issues”

CFC Examples

Q & A

Page 3: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

3“Life Through a Browser”

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>

Page 4: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

4“Life Through a Browser”

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>

Page 5: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

5“Life Through a Browser”

CFC Example (2)

<cfinvoke component="simple" method="sayHello" returnVariable="greeting">

<cfoutput>#greeting#</cfoutput>

<p>

<cfset greeter = createObject("component","simple")>

<cfoutput>#greeter.sayHello()#</cfoutput>

Page 6: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

6“Life Through a Browser”

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>)

Page 7: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

7“Life Through a Browser”

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)

Page 8: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

8“Life Through a Browser”

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

Page 9: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

9“Life Through a Browser”

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.

Page 10: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

10“Life Through a Browser”

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!

Page 11: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

11“Life Through a Browser”

<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

Page 12: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

12“Life Through a Browser”

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

Page 13: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

13“Life Through a Browser”

Web Services Gotchas…

No optional arguments allowed for methods AXIS limitation

Caching of Arguments Fixed by using CF Admin to refresh the WS

Page 14: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

14“Life Through a Browser”

CFC Examples

Session Tracker

Unit Tester (from DRK3)

Nathan Dintenfass’ Descriptor

Page 15: CFCs in Practice Raymond Camden (jedimaster@mindseye.com) Senior Developer, Mindseye Technologies.

15“Life Through a Browser”

Questions and Resources

www.cfczone.org

cfcdev mailing list

Macromedia DevNet Articles

http://www.macromedia.com/devnet/mx/coldfusion/cfcs.html

CFDJ Articles