我一直在建立一个 CFC 最佳实践列表以供分享。
那里有很多文章,但我认为将通过经验学到的任何技巧和技巧集中在一个地方可能会很巧妙。
我将在此处添加一些链接以使其继续进行,但我认为最好的方法不是可以在 Google 上搜索到的长文章。
更新:这已成为社区 wiki
我一直在建立一个 CFC 最佳实践列表以供分享。
那里有很多文章,但我认为将通过经验学到的任何技巧和技巧集中在一个地方可能会很巧妙。
我将在此处添加一些链接以使其继续进行,但我认为最好的方法不是可以在 Google 上搜索到的长文章。
更新:这已成为社区 wiki
四件快事:
加入CFCDev 邮件列表(或现在的 google 群组)。
Sean Corfield 的 CFML 演示文稿中的设计模式 PDF是一个很好的快速阅读。
http://www.cfdesignpatterns.com有一些好东西,其中包含指向优质 CFC 设计文章的链接。
Rob Brooks-Bilson 博客上关于 CFML 中的设计模式的文章。
在使用ColdBox 框架之前,我没有看到任何关于使用 Momentos 捕获属性的帖子。但是,现在我所有的 bean 都有一个 getMomento() 和 setMomento() 方法。对于需要将信息从 bean 传递到 DAO 其他对象的任何人,我会鼓励将此作为最佳实践。
在我的测试中,获得一个时刻比传递 bean 并获得属性要快得多。这是一个例子:
<cfcomponent name="userBean" output="true" hint="The account bean holds getter/setter information for a user's account.">
<cfproperty name="idUser" required="true" type="string" rules="noZeroLengthString,validEmail" invalidMessage="failed_data_validation_email" hint="Key matching the 'accounts' table.">
<cfproperty name="loginEmail" required="true" type="string" rules="noZeroLengthString,validEmail" invalidMessage="failed_data_validation_email" hint="E-mail address.">
<cfproperty name="password" required="true" type="string" rules="noZeroLengthString,validPassword" invalidMessage="failed_data_validation_password" hint="Password stored in a SHA-512 hash.">
<cffunction name="init" output="false" returntype="userBean" hint="Initalizes the userBean with default values.">
<cfset variables.instance = structNew()>
<cfset variables.instance.IDUser = 0>
<cfset variables.instance.loginEmail = "">
<cfset variables.instance.password = "">
<cfreturn this>
</cffunction>
<!--- SET LOGIN --->
<cffunction name="setLoginEmail" access="public" returntype="void" output="false">
<cfargument name="email" type="string" required="true" />
<cfset variables.instance.loginEmail = trim(arguments.email) />
</cffunction>
<cffunction name="getLoginEmail" access="public" returntype="string" output="false">
<cfreturn variables.instance.loginEmail />
</cffunction>
<!--- ID --->
<cffunction name="setIDUser" access="public" returntype="void" output="false">
<cfargument name="id" type="numeric" required="true" />
<cfset variables.instance.IDUser = arguments.id />
</cffunction>
<cffunction name="getIDUser" access="public" returntype="numeric" output="false">
<cfreturn variables.instance.IDUser />
</cffunction>
<!--- PASSWORD --->
<cffunction name="setPassword" access="public" returntype="void" output="false">
<cfargument name="password" type="string" required="true" />
<cfset var pw = arguments.password>
<cfif len(pw) EQ 0>
<cfset variables.instance.password = "">
<cfelse>
<!---><cfset variables.instance.password = hash(arguments.password, "SHA-512") />--->
<cfset variables.instance.password = arguments.password>
</cfif>
</cffunction>
<cffunction name="getPassword" access="public" returntype="string" output="false">
<cfreturn variables.instance.password />
</cffunction>
<!--- MOMENTO --->
<cffunction name="setMomento" access="public" returntype="void" output="false">
<cfargument name="momento" type="struct" required="true" />
<cfset variables.instance = arguments.momento>
</cffunction>
<cffunction name="getMomento" access="public" returntype="struct" output="false">
<cfreturn variables.instance />
</cffunction>
干杯,
亚伦格林利 我的网站