【问题标题】:Coldfusion 8 - Best practice for DAO AJAX access?Coldfusion 8 - DAO AJAX 访问的最佳实践?
【发布时间】:2011-08-31 10:53:02
【问题描述】:

我对使用 OO 概念(如 DAO、网关等)还很陌生,我正在尝试找出实现 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?

谢谢

【问题讨论】:

    标签: ajax coldfusion dao


    【解决方案1】:

    我认为解决方案的变体可能与人们提出的建议一样多,但这里有一种看法。

    我不会打开 DAO 以进行远程访问,我会将其保留为 PACKAGE(并且只能由同一包中的某些业务对象访问)。我还会在处理远程呼叫的那块地段前面放置某种外观,以及验证远程进入的呼叫是否允许进行呼叫之类的事情。您不希望任何人在您的数据库中粘贴东西!外观应该处理事物的身份验证方面,然后如果一切正常,则将调用传递给业务对象,然后使用 DAO 访问数据库。

    我也不会在你的 DAO 中包含那些 try/catch 的东西。通知调用代码出错的最好方法是抛出异常。然后调用代码可以决定如何处理它(是否以某种方式处理它,忽略它,或者将它重新冒泡到站点范围的错误处理中)。

    【讨论】:

    • 您好 Adam,感谢您快速详细的回复。这一切都很有意义,尤其是您关于安全性的观点!
    【解决方案2】:

    我建议查看 ColdSpring 及其创建远程代理的能力以及使用 AOP 保护它们的能力。这是仅将 CFC 的某些部分公开给远程访问并控制访问者的好方法。

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

    我还做了一个关于如何做到这一点的演示。你可以在这里看到一段录音:http://textiles.online.ncsu.edu/online/Viewer/?peid=a4227aeb1ad84fa89eeb3817f075af5b1d

    【讨论】:

    • 感谢 Jason 的建议,我会检查一下。
    猜你喜欢
    • 2015-06-14
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 2011-04-26
    相关资源
    最近更新 更多