1 / 20

Practical Code Generation with CF Template

Practical Code Generation with CF Template. Peter Bell SystemsForge. Webmaniacs ‘08. Overview. Why Generate? Types of Generation How to Generate Benefits of CF Template Working with Metadata. Why Generate?. Duplicated code: within applications between applications.

manjit
Download Presentation

Practical Code Generation with CF Template

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practical Code Generationwith CF Template • Peter Bell • SystemsForge Webmaniacs ‘08

  2. Overview • Why Generate? • Types of Generation • How to Generate • Benefits of CF Template • Working with Metadata

  3. Why Generate? • Duplicated code: • within applications • between applications

  4. We Already Generate GENERATING HTML: <cfoutputquery="GetUser"datasource="Scratch"> FirstName; #FirstName#<br/> LastName; #LastName#<br/> </cfoutput> GENERATING SCRIPTS: <<cfoutputquery="ObjectList"datasource="Scratch">> <cfoutputquery="Get%ObjectName%"datasource="Scratch"> <<cflooplist="%ObjectFieldNameList%"index="ThisField">> %ThisField%: %#ThisField#%<br/> <</cfloop>> </cfoutput> <</cfoutput>>

  5. Three Stages of Code • 1. Duplicated <cfqueryname="GetUserByEmail"datasource="Scratch"> select FirstName,LastName from tbl_User where Email = <cfqueryparamcfsqltype="cf_sql_varchar"value="#form.Email#"> </cfquery> <cfqueryname="GetProductByID"datasource="Scratch"> select Title,Price from tbl_Product where ProductID = <cfqueryparamcfsqltype="cf_sql_int"value="#form.ProductID#"> </cfquery>

  6. Three Stages of Code • 2. Dynamic <cfqueryname="GetData"datasource="Scratch"> select #FieldNameList# from #TableName# <cfifListFind("varchar,datetime,text",FilterFieldType)> where #FilterFieldName# = '#form.FieldValue#' <cfelse> where #FilterFieldName# = #form.FieldValue# </cfif> </cfquery>

  7. Three Stages of Code • 3. Generated <cfqueryname="Get%ObjectName%by%FilterFieldName%"datasource="Scratch"> select %FieldNameList% from %TableName% where %FilterFieldName% = <cfqueryparamcfsqltype= "%FilterFieldType%"value="form.%FilterFieldName%"> </cfquery> GENERATES: <cfqueryname="GetUserByEmail"datasource="Scratch"> select FirstName,LastName from tbl_User where Email = <cfqueryparamcfsqltype="cf_sql_varchar"value="form.Email"> </cfquery>

  8. Reasons to Generate • Prototype with Dynamic Code • Generate for: • Performance • Language Limitations • Simplicity • IP

  9. Types of Generation • Passive vs. Active • Passive (wizard) • Passive (automated) • Active (round trip)

  10. Active Code Generation • Separate files: • Inheritance • Mixins • AOP • CF Includes • Protected blocks

  11. Approaches To Generation • Approaches to Generation • Concatenation • Template • Transformation

  12. Concatenation <cfset ScriptHTML = "<cfquery name=""Get#ObjectName#by#FilterFieldName#"" datasource=""Scratch"">"> <cfset ScriptHTML = ScriptHTML & NewLine & "select #FieldNameList#"> <cfset ScriptHTML = ScriptHTML & NewLine & "from #TableName#"> <cfset ScriptHTML = ScriptHTML & NewLine & "where #FilterFieldName# = <cfqueryparam cfsqltype=""#FilterFieldType#"" value=""form.#FilterFieldName#""> "> <cfset ScriptHTML = ScriptHTML & NewLine & "</cfquery>">

  13. Generation: XML <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:outputmethod="text"indent="no"/> <xsl:templatematch="/"> &lt;cfquery name="Get<xsl:value-of select="//query/@ObjectName"/>by<xsl:value-of select="//query/@FilterFieldName"/>" datasource="Scratch"&gt; select <xsl:value-of select="//query/@FieldNameList"/> from <xsl:value-of select="//query/@TableName"/> where ##FilterFieldName## = &lt;cfqueryparam cfsqltype="<xsl:value-of select="//query/@FilterFieldType"/>" value="form.<xsl:value-of select="//query/@FilterFieldName"/>"&gt; &lt;/cfquery&gt; </xsl:template> </xsl:stylesheet> Sample XML modified from Illudium Generator by Brian Rinaldi

  14. Generation: CF Template <cfqueryname="Get%ObjectName%by%FilterFieldName%"datasource="Scratch"> select %FieldNameList% from %TableName% where %FilterFieldName% = <cfqueryparamcfsqltype="%FilterFieldType%"value="#form.%FilterFieldName%#"> </cfquery> GENERATES: <cfqueryname="GetUserByEmail"datasource="Scratch"> select FirstName,LastName from tbl_User where Email = <cfqueryparamcfsqltype="cf_sql_varchar"value="#form.Email#">

  15. CF Template Syntax • Generation Time Tags: << >> vs. < > • Generation Time Variables % vs. # • That is IT! • ALL ColdFusion features available • Including cfscript (<<cfscript>>)

  16. Benefits of CF Template • More Readable • no &lt;/&gt; • Less “Junk” • <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> • Shorter Variables • %TableName% vs. <xsl:value-of select="//query/@ObjectName"/> • Full Power of ColdFusion • DateFormat(), cfif, cfloop, cfoutput, UDFs, etc. • Nothing New to Learn!

  17. Sources of Metadata • Database metadata • Explicit Metadata • Static code analysis • Runtime reflection

  18. Metadata Options • Comma delimited • "test.csv", ",", "FirstName,LastName,Email", "tbl_User", "update" • XML • <import filename="test.csv" record-delimiter="," field-name-list="FirstName,LastName,Email" source="tbl_User" method="update" /> • Databased • “Little Language” • Import test.csv using commas update tbl_User with FirstName,LastName,Email • Visual

  19. Example Metadata Product extends: BaseObject tableName: tbl_Product Identity: ProductID Properties: Title title required Price money optional default:0 Description WYSIWYG optional ClassMethods: AdminList: Title,Price OrderBy Title DefaultAdd: Title,Price,Description QuickAdd: Title,Price multiple:5 DefaultEdit: ID, Title,Price,Description Relationship has-many Category associated optional

  20. Samples/Questions • Find out more at http://cftemplate.riaforge.com • Presentation will be posted tomorrow • Check out http://www.pbell.com

More Related