gpt4 book ai didi

coldfusion - ColdFusion 是否支持委托(delegate)?

转载 作者:行者123 更新时间:2023-12-01 11:04:32 26 4
gpt4 key购买 nike

我有几个包含在 try/catch block 中的数据库访问方法:

function GetAll() {
try {
entityLoad("Book");
}
catch (any e) {
throw (type="CustomException", message="Error accessing database, could not read");
}
}

function Save(Book book) {
try {
entitySave(book);
}
catch (any e) {
throw (type="CustomException", message="Error accessing database, could not save);
}
}

如您所见,try/catch block 重复了几次,唯一不同的是消息。是否可以在 ColdFusion 中创建一个委托(delegate),以便我可以改为执行类似的操作(使用 C# lambda 来表示匿名委托(delegate))?:

function GetAll() {
DatabaseOperation(() => entityLoad("Book"), "could not read");
}

function Save(Book book) {
DatabaseOperation(() => entitySave(book), "could not save");
}

function DatabaseOperation(delegate action, string error) {
try {
action.invoke();
}
catch (any e) {
var message = "Error accessing database, " & error;
throw (type="CustomException", message=message);
}
}

最佳答案

基于您的示例,而不是 CF9。

CF10 即将推出闭包功能,您可以这样做:

function GetAll() {
DatabaseOperation( closure(){ entityLoad("Book") } , "could not read");
}

function Save(Book book) {
DatabaseOperation( closure(){ entitySave(book) } , "could not save");
}

function DatabaseOperation(closure action, string error) {
try {
action();
}
catch (any e) {
var message = "Error accessing database, " & error;
throw (type="CustomException", message=message);
}
}

(语法可能会有所不同,不记得是否完全一样)


或者,我猜你可以在这里使用 evaluate ......

function GetAll() {
DatabaseOperation( 'entityLoad("Book")' , "could not read");
}

function Save(Book book) {
DatabaseOperation( 'entitySave(book)' , "could not save");
}

function DatabaseOperation(string action, string error) {
try {
evaluate(action);
}
catch (any e) {
var message = "Error accessing database, " & error;
throw (type="CustomException", message=message);
}
}


就我个人而言,我只是删除 try/catch 并在 Application.cfc 中使用 onError - 似乎对掩盖原始错误没有用,而是抛出一个更通用的错误?


更新:另外两个可能的选择...

当前可行的另一个选项是拥有一个公共(public)包装函数,该函数调用 DatabaseOperation 函数,传入执行实际逻辑的私有(private)函数的名称,如下所示:

function GetAll() {
DatabaseOperation( real_GetAll , Arguments , "could not read");
}
private function real_GetAll() {
entityLoad("Book")
}

function Save(Book book) {
DatabaseOperation( real_Save , Arguments , "could not save");
}
private function real_Save(Book book) {
entitySave(book)
}

function DatabaseOperation(any action, struct args , string error) {
try {
action( argumentcollection=args )
}
catch (any e) {
var message = "Error accessing database, " & error;
throw (type="CustomException", message=message);
}
}


如果您不喜欢两次定义函数的想法,但不介意模糊 API,您可以将方法设置为私有(private),然后使用 onMissingMethod:

private function GetAll()
{
entityLoad("Book")
}

private function Save(Book book)
{
entitySave(book)
}

function onMissingMethod( string MethodName , struct MethodArgs )
{
if ( NOT StructKeyExists(Variables,Arguments.MethodName) )
{
throw("The method #Arguments.MethodName# was not found");
}

try
{
var Meth = Variables[Arguments.MethodName];
Meth( ArgumentCollection=Arguments.MethodArgs );
}
catch(any e)
{
var message = "Error accessing database, ";

switch(MethodName)
{
case "GetAll":
message &= "could not read";
break;
case "Save":
message &= "could not save";
break;
}

throw (type="CustomException,message=message);
}
}

关于coldfusion - ColdFusion 是否支持委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7314027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com