工作单元问题 - Nhibernate(隔离级别以及 UoW 中还有什么)

发布于 2024-12-03 14:14:39 字数 1987 浏览 0 评论 0原文

我正在研究 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();
            }
        }
    }
  1. 我刚刚学习“IsolationLevels”,我想知道您应该使用哪一个?如果您需要对不同的事务使用多个“IsolationLevel”,会发生什么情况?您将如何配置您的 UoW(您会实现上述类的多个实现吗?)

  2. 除此之外,回滚和提交通常会进入 UoW?我知道诸如创建、更新、获取查询之类的内容会进入存储库(如果您使用此模式)那么您通常还会在其中看到什么?

  3. 我从某个网站复制了这个 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();
            }
        }
    }
  1. 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?)

  2. 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?

  3. 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 技术交流群。

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

发布评论

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

评论(1

九公里浅绿 2024-12-10 14:14:39

ISession 本身是一个工作单元实现,因此实现您自己的完全是可选的。

  1. IsolationLevel 在 System.Data 命名空间中声明。 ReadCommited是最常用的;这也是 SQL Server 的默认设置。我尝试使用 Chaos 一次只是因为我喜欢这个名字,但 SQL Server 不支持它。
  2. 问题中的代码在会话打开后立即创建一个事务,因此它是每个 UOW 实现的一个事务;请注意,如果您调用 Commit 或 Rollback,任何其他操作都将发生在事务范围之外。我真的不喜欢这个,更喜欢控制我自己的事务范围,所以我会在我的 UOW 中有一个 BeginTransaction。
  3. 实现 IDisposable 的类必须实现 Dispose 方法,并且应该在 using 块中使用或手动释放。这意味着该类有需要运行的清理操作,例如关闭 ISession。在这里,Dispose 方法应该只调用 ISession.Dispose,而不是仅仅关闭会话。

The ISession itself is a unit-of-work implementation, so implementing your own is entirely optional.

  1. IsolationLevel is declared in the System.Data namespace. ReadCommitted is the most commonly used; this is also the default for SQL Server. I tried using Chaos once just because I like the name but SQL Server doesn't support it.
  2. The code you have in the question creates a transaction as soon as the session is opened, so it's a transaction per UOW implementation; note that if you call Commit or Rollback, any additional operations will occur outside of transaction scope. I really dislike this and prefer to control my own transaction scope, so I would have a BeginTransaction in my UOW.
  3. Classes that implement IDisposable must implement the Dispose method and should be used in a 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文