2
November 2005
CF_Implements
Post a Comment
I got bored last night and decided I would try to come up with a way to have an "Implements" keyword in ColdFusion.
The download contains four files
For the Custom Tag, at the very top of your CFC put a
It will throw an error if the class does not implement all the methods in the interfaces provided.
Example:
For the implements.cfc, You can use the object's init method to check the interface for an object anywhere. To force a cfc to implement something, use
Example:
If you want to make sure an instance passed to a function uses a certain interface, replace "this" with "arguments.whatevertheinstancevariableis".
Example:
There are a couple other things.
Can download it here
Enjoy
The download contains four files
- implements.cfm - If you want to use the custom tag version, copy this to your CustomTags directory.
- implements.cfc
- iComponentCache.cfc - a sample interface
- ComponentCache.cfc - a sample class using the "Implements" keyword.
For the Custom Tag, at the very top of your CFC put a
<cf_implements interfaces="full.path.to.interface1,full.path.to.interface2,etc,etc" />
It will throw an error if the class does not implement all the methods in the interfaces provided.
Example:
<cfcomponent>
<cf_implements interfaces="iPerson" />
<cffunction name="test" ..........
......
.....
</cfcomponent>
<cf_implements interfaces="iPerson" />
<cffunction name="test" ..........
......
.....
</cfcomponent>
For the implements.cfc, You can use the object's init method to check the interface for an object anywhere. To force a cfc to implement something, use
<cfset createObject("component","path.to.implements.cfc").init(this,"path.to.interface1,path.to.interface2") />
Example:
<cfcomponent>
<cfset createObject("component","com.implements").init(this,"com.iPerson") />
<cffunction name="test" ..........
......
.....
</cfcomponent>
<cfset createObject("component","com.implements").init(this,"com.iPerson") />
<cffunction name="test" ..........
......
.....
</cfcomponent>
If you want to make sure an instance passed to a function uses a certain interface, replace "this" with "arguments.whatevertheinstancevariableis".
Example:
<cffunction name="savePerson" access="public" output="false">
<cfargument name="thePerson" type="any" required="true" hint="Must implement iPerson" />
<cfset createObject("component","com.implements").init(arguments.thePerson,"com.iPerson") />
.....
....
</cffunction>
<cfargument name="thePerson" type="any" required="true" hint="Must implement iPerson" />
<cfset createObject("component","com.implements").init(arguments.thePerson,"com.iPerson") />
.....
....
</cffunction>
There are a couple other things.
- To set the custom tag to "production" mode. put <cf_implements mode="production" /> in your application.cfc/.cfm
- To set the implements.cfc to "production" mode, you must pass the argument "mode" with the value of "production" to the init method.
- You can also pass in your own CacheManager to the implements.cfc, the passed in CacheManager must implement iComponentManager.
Can download it here
Enjoy
Posted by fwscott at 5:07 PM | Comments (0)
Subscription Options
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
No comments found.