Castle ActiveRecord SessionScopeWebModule异常处理

发布于 2024-10-06 17:50:05 字数 643 浏览 0 评论 0原文

我有一个启用了 SessionScopeWebModule 的 Web 应用程序,我想知道如何 最好地处理业务异常(不是 Hibernate / DB 异常)。

我相信 AR 2.0 SessionScopeWebModule 隐式实现 事务 - 如果请求期间发生异常,则进行更改 会话期间将被丢弃/回滚。

对于我的 Web 应用程序,业务层将与数据库进行交流,发出 几个 CRUD,比如

  • 创建实体 A
  • 创建实体 B

假设实体 A 已创建。然而,在创建实体 B 时,一些 未遵守业务规则,业务层抛出异常 业务异常。

从UI角度来看,最好“处理”业务异常 并向用户显示一条友好的消息。这就是 问题出在。如果异常已经被处理, SessionScopeWebModule 继续认为一切都很好 请求结束并将实体 A 提交到数据库中,无需 实体 B.

在这种情况下处理异常的最佳实践是什么?我是 考虑一个将为每个新的会话重置的会话变量 要求。如果发生异常,该变量将被标记为脏。 当请求结束时,SessionScopeWebModule会首先检查 会话以查看是否发生任何异常。如果是这样,它将调用 失败会话。

这是要走的路吗? FailSession 会完成丢弃的工作吗 更改会话?

谢谢!

伯彦

I have a Web app with SessionScopeWebModule enabled, and I wonder how
to best handle business exceptions (not Hibernate / DB exceptions).

I believe AR 2.0 SessionScopeWebModule implicitly implements
transaction - if an exception occurs during the request, changes made
during the session will be discarded / rolled back.

For my Web app, the business layer will be chatty with the DB, issuing
several CRUDs, say

  • Create entity A
  • Create entity B

Say entity A has been created. However upon creating entity B some
business rules were not observed and the business layer throws a
business exception.

From the UI perspective, it's best to 'handle' the business exception
and display a friendly message back to the user. This is where the
problem lies. If the exception is already handled,
SessionScopeWebModule goes along thinking everything is fine at the
end of the request and commits the entity A into database, without
entity B.

What is the best practice to handle exceptions in this case? I'm
thinking of a session variable that will be reset for each new
request. If an exception occurs, the variable will be marked dirty.
When the request ends, the SessionScopeWebModule will first check the
session to see if any exception has occured. If so, it will invoke a
FailSession.

Is this the way to go? And would FailSession do the job of discarding
changes to a session?

Thanks!

Boyan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-10-13 17:50:05

在网站 http://www.summerofnhibernate.com/ 上我已经找到了一个关于这个主题的网络聊天(这是第 13 部分),但直接使用 NHibernate(没有 ActiveRecord)。我解释了会话保存/刷新的方式和原因,以及如何阻止它这样做。也许该视频中的信息可以帮助您。

问候
朱伊·朱卡

on the website http://www.summerofnhibernate.com/ I have sean a web-chast about this topic (it's part 13), but with NHibernate directly (without ActiveRecord). I explains how and why the Session saves/flushes and how to stop it from doing that. Perhaps the informations from that video can help you.

Greetings
Juy Juka

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文