【发布时间】:2011-07-12 11:44:59
【问题描述】:
我想编写一个简单的工作单元类,其行为如下:
using (var unitOfWork = new UnitOfWork())
{
// Call the data access module and don't worry about transactions.
// Let the Unit of Work open a session, begin a transaction and then commit it.
}
这是我目前所拥有的(如果您认为我的设计有误,欢迎任何 cmets):
class UnitOfWork : IDisposable
{
ISession _session;
ITransation _transaction;
.
.
.
void Dispose()
{
_transaction.Commit();
_session.Dispose();
}
}
我想做的是回滚事务,以防数据访问代码抛出一些异常。所以Dispose() 方法看起来像:
void Dispose()
{
if (Dispose was called because an exception was thrown)
{
_transaction.Commit();
}
else
{
_transaction.RollBack();
}
_session.Dispose();
}
这有意义吗?如果可以,怎么做?
【问题讨论】:
标签: c# nhibernate exception-handling data-access-layer unit-of-work