Skip navigation
Currently Being Moderated

Application.cfc, THIS.mappings & cfinclude

Apr 5, 2012 8:36 AM

hi there

 

In my Application.cfc

 

<cfcomponent>

  <cfscript>

    THIS.name = "myName";

    {etc.}

 

    THIS.mappings = StructNew();

    mappingname = "/include";

    mappingpath = "Z:\webincludes\didi\test\aaisession\appcfc";

    THIS.mappings[mappingname] = mappingpath;

  </cfscript>

  

  <cfinclude template="/include/appcfc-onApplicationStart.inc" >

 

    {etc.}

 

 

does not work!

 

When I do the mapping in CFadmin, it works. But I need to do it on an application base.

 

Is this due to the fact, that at the time CF tries to resolve the path of the template, the mapping has not yet been assigned?

 

How can I overcome this?

 

-Didi

 
Replies
  • Currently Being Moderated
    Apr 8, 2012 12:15 AM   in reply to Didi

    You can do it in Application.cfc. However, you first have to go to the page Server Settings => Settings in the administrator, and check the option 'Enable Per Application Settings'.

     

    On a separate note, what's the deal with the INC extension in the Application file? This might force the browser to display or download. It is best practice to avoid such presentation code in the application file.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 5:03 AM   in reply to Didi

    OK. It just might be that ColdFusion isn't happy that the mapping is at once being set (written) and used (read) in the pseudo-constructor. What if you move the cfinclude tag to onRequestStart or, perhaps more appropriately, to onApplicationStart? Then it will run only after Application.cfc has been instantiated.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 5:15 AM   in reply to BKBK

    BKBK:

    The <cfinclude> is including the onApplicationStart() handler.  So kinda needs to be included from the body of Application.cfc.

     

    I suspect this won't work for the very reason the OP suspects.

     

    Didi:

    The way to overcome it is to not factor your onApplicationStart handler out into an included file.  Why are you doing that?

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 6:51 AM   in reply to Adam Cameron.

    Adam Cameron. wrote:

     

    BKBK:

    The <cfinclude> is including the onApplicationStart() handler.

    Does it? Sorry, I missed that. Likely because I don't quite understand Didi's passage about inner/outer space.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 6:57 AM   in reply to BKBK

     

     

    BKBK:

    The <cfinclude> is including the onApplicationStart() handler.

    Does it? Sorry, I missed that. Likely because I don't quite understand Didi's passage about inner/outer space.

     

    It was in the file name: appcfc-onApplicationStart.inc.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 7:21 AM   in reply to Didi

    To make sure I get it now. This works:

     

    <cffunction name="onApplicationStart">

        <cfinclude template="/include/appcfc-onApplicationStart.inc" >

      </cffunction>

     

    But replacing this code with

     

    <cfinclude template="/include/appcfc-onApplicationStart.inc" >

     

    where the file appcfc-onApplicationStart.inc includes the event onApplicationStart, doesn't work. Am I there, Didi?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 7:27 AM   in reply to Didi

     

    Why do I do this?

    • My application uses Shibboleth for authentication and I need a session mgt that is a little bit more sophisticated than the CF standard one. So my onApplicationStart,onSessionStart and onRequestStart count together about 200 lines of code just for authentication.
    • We will finally be 4 developpers at the same application.

    Modularization of the code helps us to keep oversight and avoids interfering with each other.

     

    Well, we are open for better stategies at any time :-)

     

     

     

    It doesn't to me sound like you're using source control..?

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 7:47 AM   in reply to Didi

     

    It doesn't to me sound like you're using source control..?

     

     

    We use CFBuilder2.

    I have asked in a different thread for a recommendation - http://forums.adobe.com/message/4216531#4216531 -

     

     

    CFB is a text editor, it's got nothing to do with source control.

     

    Source control:

    http://en.wikipedia.org/wiki/Revision_control

     

    Specific examples:

    http://en.wikipedia.org/wiki/Subversion_(software)

    http://en.wikipedia.org/wiki/Git_(software)

     

    You should not be developing software without using source control.  Especially if your lack of source control causes you to write code like you're suggesting to mitigate issues arising from lack of source control.

     

    Lack of source control in your development cycle is your problem here.  Not how Application.cfc implements its mappings.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 8:26 AM   in reply to Didi

    Didi wrote:

     

    Version 1 (working):

     

    Application.cfc contains:

     

    ...

    <cffunction name="onApplicationStart">

        <cfinclude template="/include/appcfc-onApplicationStart.inc" >

    </cffunction>

    ...

     

    appcfc-onApplicationStart.inc contains:

     

    <cset something = "example code that run in that event" >

     

    ---------------------------------------------------------------------- -------------------------------------------------------------

    Version 2 (NOT working):

     

    Application.cfc contains :

     

    ...

    <cfinclude template="/include/appcfc-onApplicationStart.inc" >

    ...

     

     

    appcfc-onApplicationStart.inc contains:

     

    <cffunction name="onApplicationStart">

        <cset something = "example code that run in that event" >

    </cffunction>

    --------------------------------------------------------------------- --------------------------------------------------------------

    Ah! Then it confirms my initial instinct that ColdFusion might not be happy that the mapping is at once being written and read in the pseudo-constructor.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 9:23 AM   in reply to Didi

     

    CFB is a text editor, it's got nothing to do with source control.

    Sure!

    What product would be your advice as a good integration with CFB2?

     

     

    I can't speak for integration with CFB (or Eclipse for that matter), because I don't bother to try to integrate them.  Subversion has Eclipse plug-ins like Subverse and Subclipse, but I have found them both wanting compared to simply using TortoiseSVN via Window Explorer.

     

    I also can't speak for Git, as I have not used it.  I understand it solves a few things that people perceive to be suboptimal with Subversion.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 11:57 AM   in reply to Didi

    Didi wrote:

     

    Adam Cameron. wrote:

    CFB is a text editor, it's got nothing to do with source control.

     

      Adam

     

    Sure!

    What product would be your advice as a good integration with CFB2?

     

    -Didi

     

    EDIT: source control or not: I do not like those 789-line templates. So I would break it up also when using source control. But I definitely agree: as soon as I think I got the right combination CFB2 plus SC, I will use it!!!

    Didi, you do indeed need Version Control software. One word in your other thread says it all: configuration. To avoid confusion, I will give my suggestions on version control in that thread.

     

    In my opinion, the problem you're facing in this thread has less to do with version control, and more to do with information-hiding. That is, your code requires more information-hiding. That points to design.

     

    You have to break the large pieces of code up into smaller modules. The logic should be to separate the code into public and private parts. The more code you shift into the private parts, that is, the more information you hide, the better will be your design.

     

    A simple example to hide code in private modules is as follows:

     

    <cffunction name="onApplicationStart">

    <!--- call someFunction() to do some business when the application starts --->

    <!--- call someOtherFunction() to do some other business when the application starts --->

    </cffunction>

    <cffunction name="someFunction" access="private">

    <!--- business code --->

    </cffunction>

    <cffunction name="someOtherFunction" access="private">

    <!--- business code --->

    </cffunction>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 6:49 PM   in reply to Didi

    Regarding:

    In my Application.cfc

     

    <cfcomponent>

         <cfinclude template="/include/appcfc-onApplicationStart.inc" >

     

    I find that strange.  Why would you not have an onApplicationStart method in your cfc?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 1:28 AM   in reply to Didi

    I'm not sure if this has been mentioned, but I'd extend the Application.cfc rather than doing includes. It's not necessarily better or worse, but I think it makes more sense programmatically. Like so:

     

    [ApplicationBase.cfc]

    <cfcomponent>

      [onApplicationStart, other CF methods]

    </cfcomponent>

     

    [Application.cfc]

    <cfcomponent extends="ApplicationBase">

      [your own business methods]

    </cfcomponent>

     

    They can also extend each other, so you could have Application.cfc extending ApplicationBusinessMethods.cfc which in turn extends ApplicationCoreMethods.cfc which extends ApplicationBase.cfc, which has the built-in CF methods.

     

    Just another little something to throw into the mix there

     

    O.

     

    PS: CF Builder 2, VisualSVN Server and TortoiseSVN

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 2:13 AM   in reply to Didi

     

    I just wanted to get a lean Application.cfc, since I do not like to scroll around in codefiles having hundreds of lines.

    I prefer smaller code chunks - that's actually all behind it

     

    You should not structure / design your code (or try things that don't work!) just to appease your laziness.

     

    If you don't want to scroll about in a CFC, use the Outline view.  Or bookmarks.  Or the Find functionality.  Or leverage the inbuilt code folding that CFB provides.  Or just stop being lazy!

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 1:54 AM   in reply to Owain North

     

    I'm not sure if this has been mentioned, but I'd extend the Application.cfc rather than doing includes. It's not necessarily better or worse, but I think it makes more sense programmatically. Like so:

     

     

    Yep, using inheritance would be preferable to using includes in the way Didi is currently doing it, although it's kinda contorting the notion of inheritance a bit.  However just not doing either would be better here, I think.

     

    I could possibly see doing something like this as passable:

     

    // ApplicationEventHandlers.cfc

    component {

     

     

              this.name = "myApp";

              // etc

     

     

              function onApplicationStart(){

     

              }

     

              function onApplicationEnd(){

     

              }

    }

     

     

    // SessionEventHandlers.cfc

    component extends="ApplicationEventHandlers" {

     

     

              function onSessionStart(){

     

              }

     

              function onSessionEnd(){

     

              }

    }

     

     

    // RequestEventHandlers.cfc

    component extends="SessionEventHandlers" {

     

     

              function onRequestStart(){

     

              }

     

     

              function onRequest(){

     

              }

     

     

              function onMissingTemplate(){

     

              }

     

     

              function onError(){

     

              }

     

              function onRequestEnd(){

     

              }

    }

     

     

    However this is abusing the notion of inheritance I think.  And as per my previous post, basing one's architecture on developer-laziness is not something I'd engage in.  I'd just smarten-up my approach to my work practices/ethics.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 2:06 AM   in reply to Adam Cameron.

    Ooh, ooh, I've got another one now I've realised what you're probably doing wrong.

     

    Agreed no programming decision should ever be made on the aspect solely of asthetics, and in fact Partial Classes (the ability to spread a class definition over many files then having them compiled into one) is one of the many reasons I like C#.

     

    I'd guess your methods are loosely grouped based on function - move each of these out into a separate class, then create an instance in the Application scope. For example:

     

    [SecurityFunctions.cfc]

    <cfcomponent name="SecurityFunctions">

         [security-based methods]

    </cfcomponent>

     

    [Application.cfc]

    <cfcomponent>

         [onApplicationStart]

         <cfset Application.SecurityFunctions = new SecurityFunctions() />

    </cfcomponent>

     

    You can then call Application.SecurityFunctions.MyMethod() publicly, or stick it in the variables scope if you'd rather it be private.

     

    No bodging of code, just nice neat class usage. This is how I achieve what you're trying to.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 2:12 AM   in reply to Owain North

    Yeah, that's better than the inappropriate use of inheritance.

     

    It still leaves the main event handlers in the Application.cfc though... so it depends on whether the bulk of the code is in them or the support methods.

     

    However the code in the event handlers can probably be refactored into smaller / more purposeful methods though.

     

    Difficult to say without:

    a) seeing all the code;

    b) having more motivation to refactor someone else's code than I currently do.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 2:12 AM   in reply to Adam Cameron.

    b) having more motivation to refactor someone else's code than I currently do.

    Correct answer.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 5:22 AM   in reply to Didi

    Regarding "BTW: Do you work with CFB2? What version control system are you using?"

     

    I work with Dreamweaver.  We use Team Foundation Server for source control.  We also develop on a development server as opposed to our local machines.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points