Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Prior to using the <a href="http://www.coldboxframework.com" rel="nofollow noreferrer">ColdBox Framework</a> I did not see any posts about using Momentos to capture the properties at that moment; however, now all my beans have a getMomento() and setMomento() method. I would encourage this as a best practice for anyone who needs to pass information from a bean into a DAO other object.</p> <p>In my tests, getting a momento is much faster than passing the bean and getting the properties. Here is an example:</p> <pre><code>&lt;cfcomponent name="userBean" output="true" hint="The account bean holds getter/setter information for a user's account."&gt; &lt;cfproperty name="idUser" required="true" type="string" rules="noZeroLengthString,validEmail" invalidMessage="failed_data_validation_email" hint="Key matching the 'accounts' table."&gt; &lt;cfproperty name="loginEmail" required="true" type="string" rules="noZeroLengthString,validEmail" invalidMessage="failed_data_validation_email" hint="E-mail address."&gt; &lt;cfproperty name="password" required="true" type="string" rules="noZeroLengthString,validPassword" invalidMessage="failed_data_validation_password" hint="Password stored in a SHA-512 hash."&gt; &lt;cffunction name="init" output="false" returntype="userBean" hint="Initalizes the userBean with default values."&gt; &lt;cfset variables.instance = structNew()&gt; &lt;cfset variables.instance.IDUser = 0&gt; &lt;cfset variables.instance.loginEmail = ""&gt; &lt;cfset variables.instance.password = ""&gt; &lt;cfreturn this&gt; &lt;/cffunction&gt; &lt;!--- SET LOGIN ---&gt; &lt;cffunction name="setLoginEmail" access="public" returntype="void" output="false"&gt; &lt;cfargument name="email" type="string" required="true" /&gt; &lt;cfset variables.instance.loginEmail = trim(arguments.email) /&gt; &lt;/cffunction&gt; &lt;cffunction name="getLoginEmail" access="public" returntype="string" output="false"&gt; &lt;cfreturn variables.instance.loginEmail /&gt; &lt;/cffunction&gt; &lt;!--- ID ---&gt; &lt;cffunction name="setIDUser" access="public" returntype="void" output="false"&gt; &lt;cfargument name="id" type="numeric" required="true" /&gt; &lt;cfset variables.instance.IDUser = arguments.id /&gt; &lt;/cffunction&gt; &lt;cffunction name="getIDUser" access="public" returntype="numeric" output="false"&gt; &lt;cfreturn variables.instance.IDUser /&gt; &lt;/cffunction&gt; &lt;!--- PASSWORD ---&gt; &lt;cffunction name="setPassword" access="public" returntype="void" output="false"&gt; &lt;cfargument name="password" type="string" required="true" /&gt; &lt;cfset var pw = arguments.password&gt; &lt;cfif len(pw) EQ 0&gt; &lt;cfset variables.instance.password = ""&gt; &lt;cfelse&gt; &lt;!---&gt;&lt;cfset variables.instance.password = hash(arguments.password, "SHA-512") /&gt;---&gt; &lt;cfset variables.instance.password = arguments.password&gt; &lt;/cfif&gt; &lt;/cffunction&gt; &lt;cffunction name="getPassword" access="public" returntype="string" output="false"&gt; &lt;cfreturn variables.instance.password /&gt; &lt;/cffunction&gt; &lt;!--- MOMENTO ---&gt; &lt;cffunction name="setMomento" access="public" returntype="void" output="false"&gt; &lt;cfargument name="momento" type="struct" required="true" /&gt; &lt;cfset variables.instance = arguments.momento&gt; &lt;/cffunction&gt; &lt;cffunction name="getMomento" access="public" returntype="struct" output="false"&gt; &lt;cfreturn variables.instance /&gt; &lt;/cffunction&gt; </code></pre> <p></p> <p>Cheers,</p> <p>Aaron Greenlee <a href="http://www.aarongreenlee.com/" rel="nofollow noreferrer">My Site</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload