28 Replies Latest reply: Apr 9, 2012 5:22 AM by Dan Bracuk RSS

    Application.cfc, THIS.mappings & cfinclude

    Didi

      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

        • 1. Re: Application.cfc, THIS.mappings & cfinclude
          BKBK MVP

          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.

          • 2. Re: Application.cfc, THIS.mappings & cfinclude
            Didi Community Member

            Hi BKBK

             

            not searching for eggs at these days  ?

             

            'Enable Per Application Settings' ist checked.

             

            Believe me (at least on my server ..)

             

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

             

            is not working as long as the <cfinclude ..> is in the 'outer space' of the Application.cfc.

            The idea is to implement the events inside includes.

             

            On the other hand:

             

              <cffunction name="onApplicationEnd" >

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

              </cffunction>

             

            where I declare the event in the 'outer space' of the Application.cfc and I only put the body of the event of the include, it works.

            So I think my theory gets more meat on the bone.

             

            -----

            now to your note of accessing the include:

             

            Why not an INC extension to viualize the difference between a directly called CFM page and an include?

            It just makes it more visible ...

             

              http://server/include/appcfc-onApplicationStart.inc


            doesn't expose the include file. The mapping is CF internal only - Apache does not know about this.

            That's why I took it outside the webroot.

              Z:\webincludes

            is parallel to

                Z:\webroot

             

            -----

            our goal is structuring code and not putting all in one huge Application.cfc, since out onXxxStart events are quite long.

            How would you do that?

             

            -Didi

            • 3. Re: Application.cfc, THIS.mappings & cfinclude
              BKBK MVP

              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.

              • 4. Re: Application.cfc, THIS.mappings & cfinclude
                Adam Cameron. Community Member

                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

                • 5. Re: Application.cfc, THIS.mappings & cfinclude
                  BKBK MVP

                  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.

                  • 6. Re: Application.cfc, THIS.mappings & cfinclude
                    Adam Cameron. Community Member

                     

                     

                    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

                    • 7. Re: Application.cfc, THIS.mappings & cfinclude
                      Didi Community Member

                      Adam Cameron. wrote:

                       

                      BKBK:

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

                       

                      There's the catch ...

                       

                      What I do now is defining all events in Application.cfc and puttin all the code inside the events in a include, eg:

                       

                      <cffunction name="onApplicationStart">

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

                      </cffunction>

                       

                      That works fine.

                       

                      Adam:

                      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 :-)

                       

                      -Didi

                      • 8. Re: Application.cfc, THIS.mappings & cfinclude
                        Didi Community Member

                        sorry for space confusing, to be more precise on that:

                         

                        <cfcomponent>

                          <cfscript>

                            THIS.name = "myName";

                            {etc.}

                         

                            THIS.mappings = StructNew();

                            mappingname = "/include";

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

                            THIS.mappings[mappingname] = mappingpath;

                          </cfscript>

                          

                          <cffunction name="onApplicationStart">

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

                          </cffunction>

                           {etc.}


                        </cfcomponent>

                         

                        That way it works.

                         

                        -Didi

                        • 9. Re: Application.cfc, THIS.mappings & cfinclude
                          BKBK MVP

                          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?

                          • 10. Re: Application.cfc, THIS.mappings & cfinclude
                            Adam Cameron. Community Member

                             

                            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

                            • 11. Re: Application.cfc, THIS.mappings & cfinclude
                              Didi Community Member

                              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>

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

                               

                              -Didi

                              • 12. Re: Application.cfc, THIS.mappings & cfinclude
                                Didi Community Member

                                Adam Cameron. wrote:

                                 

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

                                 

                                --

                                Adam

                                 

                                We use CFBuilder2.

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

                                 

                                I am still open for good ideas ..

                                 

                                -Didi

                                • 13. Re: Application.cfc, THIS.mappings & cfinclude
                                  Adam Cameron. Community Member

                                   

                                  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

                                  • 14. Re: Application.cfc, THIS.mappings & cfinclude
                                    BKBK MVP

                                    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.

                                    • 15. Re: Application.cfc, THIS.mappings & cfinclude
                                      Didi Community Member

                                      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!!!

                                      • 16. Re: Application.cfc, THIS.mappings & cfinclude
                                        Adam Cameron. Community Member

                                         

                                        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

                                        • 17. Re: Application.cfc, THIS.mappings & cfinclude
                                          BKBK MVP

                                          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>

                                          • 18. Re: Application.cfc, THIS.mappings & cfinclude
                                            Didi Community Member

                                            BKBK:

                                             

                                            I 100% agree with you!

                                             

                                            What I actually did not mention above: besides the 200 lines of code in the Application.cfc and the included events,

                                            there is another include containing 8 UDFs with another 300 lines of code .

                                            But your are right, I should scope most of them as private ... (they are all public yet).

                                             

                                            What are you using for version control?

                                             

                                            -Didi

                                            • 19. Re: Application.cfc, THIS.mappings & cfinclude
                                              Dan Bracuk Community Member

                                              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?

                                              • 20. Re: Application.cfc, THIS.mappings & cfinclude
                                                Didi Community Member

                                                Dan:

                                                 

                                                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

                                                 

                                                Now it looks the way as you see below ...

                                                 

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

                                                 

                                                -Didi

                                                 

                                                ___________________________________________________________________________

                                                 

                                                <cfcomponent>

                                                  <cfscript>

                                                    THIS.name = "myName";

                                                    {etc.}

                                                 

                                                    THIS.mappings = StructNew();

                                                    mappingname = "/include";

                                                    mappingpath = "Z:\webincludes-outside-webroot\appcfc";

                                                    THIS.mappings[mappingname] = mappingpath;

                                                  </cfscript>

                                                 

                                                  <cffunction name="onApplicationStart">

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

                                                  </cffunction>

                                                 

                                                  <cffunction name="onApplicationEnd" >

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

                                                  </cffunction>

                                                 

                                                  <cffunction name="onSessionStart" >

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

                                                  </cffunction>

                                                 

                                                  <cffunction name="onSessionEnd" >

                                                    <cfargument name="SessionScope" required=true />

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

                                                  </cffunction>

                                                 

                                                  <cffunction name="onRequestStart" >

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

                                                  </cffunction>

                                                 

                                                  <cffunction name="onRequestEnd" >

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

                                                  </cffunction>

                                                 

                                                </cfcomponent>

                                                <!--- ***** END of Application.cfc ***** --->

                                                • 21. Re: Application.cfc, THIS.mappings & cfinclude
                                                  Owain North Community Member

                                                  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

                                                  • 22. Re: Application.cfc, THIS.mappings & cfinclude
                                                    Adam Cameron. Community Member

                                                     

                                                    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

                                                    • 23. Re: Application.cfc, THIS.mappings & cfinclude
                                                      Adam Cameron. Community Member

                                                       

                                                      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

                                                      • 24. Re: Application.cfc, THIS.mappings & cfinclude
                                                        Owain North Community Member

                                                        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.

                                                        • 25. Re: Application.cfc, THIS.mappings & cfinclude
                                                          Adam Cameron. Community Member

                                                          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

                                                          • 26. Re: Application.cfc, THIS.mappings & cfinclude
                                                            Owain North Community Member

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

                                                            Correct answer.

                                                            • 27. Re: Application.cfc, THIS.mappings & cfinclude
                                                              Didi Community Member

                                                              Hey, that's why I am here -- to learn

                                                              Owain North wrote:

                                                               

                                                              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.

                                                               

                                                              I think, this is the way I will follow in the future ..

                                                               

                                                              Thanx to all of you that give us such valuable support here

                                                               

                                                              -Didi

                                                              • 28. Re: Application.cfc, THIS.mappings & cfinclude
                                                                Dan Bracuk Community Member

                                                                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.