ColdFusion 支持代表吗?
我有几种封装在 try/catch 块中的数据库访问方法:
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 块重复了多次,唯一不同的是消息。是否可以在 ColdFusion 中创建委托,以便我可以执行类似的操作(使用 C# lambda 表示匿名委托)?:
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);
}
}
I have several database access methods that are wrapped in a 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);
}
}
As you can see, the try/catch block is repeated several times, where the only thing that varies is the message. Is it possible to create a delegate in ColdFusion so that I can do something like this instead (using a C# lambda to represent an anonymous 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的示例,而不是 CF9。
CF10 中将提供闭包,这可能允许您执行以下操作:(
语法可能有所不同,不记得是否完全一样)
或者,我猜你可以在这里使用
evaluate
...就我个人而言,我只是删除 try/catch 并在
Application.cfc
中使用onError
- 似乎对于掩盖原始错误没有用,而是抛出一个更通用的错误?更新:还有两个可能的替代方案...
当前有效的另一个选择是使用一个公共包装函数,该函数调用 DatabaseOperation 函数,并传入执行实际逻辑的私有函数的名称,如下所示:
如果您不喜欢两次定义函数的想法,但不介意模糊 API,您可以将方法设置为私有,然后使用 onMissingMethod:
Based on your example, not with CF9.
Closures are coming in CF10 which will probably allow you to do something like:
(syntax might vary, don't remember if it was exactly like that)
Alternatively, you could probably use
evaluate
here, I guess...Personally I would just remove the try/catch and use
onError
inApplication.cfc
- doesn't seem to be useful to mask the original error and instead throw a more generic one?Update: two more possible alternatives...
Another option that currently works is to have a public wrapper function, that calls the DatabaseOperation function, passing in the name of a private function that does the actual logic like this:
If you don't like the idea of defining functions twice, but don't mind obscuring the API, you could set the methods to private then use onMissingMethod: