工作单元问题 - Nhibernate(隔离级别以及 UoW 中还有什么)
我正在研究 UoW 模式并有 3 个问题。
public class UnitofWork : unitofwork.Models.IUnitofWork
{
private readonly ITransaction transaction;
private readonly ISession session;
public UnitofWork(ISession session)
{
this.session = session;
session.FlushMode = FlushMode.Auto;
transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void Commit()
{
if (!transaction.IsActive)
{
throw new InvalidOperationException("Oops! We don't have an active transaction");
}
transaction.Commit();
}
public void Rollback()
{
if (transaction.IsActive)
{
transaction.Rollback();
}
}
public void Dispose()
{
if (session.IsOpen)
{
session.Close();
}
}
}
我刚刚学习“IsolationLevels”,我想知道您应该使用哪一个?如果您需要对不同的事务使用多个“IsolationLevel”,会发生什么情况?您将如何配置您的 UoW(您会实现上述类的多个实现吗?)
除此之外,回滚和提交通常会进入 UoW?我知道诸如创建、更新、获取查询之类的内容会进入存储库(如果您使用此模式)那么您通常还会在其中看到什么?
我从某个网站复制了这个 UoW(现在手头没有)并对其进行了更改以满足我的需求(例如我正在使用 ninject,所以我觉得 UoW 中的 sessionFactory 没有意义)并在 UoW 中打开一个会话)
我想知道 Dispose 的用途是什么?我之前见过几次(有些似乎实现了 IDispose)。
我现在实际上并没有在我的任何代码中使用它。我想知道这对我来说是否有必要,正如我提到的,我正在使用 ninject 并处理会话(即完成后关闭它)
public void Dispose()
{
if (session.IsOpen)
{
session.Close();
}
}
编辑
我将其添加到我的工作单元中
public void BeginTransaction()
{
transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void BeginTransaction(IsolationLevel level)
{
transaction = session.BeginTransaction(level);
}
,我将其从中删除构造函数
I am looking into the UoW pattern and have 3 questions.
public class UnitofWork : unitofwork.Models.IUnitofWork
{
private readonly ITransaction transaction;
private readonly ISession session;
public UnitofWork(ISession session)
{
this.session = session;
session.FlushMode = FlushMode.Auto;
transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void Commit()
{
if (!transaction.IsActive)
{
throw new InvalidOperationException("Oops! We don't have an active transaction");
}
transaction.Commit();
}
public void Rollback()
{
if (transaction.IsActive)
{
transaction.Rollback();
}
}
public void Dispose()
{
if (session.IsOpen)
{
session.Close();
}
}
}
I just learning about "IsolationLevels" and I am wondering which one should you be using? What happens if you need to make use of multiple "IsolationLevels" for different transactions? How would you configure your UoW(would you make multiple implementations of the above class?)
Other then Rollback and Commit was usually goes into the UoW? I know stuff like creating,updating,getting queries would go into a repository(if your using this pattern) So what else would you typicall see in it?
I copied this UoW from some site(don't have it on hand right now) and made changes it to fit my needs(for instance I am using ninject so I felt there was no point in the UoW taking in the sessionFactory and opening a session in the UoW)
I am wondering what is the Dispose for? I seen this a few times before(some seem to implement IDispose).
I don't actually use it right now in any of my code. I am wondering if it is necessary for me as I mentioned I am using ninject and that handles the session(ie closes it once I am done)
public void Dispose()
{
if (session.IsOpen)
{
session.Close();
}
}
Edit
I added this to my unit of work
public void BeginTransaction()
{
transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void BeginTransaction(IsolationLevel level)
{
transaction = session.BeginTransaction(level);
}
I removed it from the constructor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ISession 本身是一个工作单元实现,因此实现您自己的完全是可选的。
using
块中使用或手动释放。这意味着该类有需要运行的清理操作,例如关闭 ISession。在这里,Dispose 方法应该只调用 ISession.Dispose,而不是仅仅关闭会话。The ISession itself is a unit-of-work implementation, so implementing your own is entirely optional.
using
block or disposed of manually. It means that the class has clean up operations that need to run, such as closing the ISession. Here, the Dispose method should just call ISession.Dispose instead of just closing the session.