我刚开始使用OO概念,如DAO、Gateways等,我正在努力找出实现AJAX可访问CFC的最佳方法,同时尽量不重复许多代码。
我有以下DAO,它包含我的DB表的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?
谢谢
发布于 2011-08-31 20:05:24
我认为可能会有许多不同的解决方案,因为会有很多人提出一个解决方案,但这里有一个。
我不会打开DAO进行远程访问,我会将其保留为包(并且只能由同一包中的某些业务对象访问)。我还在那些地方设置了某种外观,用于处理远程调用,以及验证远程传入的调用是否被允许进行调用。你不会想让任何人把东西塞进你的数据库的!facade应该处理身份验证方面的事情,如果一切正常,那么将调用传递给业务对象,然后业务对象使用DAO访问数据库。
我也不会在你的DAO中有那种try/catch的东西。通知调用代码出错的最好方法是抛出异常。然后调用代码可以决定如何处理它(是以某种方式处理它,忽略它,还是将它重新冒泡到站点范围的错误处理中)。
发布于 2011-08-31 21:48:25
我建议看看ColdSpring及其创建远程代理的能力,以及它使用面向方面编程保护远程代理的能力。这是一种很好的方法,可以仅将CFC的某些部分公开给远程访问,并控制访问它们的人。
ColdSpring快速入门指南:http://www.coldspringframework.org/coldspring/examples/quickstart/中涵盖了这两个主题
我还做了一个关于如何做到这一点的演示。你可以在这里看到录音:http://textiles.online.ncsu.edu/online/Viewer/?peid=a4227aeb1ad84fa89eeb3817f075af5b1d
https://stackoverflow.com/questions/7255909
复制相似问题