ASP.NET MVC 2、Nhibernate 和跨层事务
我正在 ASP.NET MVC 2、NHibernate 和 DDD 中创建一个解决方案。我正在使用半 CQRS 类型模型。
ASP.NET 控制器将经过验证的消息发送到服务层,该服务层更新域对象的状态。 我有我的域调度“事件”,然后这些事件被对它们采取行动的“事件处理程序”捕获。每个事件处理程序都可以访问存储库层并可以提交域对象状态。
事件处理程序还使用(非 NHibernate )存储库将记录直接插入到基于报告的表中。事件处理程序还可以执行非数据库相关的操作,例如发送电子邮件。
事件处理程序还可以更改对象的状态,从而创建新的事件集。
我如何确保单个 asp.net 请求期间发生的所有数据库操作都在单个事务内。 我一直在阅读一些博客(例如 凯文·威廉姆斯,马特·沃克和Davy Brion)并获得了有关如何在开始和结束请求中启动会话对象的信息(我将再次在此处使用结构图),但不确定如何维护事务。由于开始和结束请求可能在不同的线程上调用,这一事实使情况变得更加复杂。
我的存储库类在其参数中采用 NHibernate ISession。如果我将 ISession 创建为混合作用域 (StructureMap),将确保在请求期间由 StructrueMap 传递的 ISession 参数保持不变。
如果我的问题不清楚,请提出建议并告诉我。
谢谢你
玛尔玛尔
,
I am creating a solution in ASP.NET MVC 2, NHibernate and DDD. I am using a semi CQRS type Model.
ASP.NET Controller send validated messaged to Service Layer which updates state of an Domain object.
I have my Domain Dispatch "Events" and these are then caught by "Event Handlers" who act on them. Each of these Event Handler have access to Repository Layer and can commit an Domain Object State.
Event Handlers also insert records directly into reporting based tables using a (non NHibernate ) Repository. Event Handlers may also do non database related operations like sending emails.
Event Handler can also change state of an object thereby creating new set of events.
How can I assure that all database operation that occur during a single asp.net Request are inside a single Transaction.
I have been reading some blogs ( like Kevin Williams , Matt Wrock and Davy Brion) and have got information on how to start a Session object in Begin and End Request ( Again I will be using Structure Map here) but not sure how the transaction is maintained. This was compounded by the fact that start and end Requests may be called on different threads.
My Repository Class takes NHibernate ISession in its Parameter. If I create ISession as Hybrid Scope ( StructureMap) will that ensure that during a request ISession parameter that is passed by StructrueMap remain same.
Please advise and also let me know if my question is not clear.
Thank you,
Mar
The Mar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以考虑为每个 Web 请求实现
工作单元模式
。工作单元创建 NHibernate 会话并处理事务。您可以在网上找到多种实现,例如 this 和 这个。You can consider implementing the
Unit Of Work pattern
for each web request. The unit of work creates an NHibernate session and also handles transactions. There are several implementations that you can find on the web such as this and this.除了 WorldIsRound 的答案之外,您还没有在描述中指定创建和提交事务的位置(根据您的解释,我假设它应该位于服务层的某个位置)。
我知道有一些工具可以为您管理您的会话,也许还可以管理您的交易,但在我看来,您希望明确控制您的交易。
我也在我的项目中使用 UOW 模式,在 Begin_Request 事件中创建一个 Session 对象,然后在需要时使用该对象创建事务。
再说一次,这只是我的意见,但我认为您应该完全控制打开和关闭交易。
祝你好运
in addition to WorldIsRound's answer- you haven't specified in your description where transactions are created and commited (from your explanation i'm assuming it should be somewhere in the Service layer).
I konw that there are tools that'll manage your sessions, and maybe your transactions, for you, but in my opinion, you want to explicitly control your transactions.
I use UOW pattern in my projects as well, to create a Session object in the Begin_Request event, and then I use that object to create transactions when I need them.
Again, that's just my opinion, but I think you should have complete control over opening and closing your transactions.
Good luck