onMissingMethod and Abstract Object - Part 2

I just did a bit more work on the onMissingMethod handler in my abstract bean cfc.

In case you are interested here are the differences between my implementation and John Whish's original which it was based on.

  • I have improved the code compared to the earlier post.
  • I have made the code work when you have multiple levels of CFC with one extending another. (John's original code only worked at the first level.
  • I have implemented the checking of what is gettable and settable.
  • I have removed the type checking that John put in, as I prefer to be able to load everything from a form into my bean and call validate to find out what is invalid. (You can always put John's checking back.)
Any way here is the code should you want it:

[More]

onMissingMethod and Abstract Objects

I have finally got around to working a bit more on the fusebox scaffolder project. One of the things I have meaning to do was improve the generated code by using an abstract bean and letting generated beans inherit some base methods. I intend to do this for most of the generated cfcs, but just started with the bean as a first step.

I have not in the past used onMissingMethod but now enough of what I am doing is on CF8 that I decided it was worth incorporating in the generated code. I began by reading up about it to see what others had done.

There was certainly lots of ideas out there and I decided to take John Whish's dynamicAccessors.cfc and take it a bit further.

John's idea was to make use of the <cfproperty> tag to set up the properties in a cfc and then to make use of the onMissingMethod in a base cfc to create dynamic accessors. I really like this idea but I also like Peter Bell's idea for controlling the access to the getters and setters through a list of property names.

I was wondering about combining the two ideas and remembered somthing I discovered in the early days of CFCs. You can actaully create your own attributes on many of the CF tags just by adding them to the tag.

So I can write:

<cfproperty name="title" type="string" settable="true" gettable="true"/>
and ColdFusion will ignore my two new attributes in most respects. What it will do is add them to the metadata so I can read them. This allows me to extend John's getAccessors() method to update two new structures one for the setters and one for the getters.

<cffunction name="getAccessors"
         hint="I build a structure of public properties and data types."
         access="public">

      <cfset var metadata = "" />
      <cfset var properties = arrayNew(1) />
      <cfset var index = "" />
         
      <cfif StructKeyExists( variables.instance, "_properties" )
         AND StructKeyExists( variables.instance, "_getable" )
         AND StructKeyExists( variables.instance, "_setable" )
         AND IsStruct( variables.instance._properties )
         AND IsStruct( variables.instance._gettable )
         AND IsStruct( variables.instance._settable )>

         
         <cfdump var="#variables.instance._properties#"/>
         <cfdump var="#variables.instance._gettable#"/>
         <cfdump var="#variables.instance._settable#"/>
         <cfabort/>
         
         <cfreturn variables.instance._properties />
      <cfelse>
         <!--- The first time we get or set a property we need to build a structure of properties and types --->
         <cfset variables.instance._properties = {} />
         <cfset variables.instance._gettable = {} />
         <cfset variables.instance._settable = {} />
         
         <cfset metadata = getMetaData(this)>
         
         <cfloop condition="structKeyExists(metadata,'properties') OR structKeyExists(metadata,'extends')">
            <cfif structKeyExists(metadata,"properties")>
               <cfset properties = metadata.properties />
               <cfloop array="#properties#" index="index">
                  <cfif IsDefined("index.type")>
                     <cfset StructInsert( variables.instance._properties, index.name, index.type ) />
                  <cfelse>
                     <cfset StructInsert( variables.instance._properties, index.name, "any" ) />
                  </cfif>
                  <cfif IsDefined("index.getable")>
                     <cfset StructInsert( variables.instance._gettable, index.name, index.getable ) />
                  <cfelse>
                     <cfset StructInsert( variables.instance._gettable, index.name, "false" ) />
                  </cfif>
                  <cfif IsDefined("index.setable")>
                     <cfset StructInsert( variables.instance._settable, index.name, index.setable ) />
                  <cfelse>
                     <cfset StructInsert( variables.instance._settable, index.name, "false" ) />
                  </cfif>
               </cfloop>
            </cfif>
            <cfif structKeyExists(metadata,"extends")>
               <cfset metadata = metadata.extends>
            <cfelse>
               <cfset metadata = "">
            </cfif>
         </cfloop>
         
         <cfreturn variables.instance._properties />   
      </cfif>
         
   </cffunction>
I now have two new structures in my object which I can use to test if the generated code should allow you to set or get each property.

A small change to the onMissingMethod() function and I can now control who has access quite easily.

What I really want to know is: Am I mad to do this? After all I am arbitarily extending the language and I didn't ask anyones permission.

27Jan 16:25 GMT - Modified to fix a couple of typos.

CF 8 Upgrade

Up until now I had not come across any bugs in CF8 which would force me to upgrade to CF8.0.1, but I did earlier this week.

The bug was in onMissingMethod and came to light when I installed Bob Silverberg's ValidateThis! framework.

I'm running Windows XP and unfortunately my attempt to upgrade to CF8.0.1 failed and I had to do an uninstall and reinstall. The Uninstall also failed which then led me down the horrible route to registry editing. I always feel a little sick when doing things with regedit as its a bit like doing brain surgury in the mirror. One false move and you're dead!

Anyway its all over now and the ValidateThis! demo runs on my desktop machine. I am trying some new ideas in the scaffolding which will build validate this into one of the template sets.

CFMeetup presentation - Fusebox Scaffolding

On Thursday 17th i will be giving a short presentation on Fusebox Scaffolding to the CFMeetup Online ColdFusion User Group.

Its a preview of a longer presentation I will be doing at CFUnited Europe on 12th and 13th March 2008.

If you would like to see it please sign up at CF Meetup site.

Scaffolding progress

I finally got time to work on this again after a few weeks of hectic madness, which included passing my Coastal Skipper exam and visiting my sister for a weekend. I spent Sunday afternoon and yesterday evening generating code using the ColdSpring Templates and finding the bugs in the generated code. Then I found the place where the bug was generated and fixed the template.

Its quite tedious as on my laptop it takes a couple of minutes to regenerate the application with my test database.

I have uploaded what I have done so far to SVN as it makes my life easier to download it all from SVN onto the various machines I have to test it on. There are probably still bugs in the ColdSpring templates and I know that in the process I have broken the Reactor Templates.

I also changed the order that code is generated in. This is mostly for my own benefit as I was finding it difficult to find things. In the past code in the circuit.xml file was generated in order of fuseaction then object. Now its in alphabetic order of object then fuseaction. This means that all the fuseactions related to a particular object are next to each other.

The new Fusebox 5.5 will be realesed on December 1st so I am working to get my changes and fixes complete by them but I suspect the scaffolding will still be in Beta at that point as I haven't had as much opportunity to test as I would have liked. I been installing new servers or updating old code to later versions of CF rather than developing new code.

You can get the latest code version from SVN at: http://svn.fuseboxframework.org/framework/branches/dev/extensions/scaffolding

Scaffolding fixes

A couple of fixes have been posted to the scaffolding code alpha on the fusebox website. The most important was a missing template file for the coldSpring templates.

Anoter addition is the documentation which got missed off the original release. The word doc contains instructions on how to write your own templates. It still has some big omissions and will be updated with a complete API in future releases.

There is still an unfortunate need to change the directory that the code is installed in, but I am planning to fix that by the time the next release is available. For the present please be sure to read the readme.txt file.

If you do experience any issues please try to be specific about the problem, as I can't help if you tell me only that it does not work. If you need help please send me a copy of the error message and it also helps if you can send a copy of the generated sacffolding.xml file too.

Remember this is still an alpha version so all bugs are not yet found and tracked down.

Good Luck!

What is Scaffolding and How to use it - Part 3

If you read Part 2 of the series you might have seen Peter Bell's comment about the best way to use the code generated by scaffolding. Ok, so wouldn't it be great if the scaffolding was good enough to use without any change? Unfortunately the current templates available with the Fusebox scaffolder are likely to produce code that is less than complete. There are several areas where you will find it possible to improve the code. Lets look at each in turn because this situation is one where the answer may vary slightly in each case.

[More]

What is Scaffolding and how do I use it? - Part 2

So you want to use an architecture to avoid those nightmares. Unfortunately its not easy to get started. Unlike building a single page web site you have to create a lot of stuff before you even see anything. In Fusebox 5.1 with MVC you have to create a view directory with a dsp_ fuse, a controller directory with a circuit.xml, in the root goes a fusebox.xml and you define your application name in index.cfm before you get close to seeing the magic words "Hello World". If the words are going to come from a database you have to create a model directory and a qry_ fuse too.

So what if your application does more than output "Hello World"?

[More]

Fusebox Beta and Alpha releases

Yesterday at MAX Sean Corfield announced the release of Fusebox 5.5 Public Beta.

There is a new page on the Fusebox web site with links to all the modules.

One of the modules vailable on the beta page is my scaffolding code which is not strictly a Beta as its currently in Alpha. If you would like to take part in Alpha testing please let me know. I am keen to see people using the tool to generate applications and to get as many of the bugs as possible in the current code tracked down before we start adding the features for the next Alpha 2 version which should include some other databases.

Sean has also put the Fusebox 5.5 release notes on Share so demostrating the usefulness of the new service.

What is Scaffolding and how do I use it? - Part 1

You need scaffolding!

So you read something somewhere that said you should be using scaffolding and want to know how? Let me tell you a story.

ColdFusion has been developed to make creating applications easier right?

[More]

More Entries

BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.