So you want to create a ModelGlue:Unity application? ( Part 10 )
Sean Corfield pointed out that using ColdSpring to make instance (throwaway) components is quite a heavyweight approach for a simple form bean. He is, of course, correct. Our ContactFormBean is currently created by ColdSpring. In this series, we will add a factory object that will make our instance beans for us. The factory itself will be configured through ColdSpring as well as the configuration for our objects.
This is a very simple factory. It has three methods, GetConfig, SetConfig and getBean. In turn:
- getConfig() returns a struct with keys = objectnames, and values = paths
- setConfig() provides an interface for ColdSpring to set the configuration struct.
- getBean() gets the path for a specific object from the configuration struct and returns a created object.
2
3 <!--- Author: dwilson Date: 3/14/2007 Usage: I return an instance object --->
4 <cffunction name="getBean" output="false" access="public" returntype="any" hint="I return an instance object">
5 <cfargument name="ObjectName" type="string" required="true"/>
6 <cftry>
7
8 <cfreturn createObject( "component", structFind(getConfig(), arguments.ObjectName ) ).init() />
9
10 <cfcatch type="any">
11
12 <cfthrow message="BAD_OBJECT_CONFIG_IN_INSTANCEFACTORY" detail="You provided [ #arguments.ObjectName # ] and I can't create it. Go check the config." />
13
14 </cfcatch>
15
16 </cftry>
17
18 </cffunction>
19
20 <!--- Usage: GetConfig / SetConfig methods for Config value --->
21 <cffunction name="getConfig" access="public" output="false" returntype="any">
22 <cfreturn variables.instance.Config />
23 </cffunction>
24
25 <cffunction name="setConfig" access="public" output="false" returntype="void">
26 <cfargument name="Config" type="any" required="true" />
27 <cfset variables.instance.Config = arguments.Config />
28 </cffunction>
29
30</cfcomponent>
To use this instance factory, add the following ColdSpring xml snippet to the ColdSpring.xml file.
2 <property name="Config">
3 <map>
4 <entry key="ContactFormBean"><value>ContactManagerMG.model.ContactFormBean</value></entry>
5 </map>
6 </property>
7</bean>
Now remove the original ContactFormBean configuration.
Next, open the Controller.cfc file and change each instance (getContactForm, handleContactForm and removeContact) of getModelGlue().getBean("ContactFormBean") to getModelGlue().getBean("InstanceFactory").getBean("ContactFormBean") .
Finally, reinitialize your application and click on the ContactForm tab. You should see your form as before.
A factory might seem like overkill right now, since this is a simple application. However, Factory objects are a good pattern to learn. Our motivation for the factory in this case was to reduce the amount of unnecessary processing incurred by using ColdSpring to make a simple form bean. We could however, expand our factory to take additional parameters and create all sorts of dynamic objects for us.
In a later series, we'll do just that.
There are no comments for this entry.


Probably the best MG tutorial i've seen, thanks for putting this together!
I was looking for a Model-Glue tutorial that approached things more clearly than the the docs that came with Model-Glue. This is exactly what I needed - thanks. I'll be posting a link to your site on my blog shortly. :)
-Nolan
Excellent series, I learnet a lot from this series, I have got one question, how can we make site SEO friendly when using this MG framework?
Thanks and continue doing good work and help the comunity.
Thanks for the tutorial Dan it's a good learning experience...
DK is right,those stuff is very usful.