Ask Model-Glue: Beans Scope

John C. Bland just wrote in with a question on the Model-Glue mailing list asking about Event types. His code sample gave us an opportunity to share a new Model-Glue 3 feature. This information could be useful to others so let's eavesdrop on the conversation...

Hi , John, that looks pretty good. You are definitely on the right track with your event type work. Your question is actually a very timely one and gives us an opportunity to share a new feature of Model-Glue 3. Since you are interested in short cuts and in using MG3 most effectively, let's talk about the newly added Beans scope.

In your code sample, you defined a configuration bean in ColdSpring, always a good thing, and retrieved it from ColdSpring with the syntax getModelGlue().getBean("urlConfig").

Nothing is wrong with that syntax at all, and your code will work fine. However, there is an even easier way to get beans from ColdSpring. MG3 introduces the concept of the Beans scope that is not only easier, but it also leaves good documentation of your application as well.

Consider this code:

<cffunction name="SetPageConfigs" access="public" returntype="void" output="false">
    <cfset arguments.event.setValue("urlConfig", getModelGlue().getBean("urlConfig")) />
</cffunction>

The urlConfig object is retrieved from ColdSpring through the getModelGlue().getBean() declaration. What is nice about this is it makes accessing ColdSpring managed objects very easy. However, it sort of makes it difficult to find all the the Coldspring managed beans in a specific controller. Using Find tools to scan source code for references is not really the very best thing because other developer may have chosen a name that isn't obvious or has been abstracted for some reason. Model-Glue 3 introduces the notion of a 'beans' scope. The Beans scope is a controller-wide scope containing all the ColdSpring managed beans for a specific controller. Here is a short sample:

<cfcomponent extends="ModelGlue.gesture.controller.Controller" output="false"
            beans="urlConfig,UserService">


    <cffunction name="doSetUp" output="false" access="public" returntype="void" hint="I perform global setup stuff for each request">
        <cfargument name="event" type="any">
        <cfset arguments.event.setValue("urlConfig", beans.urlConfig ) />
    </cffunction>
...

See the beans attribute of the cfcomponent tag? This is a comma separated list of all the ColdSpring managed beans we need in this controller. Inside the doSetup() method, we use the prefix 'beans' to get at the specific object:

beans.xxxxx

One nice effect of this technique is less typing on the keyboard.

More importantly, we now have a bit of auto-documentation on exactly which ColdSpring managed beans are used by this controller. This documentation is at the very top of the file. Now, when you maintain your code, or code from other developers, you can simply read the beans attribute in your controller to find out which beans are in use, rather than scanning or reading lots of code for that information. Nice huh?

Model-Glue is all about building applications quicker and reducing maintenance costs and I'm sure you'll agree the Beans scope makes sense on both fronts.

Keep sniffing the 'Glue

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
And my response on the list:

PIMP!!!!! :-D

I made the change and it is working as advertised. :-D
# Posted By john C. Bland II | 10/8/09 1:57 PM
You can also have the "beans" attribute on the XML configuration for the controller in your MG.xml file. I find that is better from the documentation perspective since all MG config information is in one place, but its nice to have options.
# Posted By Sean Coyne | 10/9/09 8:15 AM
we can also add the beans to a controller, in its definition in controller section of mg.xml:
<controller beans="..." type="..." beans="...>
this way we don't affect any code
salvatore
# Posted By salvatore fusto | 10/9/09 10:19 AM
@Sean @Salvatore
Excellent guys! I like this. It keeps my changes all in the config.

Thanks!
# Posted By john C. Bland II | 10/9/09 10:56 AM
Hello to everybody,

do anybody know where can I find documentation for Model Glue 2.0 ?
I know it is legacy, but I really need it.
# Posted By Jovan | 7/25/11 8:48 AM
© 2014 Joe Rinehart
BlogCFC was created by Raymond Camden. This blog is running version 5.9.3.006.