Model Glue Tips Part 1: Separate out your Model-Glue file

Posted At : August 10, 2007 6:22 PM | Posted By : Mark Drew
Related Categories: model-glue

I have been developing a number of Model Glue applications over the last year and a half. Its a great framework and it has actually saved my neck on a project with incredible tight deadlines. I thought I would share some of the tips I have learned along the way by doing a series of posts with tips over the next couple of weeks.

In the first part of the series I shall be looking at:

Separate out your Model-Glue file
In any moderately sized project, you will start to gather a large number of controllers and event handlers, not to mention the messages you are passing back to the controllers. This will lead you to have a very large ModelGlue.xml file which becomes unwieldy and difficult to find things without the help of outline tools such as the CF Frameworks Explorer in Eclipse. For this reason, I like splitting my my ModelGlue.xml file out into a number of files, this can be done easily thanks to the top level include tag (which is a child of the modelglue tag). To use this, you simply have to create another ModelGlue file, and you simply point to it by doing the following:

<modelglue>
         
         < include template="config/ModelGlue_Pages.xml">
         < include template="config/ModelGlue_Security.xml">
         < include template="config/ModelGlue_Products.xml">
         <controllers>
            ...
         </controllers>
         <event-handlers>
            ...
         </event-handlers>
      </modelglue>
The included files are simply other ModelGlue.xml files, with their own controllers an event handlers dealing with that so you have split out your application into relevant files. This also means that different developers can work on different parts of the application, simply by removing the include in your own version. By the way, you can do a very similar think with ColdSpring (but more on that later)

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Andy J's Gravatar Another tip is to prefix your event tips so they don't conflict i.e. pages.doSomething, security.doSomething, products.doSomething. I've made that mistake before :o)
# Posted By Andy J | 8/11/07 4:54 AM
Mark Drew's Gravatar Damn right Andy, See my second post in the series!
# Posted By Mark Drew | 8/11/07 6:28 AM
Critter's Gravatar I was unaware you could do that. Thanks, great top
# Posted By Critter | 8/11/07 7:57 AM
sal's Gravatar Implementing this on a current app; am I gonna have to put my layout template event in every xml file? seems DRY...
# Posted By sal | 8/13/07 11:56 AM
Mark Drew's Gravatar well, you are repeating yourself but only in your configuration. What you can do is put something in the request that adds a result if one is not defined, I shall go into this in a couple of posts :)
# Posted By Mark Drew | 8/13/07 12:02 PM
sal's Gravatar Correction...
My bad, I just put my layout in my main ModelGlue.xml and every include now recognizes my template event. Sorry hadn't noticed I removed it from my main config file.

cheers :-)
# Posted By sal | 8/13/07 12:17 PM
sal's Gravatar Mark,

Is it possible to have multiple OnRequestStart methods in each separated xml/controller...?

example;
in my security xml file I would like an onRequestStart controller method that checked every requested event (security), for a isLoggedIn flag in the session, and in my admin.xml file I would like a controller method script to check against an isAdmin flag in the session...? If this is possible, would my event handler names be similar in both xml files??

cheers
# Posted By sal | 8/20/07 6:01 PM
sal's Gravatar ...continued.

and if this is possible which I'm assuming, which onRequestStart method takes precedence if they both get executed...?
# Posted By sal | 8/20/07 6:08 PM
Mark Drew's Gravatar @Sal: It is indeed possible to do that, now with regards to the order, I might be wrong here, but I would presume they would fire in the order that they are read, i.e. if you have your Admin controller before your Security one the admin one would be triggered
# Posted By Mark Drew | 8/21/07 4:41 AM
Adam Tuttle's Gravatar >"To use this, you simply have to create another ModelGlue file"

Does this imply that each included file should start with &lt;modelglue&gt; and could/should contain the sections: event-handlers, views, etc; or is it more like a CF include - as long as the file contains valid XML that you could drop into ModelGlue.xml in place of the &lt;include&gt; then it will work? I can see advantages and disadvantages to both, but it certainly sounds like an idea that will be useful to me, and I'm not even done writing my <em>first</em> Model-Glue project yet. :)
# Posted By Adam Tuttle | 8/22/07 6:08 AM
Mark Drew's Gravatar @Adam: Each included MG file is a complete ModelGlue file (with modelglue/controllers|event-handlers/ etc

Good look with the project!
# Posted By Mark Drew | 8/22/07 6:40 AM
Jesus's Gravatar "...By the way, you can do a very similar think with ColdSpring (but more on that later)..."

How?
# Posted By Jesus | 10/22/07 11:04 AM
Mark Drew's Gravatar @Jesus: Just for you, I did a blog post about it :D
# Posted By Mark Drew | 10/28/07 8:19 AM
James Allen's Gravatar I really really like this Mark!

I have just been sketching out some ideas on how to implement this kind of thing on a huge project I am embarking on. As it's pretty massive I was wondering whether it might be worth subdividing the config's into seperate
folders. E.G:

config/users/usersmain.xml
config/users/usersadmin.xml

etc.

This way it's possible to really seperate out the various sections of the site into really manageable chunks.

I was also thinking of using the <include> tag in the various main sub configs. So I'd have an include to usersadmin.xml in the usersmain.xml file.

I haven't done any testing on this yet, but would be interested in comments.. I think on a big project, the smaller the config files can be and the more segmented the controllers are the better.
# Posted By James Allen | 11/21/07 6:12 AM
Mark Drew's Gravatar @James: Glad to have helped!

I tend to name my files something like modelglue_subsection.xml simply so I know what the XML file is from looking at the name, usersmain.xml might not tell you too much if you also have a coldspring file for the user configuration beans for example
# Posted By Mark Drew | 11/21/07 6:25 AM
James Allen's Gravatar Thanks Mark - good point about the naming.

Would you advocate the user of subfolders in config?

I was thinking it might be a nice way to seperate everything out as I can see the config directory growing quite fast.

Also, would you tend to <include> everything in the modelglue.xml or seperate these out into the various sub-config files (on a large site)?
# Posted By James Allen | 11/21/07 6:29 AM