Ask a Jedi: Moving existing application code into Model-Glue

Rony sent me some interesting questions today concerning existing code and Model-Glue. He sent two questions. His second question was easier so I'll address that one first:

I also have several variables, objects (such as DAO's, gateways) and structs inside stored in my application scope. How I can integrate this with MG?

Model-Glue makes this rather simple. All controllers (you should have at least one per application) are automatically stored in the Application scope. This may not be 100% obvious as you don't see the Application scope used in your own, but it is indeed cached there. In fact, if you use the Model-Glue application template, you will see this in the Controller.cfc init method:

<!--- Controllers are in the application scope: Put any application startup code here. --->

All you need to do is place your Model CFCs here to cache them. An example:

<cfset variables.userModel = createObject("myapp.model.user")>

Because the controller itself is cached, this CFC will also be cached.

Now for his second question:

I currently have an application.cfc in which i have code for each of the functions inside this component, onrequeststart(), on applicationstart() etc. How can I integrate this into MG?

There are a few answers to this. First off - Model-Glue will automatically call onRequestStart and onRequestEnd in each of your controller CFCs. So if you had 4 controllers, you actually get 4 different ways to run something on the start and end of each request.

As for onApplicationStart(), I kind of covered this in the first portion. The init method of each controller is run when the controller is created. This acts like a virtual onApplicationStart, and again, you have one for each controller. This is both good and bad. On one hand - I like how Model-Glue allows me to focus on each controller and it's own application startup/request startup work. On the flip side, that makes it a bit more difficult to see - quickly - exactly what is going on in your startup code. It's not impossible of course. You just need to look at multiple files.

As a side question - do you think it makes sense for Model-Glue to have a site wide Application and Request startup handler?

Archived Comments

Comment 1 by Rony Fayyad posted on 5/28/2007 at 4:12 AM

Ray,

The reason I like Application.cfc is that it clearly indicated what was happening in my application via the functions available inside this component.

Now that each controller has a unique set of functions as found in the application.cfc, I find that the centralized way of identifying what is going on in the application ( via application.cfc ) is now going to be made harder.

To answer your side question ( if it was meant for me alone) was that i would like to see a state wide application and request startup handler. It just seems to make more sense to me, then breaking it down to individual controllers.

I am going to place the onsessionstart() in the main MG controller? Is that what i am supposed to be doing? I don't see the the need to have it in each controller, as opposed to the the onrequeststart().

Thanks,

Comment 2 by Raymond Camden posted on 5/28/2007 at 9:28 AM

When it comes to sessions, I break the 'rules' and directly access the Session scope in my controller CFCs. I figure this is ok since controllers really aren't the same as the model. So if I needed to do something on session start, I'd probably use onRequestStart and check to see if a particular session var existed. Not a -great- solution mind you.

Comment 3 by Rony Fayyad posted on 5/29/2007 at 2:51 AM

The onSessionStart() was nice enough to do all this.

Now having to manually do it would be frustrating.

Also, what about on onSessionEnd() and onError()?

This is what i liked about the application.cfc, it did all this for you and all we had to do was written the code per function.

Comment 4 by Raymond Camden posted on 5/29/2007 at 5:01 AM

So session event support could be nice. onError _is_ supported. Model-Glue supports running an event on error.

Comment 5 by Jazon posted on 1/23/2008 at 1:01 PM

Great job on the whole series!
Is the second time I'm going through the series, first time was a great learning experience, but now I'm paying close attention to things.

on the IIS section: shouldn't that be C:\Inetpub\wwwroot? you haveit backwards C:\wwwroot\inetpub

Thanks again!!!

Comment 6 by Raymond Camden posted on 1/23/2008 at 5:28 PM

Well, you would use whatever your web root is. I may have changed mine.

Comment 7 by Jazon posted on 1/31/2008 at 9:40 AM

Thanks a lot Raymond :)

Comment 8 by Troy posted on 1/31/2008 at 9:48 AM

I currently have an application.cfc in which i have code for each of the functions inside this component, onrequeststart(), on applicationstart() etc. How can I integrate this into MG?

Thanks in advance.

Comment 9 by Troy posted on 1/31/2008 at 9:51 AM

Nice idea. Thanks a lot Zimon and the rest of the guys.

Comment 10 by Raymond Camden posted on 1/31/2008 at 5:21 PM

Zimon was actually a spammer. He copied text from me above. I deleted his comment.

Comment 11 by Paul Stewart posted on 7/4/2008 at 4:23 PM

Ray, does this still apply in MG 2? i cant see any 'Model-Glue application template' where i can see a controller.cfc init method. And when i try and add an init method to my controller, MG Framework fails to load with this message:

Element MODELGLUE.FRAMEWORK is undefined in VARIABLES.

Comment 12 by Raymond Camden posted on 7/4/2008 at 6:02 PM

Well the MG app template is just the skeleton application code.

Comment 13 by Paul Stewart posted on 7/7/2008 at 2:33 PM

Below is what i have in \modelglue_2.0.304\modelglueapplicationtemplate\controller

only 3 methods and no sign of init() method
--
LICENSE INFORMATION:

Copyright 2007, Joe Rinehart

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licen...

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

VERSION INFORMATION:

This file is part of Model-Glue Model-Glue: ColdFusion (2.0.304).

The version number in parenthesis is in the format versionNumber.subversion.revisionNumber.
--->

<cfcomponent displayname="Controller" extends="ModelGlue.unity.controller.Controller" output="false">
<!---
Any function set up to listen for the onRequestStart message happens before any of the <event-handlers>.
This is a good place to put things like session defaults.
--->
<cffunction name="onRequestStart" access="public" returnType="void" output="false">
<cfargument name="event" type="any">
</cffunction>

<!---
Any function set up to listen for the onQueueComplete message happens after all event-handlers are
finished running and before any views are rendered. This is a good place to load constants (like UDF
libraries) that the views may need.
--->
<cffunction name="onQueueComplete" access="public" returnType="void" output="false">
<cfargument name="event" type="any">
</cffunction>

<!---
Any function set up to listen for the onRequestEnd message happens after views are rendered.
--->
<cffunction name="onRequestEnd" access="public" returnType="void" output="false">
<cfargument name="event" type="any">
</cffunction>
</cfcomponent>

Comment 14 by Raymond Camden posted on 7/7/2008 at 3:19 PM

Oh. I misread you. You can just add the init method. MG will run it automatically on startup.