Coldfusion 8 - DAO AJAX访问的最佳实践?

时间:2011-08-31 10:53:02

标签: ajax coldfusion dao

我很擅长使用诸如DAO,网关等OO概念,而我正试图找出实现AJAX可访问CFC的最佳方法,同时尽量不重复批量代码。

我有以下DAO,它为我的数据库表保存CRUD方法,并在其构造函数中将应用程序DSN作为参数:

<cfcomponent name="property_imageDAO" displayname="property_imageDAO" output="false" hint="">

<!--- pseudo constructor --->
<cfscript>
    variables.dsn = application.dsn;
</cfscript>

<!--- constructor --->
<cffunction name="init" access="public" output="false" returntype="any"
        hint="Constructor for this CFC">
    <!--- take DSN as argument --->
    <cfargument name="dsn" type="string" required="true" hint="The datasource name" />

    <!--- put dsn in variables scope so we can use it throughout the CFC --->
    <cfset variables.dsn = arguments.dsn />

    <!--- return this CFC --->
    <cfreturn this />
</cffunction>

<!--- CRUD methods (create, read, update, delete) --->
<!--- CREATE: inserts a new property_image into the database --->
<cffunction name="createRecord" access="remote" output="true"
        hint="Creates a new property_image record and returns a struct containing a boolean (success) indicating the success or
        failure of the operation, an id (id), and a string (message) containing a message">

    <!--- take property_image bean as argument --->
    <cfargument name="property_image" type="any" required="true" />

    <!--- initialize variables --->
    <cfset var results = StructNew() />
    <cfset var qInsertproperty_image = 0 />

    <!--- defaults --->
    <cfset results.success = true />
    <cfset results.message = "The record was inserted successfully." />

    <!--- insert the property_image --->
    <cftry>
        <cfquery name="qInsertproperty_image" datasource="#variables.dsn#">
            INSERT INTO property_image (
                name,
                alt 
            )
            VALUES (
                <cfqueryparam value="#arguments.property_image.getname()#" cfsqltype="cf_sql_varchar" />,
                <cfqueryparam value="#arguments.property_image.getalt()#" cfsqltype="cf_sql_varchar" />
            )
        </cfquery>
        <cfcatch type="database">
            <cfset results.success = false />
            <cfset results.message = "Inserting the record failed.  The error details if available are as follows: " & CFCATCH.Detail />
        </cfcatch>
    </cftry>

    <!--- return the struct --->
    <cfreturn StructCopy(results) />
</cffunction>

我应该为此DAO添加功能以使其可以访问AJAX,还是应该创建另一个专门用于远程访问的DAO?

由于

2 个答案:

答案 0 :(得分:3)

我认为解决这个问题的方法可能有很多,因为会有人提出建议,但这里只有一个。

我没有将DAO打开到REMOTE访问权限,我将其保留为PACKAGE(并且只能由同一个包中的某个业务对象访问)。我也有一些门面坐在那个处理远程呼叫的地段前面,以及诸如验证远程呼叫是否允许进行呼叫之类的事情。你不希望任何人在你的数据库中粘贴东西!外观应该处理事情的auth方面,然后如果所有OK都将调用传递给业务对象,然后使用DAO访问数据库。

我也不会在你的DAO中尝试/捕捉东西。通知调用代码出错的最佳方法是抛出异常。然后调用代码可以决定如何处理它(是否以某种方式处理它,忽略它,或者将它重新鼓泡到整个站点的错误处理)。

答案 1 :(得分:3)

我建议看一下ColdSpring及其创建远程代理的能力及其使用AOP保护它们的能力。这是一种很好的方式,只将CFC的某些部分暴露给远程访问,并控制访问它们的人。

ColdSpring快速入门指南中涵盖了这两个主题:http://www.coldspringframework.org/coldspring/examples/quickstart/

我还介绍了如何做到这一点。你可以在这里看到录音:http://textiles.online.ncsu.edu/online/Viewer/?peid=a4227aeb1ad84fa89eeb3817f075af5b1d

相关问题