ColdFusion Components <ul><li>Andy Allan </li></ul><ul><li>[email_address] </li></ul><ul><li>www.creative-restraint.co.uk ...
Agenda <ul><li>CFC Introduction </li></ul><ul><li>How to use CFCs </li></ul><ul><li>Scoping your variables </li></ul><ul><...
What are CFCs? <ul><li>A new way to develop code. </li></ul><ul><li>Can be used in normal CFM applications, Web Services, ...
CFC/OOP Terminology <ul><li>Classes: the building blocks of OOP. </li></ul><ul><li>Instances: specific representation of a...
The CFC Tags <ul><li><cfcomponent> </li></ul><ul><li><cffunction> </li></ul><ul><li><cfargument> </li></ul><ul><li><cfretu...
<cfcomponent> <ul><li>All CFCs are wrapped inside a <cfcomponent> tag. </li></ul><ul><li>Optional attributes: </li></ul><u...
<cffunction> <ul><li>Defines a method describing something our CFC can do. </li></ul><ul><li>A Number of attributes, both ...
Pseudo Constructor Area <ul><li>Any code not within <cffunction>. </li></ul><ul><li>Executed when CFC is instantiated. </l...
<cfargument> <ul><li>Allows our methods to accept parameters. </li></ul><ul><li>Must be placed BEFORE any other code. </li...
<cfreturn> <ul><li>Returns a value based on the returntype set in <cffunction> </li></ul><ul><li>You can return one AND ON...
<cfproperty> <ul><li>Used in Web Services. </li></ul><ul><li>Defines complex data types. </li></ul><ul><li>For more inform...
Self Documenting <ul><li>Developers hate documentation. </li></ul><ul><li>Displayname and Hint attributes aid documentatio...
Component.cfc <ul><li>WEB-INF.cftags.component </li></ul><ul><li><cfmxroot>/WEB-INF/cftags </li></ul><ul><li>What can you ...
How to use CFCs <ul><li><cfinvoke> </li></ul><ul><li><cfobject> </li></ul><ul><li>createObject() </li></ul>
<cfinvoke> <ul><li><cfinvoke> uses components like they are just a normal .cfm page. </li></ul><ul><li>This is how Web Ser...
<cfobject>/createObject() <ul><li>Essentially the same, only the syntax differs. </li></ul><ul><li>Allows us to use CFCs a...
CFC Persistence <ul><li>instanceCFC doesn’t exist!!! </li></ul><ul><li>Server/Application/Session scope </li></ul><ul><li>...
Scoping your Variables <ul><li>Do it!!! </li></ul><ul><li>What scopes are there? </li></ul><ul><ul><li>This </li></ul></ul...
This Scope <ul><li>“ This” is available EVERYWHERE. </li></ul><ul><li>Inside CFC: this.variablename </li></ul><ul><li>Outs...
Variables/Unnamed Scope <ul><li>Variables scope is private to the CFC. </li></ul><ul><li>They are available within the CFC...
Local Function Scope <ul><li>ALL local function variables should be scoped using VAR! </li></ul><ul><li>This includes temp...
Arguments Scope <ul><li>Accessible via arguments.variablename </li></ul><ul><li>Always refer to your argument variables us...
Build your own Instance Scope <ul><li>Create a virtual scope inside of “Variables”. </li></ul><ul><li><cfset variables.ins...
Structuring your CFCs <ul><li>Where should you save your CFCs? </li></ul><ul><li><cfset ourCFC=createObject(“component”,”P...
Structuring Your CFCs (2) <ul><li>Store your CFCs outside the web root. </li></ul><ul><li>Create a mapping. </li></ul><ul>...
Structure your CFCs Internally <ul><li>Comment </li></ul><ul><li>Pseudo Constructor (if any) </li></ul><ul><li>Public Meth...
<cfinclude> <ul><li>Don’t use <cfinclude> in your CFC methods. </li></ul><ul><li>All Local Function variables are copied t...
Resources <ul><li>www.cfczone.org </li></ul><ul><li>cfcdev mailing list </li></ul><ul><li>CFC Best Practices by Nathan Din...
Q&A
Upcoming SlideShare
Loading in …5
×

ColdFusion Components

2,255 views

Published on

Presentation from Andy Allen to the Scottish CFUG on the use of CFCs in Adobe ColdFusion MX

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,255
On SlideShare
0
From Embeds
0
Number of Embeds
55
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • ColdFusion Components

    1. 1. ColdFusion Components <ul><li>Andy Allan </li></ul><ul><li>[email_address] </li></ul><ul><li>www.creative-restraint.co.uk </li></ul>
    2. 2. Agenda <ul><li>CFC Introduction </li></ul><ul><li>How to use CFCs </li></ul><ul><li>Scoping your variables </li></ul><ul><li>Structuring your CFCs </li></ul><ul><li>Q&A </li></ul>
    3. 3. What are CFCs? <ul><li>A new way to develop code. </li></ul><ul><li>Can be used in normal CFM applications, Web Services, Flash. </li></ul><ul><li>A first step to Object Orientated Programming. </li></ul>
    4. 4. CFC/OOP Terminology <ul><li>Classes: the building blocks of OOP. </li></ul><ul><li>Instances: specific representation of a class. </li></ul><ul><li>Methods: define what an object can do. </li></ul><ul><li>Properties: define information about our objects. </li></ul>
    5. 5. The CFC Tags <ul><li><cfcomponent> </li></ul><ul><li><cffunction> </li></ul><ul><li><cfargument> </li></ul><ul><li><cfreturn> </li></ul><ul><li><cfproperty> </li></ul>
    6. 6. <cfcomponent> <ul><li>All CFCs are wrapped inside a <cfcomponent> tag. </li></ul><ul><li>Optional attributes: </li></ul><ul><ul><li>Hint </li></ul></ul><ul><ul><li>Displayname </li></ul></ul><ul><ul><li>Extends </li></ul></ul><ul><ul><li>Output </li></ul></ul><ul><li>Remember, one component per file. </li></ul>
    7. 7. <cffunction> <ul><li>Defines a method describing something our CFC can do. </li></ul><ul><li>A Number of attributes, both required and optional. </li></ul><ul><ul><li>Access, Displayname, Hint, Name, Output, ReturnType, Roles </li></ul></ul><ul><li>Name is required. </li></ul><ul><li>For Web Services, ReturnType is also required and Access must be set to remote. </li></ul>
    8. 8. Pseudo Constructor Area <ul><li>Any code not within <cffunction>. </li></ul><ul><li>Executed when CFC is instantiated. </li></ul><ul><li>Useful to initialise variables. </li></ul><ul><li>Preferable to use an init() method instead. </li></ul><ul><li>Usage of CFCs consistent since you are guaranteed to be able to call init() on any CFC instance. </li></ul><ul><li>init() should return “this”. </li></ul><ul><li>The exception is when building a Web Service/Flash app </li></ul>
    9. 9. <cfargument> <ul><li>Allows our methods to accept parameters. </li></ul><ul><li>Must be placed BEFORE any other code. </li></ul><ul><li>One <cfargument> required for every argument passed in. </li></ul><ul><li>Required/Optional attributes: </li></ul><ul><ul><li>Default, Displayname, Hint, Name, Required, Type </li></ul></ul><ul><li>Name is the only required attribute. </li></ul><ul><li><cfargument> tags in a method used as a Web Service are ALWAYS required. </li></ul><ul><li>You refer to arguments using the #arguments# scope. </li></ul>
    10. 10. <cfreturn> <ul><li>Returns a value based on the returntype set in <cffunction> </li></ul><ul><li>You can return one AND ONLY one expression. </li></ul><ul><li>You can however use a Struct to return multiple values. </li></ul>
    11. 11. <cfproperty> <ul><li>Used in Web Services. </li></ul><ul><li>Defines complex data types. </li></ul><ul><li>For more information, see the following Macromedia technote: </li></ul><ul><li>http://www.macromedia.com/support/coldfusion/ts/documents/cfc_datatypes_webservices.htm </li></ul>
    12. 12. Self Documenting <ul><li>Developers hate documentation. </li></ul><ul><li>Displayname and Hint attributes aid documentation. </li></ul><ul><li>Component Browser. </li></ul><ul><li>Component Invoker (Brandon Purcell). </li></ul><ul><li>http://www.bpurcell.org/blog/index.cfm?mode=entry&ENTRY=947 </li></ul>
    13. 13. Component.cfc <ul><li>WEB-INF.cftags.component </li></ul><ul><li><cfmxroot>/WEB-INF/cftags </li></ul><ul><li>What can you use it for? </li></ul>
    14. 14. How to use CFCs <ul><li><cfinvoke> </li></ul><ul><li><cfobject> </li></ul><ul><li>createObject() </li></ul>
    15. 15. <cfinvoke> <ul><li><cfinvoke> uses components like they are just a normal .cfm page. </li></ul><ul><li>This is how Web Services are called. </li></ul>
    16. 16. <cfobject>/createObject() <ul><li>Essentially the same, only the syntax differs. </li></ul><ul><li>Allows us to use CFCs as objects. </li></ul><ul><li>Referencing instanceCFC allows us to access our CFC anywhere on the page. </li></ul><ul><li>What happens when we click on the “Next Page” link??? </li></ul>
    17. 17. CFC Persistence <ul><li>instanceCFC doesn’t exist!!! </li></ul><ul><li>Server/Application/Session scope </li></ul><ul><li>Instantiate CFCs in your Application.cfm file. </li></ul><ul><li>Refreshing your CFCs. </li></ul>
    18. 18. Scoping your Variables <ul><li>Do it!!! </li></ul><ul><li>What scopes are there? </li></ul><ul><ul><li>This </li></ul></ul><ul><ul><li>Variables/Unnamed </li></ul></ul><ul><ul><li>Function Local </li></ul></ul><ul><ul><li>Arguments </li></ul></ul>
    19. 19. This Scope <ul><li>“ This” is available EVERYWHERE. </li></ul><ul><li>Inside CFC: this.variablename </li></ul><ul><li>Outside CFC: component.first_name, e.g. Person.first_name </li></ul><ul><li>“ This” is not available with <cfinvoke>. </li></ul><ul><li>DON’T USE IT…. Breaks Encapsulation. </li></ul>
    20. 20. Variables/Unnamed Scope <ul><li>Variables scope is private to the CFC. </li></ul><ul><li>They are available within the CFC, but not outside it. </li></ul><ul><li>They can either be scoped with “variables” or left blank (unnamed scope). </li></ul><ul><li>Scope them!!! Or you’ll get bug bitten! </li></ul><ul><li>Exist for the lifetime of the CFC – persist between method calls. </li></ul>
    21. 21. Local Function Scope <ul><li>ALL local function variables should be scoped using VAR! </li></ul><ul><li>This includes temporary variables and index variables for loops </li></ul><ul><li>They exist ONLY in the method in which they are created. </li></ul><ul><li>They MUST be declared before any other code after any <cfargument> tags and they must be assigned a value. </li></ul><ul><li>Once declared they are not prefixed when referenced. </li></ul><ul><li>Variables declared without using VAR will exist in the variables/unnamed scope. </li></ul>
    22. 22. Arguments Scope <ul><li>Accessible via arguments.variablename </li></ul><ul><li>Always refer to your argument variables using the scope prefix or not at all. </li></ul>
    23. 23. Build your own Instance Scope <ul><li>Create a virtual scope inside of “Variables”. </li></ul><ul><li><cfset variables.instance = StructNew()> </li></ul><ul><li>Allows you to workaround Duplicate() bug. </li></ul>
    24. 24. Structuring your CFCs <ul><li>Where should you save your CFCs? </li></ul><ul><li><cfset ourCFC=createObject(“component”,”Person”)> </li></ul><ul><li>Restricts re-use. </li></ul><ul><li>Component Packages. </li></ul><ul><li>com.scottishcfug maps to d:wwwextensionscomponentscomscottishcfug </li></ul>
    25. 25. Structuring Your CFCs (2) <ul><li>Store your CFCs outside the web root. </li></ul><ul><li>Create a mapping. </li></ul><ul><li>The exception is Web Services/Flash. </li></ul><ul><li>Remember to set your component methods to Remote. </li></ul>
    26. 26. Structure your CFCs Internally <ul><li>Comment </li></ul><ul><li>Pseudo Constructor (if any) </li></ul><ul><li>Public Methods (with init() first) </li></ul><ul><li>Package Methods (if any) </li></ul><ul><li>Private methods (if any) </li></ul>
    27. 27. <cfinclude> <ul><li>Don’t use <cfinclude> in your CFC methods. </li></ul><ul><li>All Local Function variables are copied to Variables scope. </li></ul><ul><li><cfinclude> is compiled externally – it’s not a straight textual include. </li></ul><ul><li>Macromedia use examples of <cfinclude> in official docs – BAD! </li></ul>
    28. 28. Resources <ul><li>www.cfczone.org </li></ul><ul><li>cfcdev mailing list </li></ul><ul><li>CFC Best Practices by Nathan Dintenfass </li></ul><ul><ul><li>http://www.dintenfass.com/cfcbestpractices/ </li></ul></ul>
    29. 29. Q&A

    ×