Nhibernate:谁负责非 Web 应用程序中的事务管理
在 Web 应用程序中,工作单元负责事务管理。
但是 Windows 应用程序呢?
据我所知,存储库是我的数据访问层和业务层之间的连接器。 它隐藏了我的业务层的所有数据访问内容。
利用这个事实让我想到将所有交易内容放入存储库中。
但我读到,在存储库上使用 Commit/RollBack 方法违反了存储库的意图。
我问自己,谁负责非 Web 应用程序中的事务管理,以及如何从业务层隐藏事务/Nhibernate 内容?
Well in a web application a unit of work is responsible for the transaction management.
But what about a windows application?
As far as I know the repository is the connector between my data access layer and my business layer.
It hides all the data access stuff from my business layer.
Using this fact let me think of taking all the transaction stuff into the repository.
But I read that having Commit/RollBack methods on the repository is violating the repository's intent.
I ask myself who is responsible for transaction management in a non web application and how do I hide the transaction/Nhibernate stuff from the business layer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般的答案是“无论谁实例化
ISession
都应该处置它。如果事务尚未提交,则这实际上是回滚。”我已经成功地使用命令模式来定义我想要对工作单元执行的操作。假设我们有一个
Person
实体,我们可以做的事情之一就是更改一个人的名字。让我们从实体开始:定义一个简单的 POCO 命令,如下所示:
...以及该命令的处理程序:
目标是存在于会话/工作范围之外的代码可以执行如下操作:
命令的调用意味着“在工作单元上执行此命令。”这就是我们在调用命令时希望发生的情况:
IHandle
IHandle<来自 IoC 范围的 ChangeNameCommand>
下面是一个使用 Autofac 作为 IoC 容器:
注意:我在此处展示的
UnitOfWorkInvoker
违反了 SRP - 这是UnitOfWorkFactory
、UnitOfWork
和Invoker
合而为一。在我的实际实现中,我把它们分解了。The general answer is "Whoever instantiates the
ISession
should dispose of it. If the transaction has not been committed, this is effectively a rollback."I've had success by using the command pattern to define an operation that I want to perform on a unit of work. Say we have a
Person
entity and one of the things we can do is change a person's name. Let's start with the entity:Define a simple POCO Command like this:
...and a Handler for the command:
The goal is that code that exists outside of a Session/Work scope can do something like this:
The invocation of the command implies "do this command on a unit of work." This is what we want to happen when we invoke the command:
IHandle<ChangeNameCommand>
from the IoC scopeSo here's an example using Autofac as the IoC container:
Note: The
UnitOfWorkInvoker
I've shown here is violating SRP - it is aUnitOfWorkFactory
, aUnitOfWork
, and anInvoker
all in one. In my actual implementation, I broke them out.当我使用存储库时,它们包含在一个工作单元中。工作单元跟踪存储库的更改并处理事务管理。
为什么在 Web 应用程序中使用工作单元来处理事务管理是有效的,而不是在 Windows 应用程序中?如果它是一个 N 层应用程序,您的业务层实际上将在两者之间共享。
When I use repositories, they are contained within a unit of work. The unit of work tracks changes to the repositories and handles transaction management.
Why would it be valid to use a unit of work to handle transaction management in a web application and not in a windows application? If it's an N-Tier application, your business layer would actually be shared between both.