将数据放入应用程序范围

时间:2010-02-12 17:01:43

标签: coldfusion

对于我的查找表,对应用程序中的每个用户都是相同的,我做了

Application.objectname = createobject(...).init(datasource)

在init方法中,我将表读入此范围,如下所示:

cfquery name="this.queryname"
return this

现在,每当我需要引用查询时,我都可以像这样引用它:

cfselect query="Application.objectname.queryname" ...
问:这有什么不对吗?

3 个答案:

答案 0 :(得分:3)

不,那没关系。服务器将整个对象实例作为应用程序范围的一部分保留在内存中,该范围将包括其所有属性。

作为一个样式问题,我建议您将查询作为私有属性(在CFC中的variables范围内)而不是公共属性(在CFC的this范围内)。允许对象属性公开意味着作为黑盒子设计器,你可以使用未知的开发人员覆盖该值。如果这些是您正在存储的数据库查找表,我猜您打算将此数据设置为只读。请考虑以下事项:

<cfcomponent hint="Proxy for database lookup tables" output="false">
    <cfproperty name="variables.lookupTable1" type="query" hint="[Private] lookupTable1 query object." />
    <cfproperty name="variables.lookupTable2" type="query" hint="[Private] lookupTable2 query object." />
    <!--- Implicit initialization --->
    <cfscript>
        variables.lookupTable1 = QueryNew('');
        variables.lookupTable2 = QueryNew('');
    </cfscript>

    <!--- Active initialization --->
    <cffunction name="init" returntype="void" access="public" hint="Initializes the query objects with data." output="false">
        <cfargument name="dsn" type="string" required="true" hint="The datasource to use." />
        <cfquery name="variables.lookupTable1" datasource="#arguments.dsn#">
            SELECT * FROM [TblFoo]
        </cfquery>
        <cfquery name="variables.lookupTable2" datasource="#arguments.dsn#">
            SELECT * FROM [TblBar]
        </cfquery>
    </cffunction>

    <!--- Data Fetching Methods --->
    <cffunction name="getFoo" returntype="query" access="public" hint="Returns the contents of TblFoo." output="false">
        <cfreturn variables.lookupTable1 />
    </cffunction>

    <cffunction name="getBar" returntype="query" access="public" hint="Returns the contents of TblFoo." output="false">
        <cfreturn variables.lookupTable2 />
    </cffunction>
</cfcomponent>

答案 1 :(得分:1)

从句法上讲,没有。但是,我假设您还为该cfselect标记添加了“name”属性,因为它是必需的。

答案 2 :(得分:1)

如果这是您使用查询对象的唯一位置,则可能需要缓存cfselect下拉框的输出。 :)

如果您未在onApplicationStart()onServerStart()中设置应用范围变量,请不要忘记使用<cflock>