Search code examples
coldfusionrailocfml

ColdFusion - When to use the "request" scope?


Been going over my predecessor's code and see usage of the "request" scope frequently. What is the appropriate usage of this scope?


Solution

  • There are several scopes that are available to any portion of your code: Session, Client, Cookie, Application, and Request. Some are inadvisable to use in certain ways (i.e. using Request or Application scope inside your Custom Tags or CFC's; this is coupling, violates encapsulation principles, and is considered a bad practice), and some have special purposes: Cookie is persisted on the client machine as physical cookies, and Session scoped variables are user-specific and expire with the user's session on the website.

    If a variable is extremely unlikely to change (constant for all intents and purposes) and can simply be initialized on application startup and never written again, generally you should put it into Application scope because this persists it between every user and every session. When properly implemented it is written once and read N times.

    A proper implementation of Application variables in Application.cfm might look like this:

    <cfif not structKeyExists(application, "dsn")>
        <cflock scope="application" type="exclusive" timeout="30">
            <cfif not structKeyExists(application, "dsn")>
                <cfset application.dsn = "MyDSN" />
                <cfset foo = "bar" />
                <cfset x = 5 />
            </cfif>
        </cflock>
    </cfif>
    

    Note that the existence of the variable in the application scope is checked before and after the lock, so that if two users create a race condition at application startup, only one of them will end up setting the application variables.

    The benefit of this approach is that it won't constantly refresh these stored variables on every request, wasting the user's time and the server's processing cycles. The trade-off is that it is a little verbose and complex.

    This was greatly simplified with the addition of Application.cfc. Now, you can specify which variables are created on application startup and don't have to worry about locking and checking for existence and all of that fun stuff:

    <cfcomponent>
        <cfset this.name = "myApplicationName" />
    
        <cffunction name="onApplicationStart" returnType="boolean" output="false">
            <cfset application.dsn = "MyDSN" />
            <cfset foo = "bar" />
            <cfset x = 5 />
            <cfreturn true />
        </cffunction>
    </cfcomponent>
    

    For more information on Application.cfc including all of the various special functions available and every little detail about what and how to use it, I recommend this post on Raymond Camden's blog.

    To summarize, request scope is available everywhere in your code, but that doesn't necessarily make it "right" to use it everywhere. Chances are that your predecessor was using it to break encapsulation, and that can be cumbersome to refactor out. You may be best off leaving it as-is, but understanding which scope is the best tool for the job will definitely make your future code better.