2007 adobe systems incorporated. all rights reserved. 1 glenda vigoreaux adobe certified instructor...

26
2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. [email protected] Component Development in ColdFusion 8

Upload: sabrina-bryan

Post on 17-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.1

Glenda Vigoreaux

Adobe Certified Instructor

roundpeg, Inc.

[email protected]

Component Development in ColdFusion 8

Page 2: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.2

Creating ColdFusion Components (CFCs)

ColdFusion Component Invocation

Instantiating ColdFusion Components

Persisting ColdFusion Components

CFCs and Object-Oriented Programming

Encapsulation

Inheritance and the Super keyword

Introspection

Component Access Control

Questions and Answers

Session Outline

Page 3: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.3

Allow you to group ColdFusion statements into user-defined functions that perform a logical unit of work

Object-based building block for building applications

Logically group data and functionality

CFCs can perform many related actions, defined in multiple methods

CFCs are more structured making the code easy to follow and troubleshoot

Reviewing ColdFusion Components

Page 4: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.4

Provide an ideal mechanism for code reuse

Can be invoked without including the code on the page

Can be called as web services by other clients using any platform

Are self-documenting through introspection

Can persist between page requests

Can be secured using roles-based security

Benefits of ColdFusion Components

Page 5: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

52007 Adobe Systems Incorporated. All Rights Reserved.

<cfcomponent>

<cffunction name="getServices" returntype="query">

<cfargument name="sort" type="string" required="yes">

<cfset var qGetAll="">

<cfquery name="qGetAll" datasource="#application.dsn#">

SELECT * FROM Services

ORDER BY #arguments.sort#

</cfquery>

<cfreturn qGetAll>

</cffunction>

<cffunction name="invoke_function">

<cfset getServices()>

</cffunction>

</cfcomponent>

Creating ColdFusion Components

Page 6: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.6

Creating a ColdFusion Component

Walkthrough 1

Page 7: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

72007 Adobe Systems Incorporated. All Rights Reserved.

<cfinvoke

component="marketing.services"

method="getServices"

returnvariable="qServices">

<cfinvokeargument name="sort" value="serv_id"/>

</cfinvoke>

ColdFusion Component Invocation

Page 8: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.8

Invoking a ColdFusion Component

Walkthrough 2

Page 9: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.9

CFCs can be instantiated as objects and then invoked using object style syntax

Object type invocation is essential for CFCs with persistent properties and CFCs used multiple times

Using <CFOBJECT> to create an instance

<cfobject component="marketing.services" name="cfcServices">

Using CreateObject() to instantiate the same component

<cfset cfcServices =

CreateObject("component","marketing.services")>

Instantiating ColdFusion Components

Page 10: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.10

Use the <CFINVOKE> tag

The CFC instance name is enclosed in pound signs (#) in the component attribute

<cfobject component="marketing.services" name="cfcServices">

<cfinvoke component="#cfcServices#" method="getServices"

returnvariable="qServices" sort="serv_id"/>

Invoking Methods on a CFC instance

Page 11: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.11

Object methods may also be invoked inline, using object.method() syntax

<cfobject component="marketing.services" name="cfcServices">

<cfset qServices =

cfcServices.getServices("serv_id")>

Invoking Methods on a CFC instance

Page 12: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.12

Invoking Components as Objects

Walkthrough 3

Page 13: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.13

CFCs may be instantiated into persistent scopes

Place components into the following scopes:Session

Application

<cfobject component="marketing.services"

name="application.cfcServices">

<cfset qServices =

application.cfcServices.getServices("serv_id")>

Persisting ColdFusion Components

Page 14: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.14

Persisting CFC Instances

Walkthrough 4

Page 15: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.15

ColdFusion is not object oriented

CFCs deliver some of the power and capabilities of objects

Some of the object-oriented features of CFCs include encapsulation, inheritance, and introspection

CFCs and Object-Oriented Programming

Page 16: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.16

The technique of incorporating both code and data into one object such as a CFC is known as Encapsulation

Encapsulation lets users pass data to and get a result from the CFC without having to understand the underlying code

Data passed to the CFC can be validated

CFCs can enforce data types, check for required parameters, and optionally assign default values

Encapsulation

Page 17: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.17

Allows a CFC to extend another CFC

Original component is called “base” or “parent” component

Component that extends another component is called the “child” component

Child component inherits the methods and properties from the parent component

Inheritance lets you build multiple specific components without rewriting the code for the basic building blocks of the components

Inheritance

Page 18: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.18

Child component uses the extends attribute to specify the parent component

<cfcomponent extends = "components.myParent">

The Extends Attribute

Page 19: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

192007 Adobe Systems Incorporated. All Rights Reserved.

Useful when a child and parent share the same method

Child component can use the super scope to reference the parent component methods

Super keyword

Services.cfc [Parent]<cfcomponent><cffunction name=”getServices”>

[Function CFML code]</cffunction></cfcomponent>

OtherServices.cfc [Child]<cfcomponent extends=”services”><cffunction name=”getServices”><cfreturn super.getServices()></cffunction></cfcomponent>

Page 20: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.20

CFCs support introspection because they can provide information about themselves

You can see information about the component by:Displaying a component page directly in an HTML browser

Inspecting it in the Dreamweaver component browser

The information displayed includes the component’s path, property, methods, and additional information specified using special attributes and tags

Introspection

Page 21: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.21

Extending Components

Walkthrough 5

Page 22: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.22

The access attribute of the <cffunction> tag determines how and where a method can be invoked

<cffunction name="getServices" returntype="query" access="public">

Method access can also be restricted based on the role of the user that is logged in

Use the <cflogin> tag to establish the roles

<cffunction name="getServices" returntype="query" access="public" roles="admin,manager">

Component Access Control

Page 23: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

232007 Adobe Systems Incorporated. All Rights Reserved.

PrivateThis function can only be called by other functions within the same component and any components that extend the component in which this function is defined

Access Types and Description

PackageThis function can be called by any other component within the same directory in addition to the conditions for access=”private”

PublicThis function can be called by any ColdFusion page or component method located on the same server as this function

RemoteThis function can be called by any ColdFusion page or component method located on any server, or by any client through a URL, form submission, Flash Remoting or web service

Page 24: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.24

Experimenting with Access Control

Walkthrough 6

Page 25: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.25

CFCs framework force developers to work in a more systematic way

Code inside a CFC is generally easy to follow and troubleshoot

Components help developers establish advanced development techniques by allowing for a higher degree of reuse, encapsulation, and security

Components can persist by placing them in the session or application scope

Some of the object-oriented features of CFCs include encapsulation, inheritance, and introspection

Component access can be restricted by using the access and roles attributes of the <cffunction> tag

Summary

Page 26: 2007 Adobe Systems Incorporated. All Rights Reserved. 1 Glenda Vigoreaux Adobe Certified Instructor roundpeg, Inc. glenda@roundpeg.com Component Development

2007 Adobe Systems Incorporated. All Rights Reserved.26

Questions

Thank you for attending this session

Questions and Answers