Rip's Domain

BaseCFC: sharing some code

Posted in ColdFusion by rip747 on October 18, 2006

Just my stupid little contribution.

During development you can retype alot of code creating classes. This is a little class file I created that I can extend to stop me from going crazy. Basically all it does it is automatically create the setters and getters methods for all variables that I setup in the constructor. Use it if you will and tell me what I can do to make it better.

Above have a good laugh at the way I code😛
<!— BaseCFC.cfc —>
<cfcomponent hint=”Base CFC model that all cfcs should extend” output=”false”>

<!— settings up the base constructor —>
<cfset variables.instance = StructNew()>

<!— init —>
<cffunction name=”init” access=”public” returntype=”Any” output=”false”
hint=”default init method. Will return any. should be overloaded by the inheriting CFC”>
<cfreturn this>
</cffunction>

<!— getInstance —>
<cffunction name=”getInstance” access=”public” returntype=”struct” output=”false”
hint=”default method that will return all the variables in the constructor”>
<cfreturn variables.instance>
</cffunction>

<!— get —>
<cffunction name=”get” access=”public” returntype=”Any” output=”false”
hint=”gets the value from the constructor”>
<cfargument name=”key” type=”any” required=”true” hint=”the variable to get”>
<cfif ConstructorVarExists(arguments.key)>
<cfreturn variables.instance[arguments.key]>
</cfif>
<cfset Local.MetaData = GetMetaData(this)>
<cfthrow type=”Custom”
errorcode=”BaseCFC.Constructor.Get”
message=”the property does not exist inside the object (#Local.MetaData[‘name’]#).”
detail=”Can’t find the key ‘#arguments.key#’ in the property structure. Are you sure you spelled the key correctly? Valid keys are: #ListSort(StructKeyList(this.getInstance(), ‘, ‘), ‘textnocase’)#”>
</cffunction>

<!— set —>
<cffunction name=”set” access=”public” returntype=”void” output=”false”
hint=”sets the value in the constructor”>
<cfargument name=”key” type=”any” required=”true” hint=”the variable’s to set”>
<cfargument name=”value” type=”Any” required=”true” hint=”the value to set the variable to”>
<cfset var Local = StructNew()>
<cfif NOT ConstructorVarExists(arguments.key)>
<cfset Local.MetaData = GetMetaData(this)>
<cfthrow type=”Custom”
errorcode=”BaseCFC.Constructor.Set”
message=”the property does not exist inside the object (#Local.MetaData[‘name’]#).”
detail=”Can’t find the key ‘#arguments.key#’ in the property structure. Are you sure you spelled the key correctly? Valid keys are: #ListSort(StructKeyList(this.getInstance(), ‘, ‘), ‘textnocase’)#”>
</cfif>
<cfset variables.instance[arguments.key] = arguments.value>
</cffunction>

<!— ConstructorVarExists —>
<cffunction name=”ConstructorVarExists” access=”private” returntype=”boolean” output=”false”
hint=”tells whether the variable is part of the constructor variable.”>
<cfargument name=”key” type=”string” required=”true” hint=”the variable name to check”>
<cfreturn StructKeyExists(variables.instance, arguments.key)>
</cffunction>

</cfcomponent>

How to use:

<!— Event.cfc —>
<cfcomponent output=”false” extends=”BaseCFC”>

<cfset variables.instance.eventid = “”>
<cfset variables.instance.cancelled = “0”>
<cfset variables.instance.type = “T”>

</cfcomponent>

2 Responses

Subscribe to comments with RSS.

  1. Peter Bell said, on October 19, 2006 at 11:10 am

    You may want to check out my iterating business object with generic getters and setters. Here is a post on the generic getter (old code, but right concept):
    http://www.pbell.com/index.cfm/2006/7/27/Snippets-A-Generic-Getter

    Two key issues:
    Support comma delimited list of gettable attributes for information hiding
    The generic getter checks to see if getX() exists (a custom getter) and then uses the custom getter if it is there. That way if you add a custom getter with business logic that change is encapsulated – still use the generic getter and it’ll be smart enough to call the custom method instead.

    Just a thought!

  2. rip747 said, on October 19, 2006 at 6:20 pm

    @Peter

    Although sometimes the high level language of your posts make my head spin (don’t take offense, I’m just one of those guys who has a very limited vocabulary), I always find your post very informative and interesting. Any thoughts that you have are more then welcome and I will be definiatly be checking out your article link in your comment.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: