Coldfusion 8-DAO AJAX访问的最佳实践?
我对使用诸如DAO、网关等OO概念相当陌生,我正在努力找出实现AJAX可访问CFC的最佳方法,同时尽量避免重复大量代码 我有以下DAO,它为我的DB表保存CRUD方法,并将应用程序DSN作为其构造函数中的参数:Coldfusion 8-DAO AJAX访问的最佳实践?,ajax,coldfusion,dao,Ajax,Coldfusion,Dao,我对使用诸如DAO、网关等OO概念相当陌生,我正在努力找出实现AJAX可访问CFC的最佳方法,同时尽量避免重复大量代码 我有以下DAO,它为我的DB表保存CRUD方法,并将应用程序DSN作为其构造函数中的参数: <cfcomponent name="property_imageDAO" displayname="property_imageDAO" output="false" hint=""> <!--- pseudo constructor ---> <cfs
<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>
variables.dsn=application.dsn;
插入到属性图像中(
名称
中高音
)
价值观(
,
)
我应该向这个DAO添加功能以使其可以通过AJAX访问,还是应该专门为远程访问创建另一个DAO
谢谢我认为可能会有很多不同的解决方案,因为会有人提出建议,但这里有一个解决方案 我不会打开DAO进行远程访问,而是将其作为包(并且只能由同一包中的某些业务对象访问)。我也会有一些门面坐在处理远程呼叫的停车场前面,以及验证远程传入的呼叫是否被允许进行呼叫之类的事情。你不希望任何人把东西粘在你的数据库里!facade应该处理事物的auth端,然后如果一切正常,则将调用传递给业务对象,然后业务对象使用DAO访问DB
我也不会在你的刀上放那种试试看的东西。通知调用代码出错的最好方法是抛出异常。然后,调用代码可以决定如何处理它(是以某种方式处理它、忽略它,还是将它重新冒泡到站点范围的错误处理中)。我建议查看ColdSpring及其创建远程代理的能力和使用AOP保护它们的能力。这是一种很好的方法,可以只对CFC的某些部分进行远程访问,并控制访问者 《ColdSpring快速入门指南》介绍了这两个主题:
我还做了一个关于如何做到这一点的演示。你可以在这里看到一段录音:你好,亚当,谢谢你迅速而详细的回复。这一切都很有道理,特别是你关于安全性的观点!谢谢你的建议,杰森,我去看看。