How to properly use a NHibernate ISession object - Session Is Closed! errors

前端 未结 4 1732
伪装坚强ぢ
伪装坚强ぢ 2021-01-04 19:23

I\'m running into issues with my ISessions in NHibernate. I keep getting \"Session Closed!\" errors. Can some one please show me the correct pattern including a definition o

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-04 19:33

    About the problem, your method of locking is right as long as you dispose the session but probably the bug lies under another part of your codes. by the way about the design, it is better that you pass the session variable to repositories because of unit of work implementation of the session and aggregate root's transaction like this:

    using (ISession session = SessionFactory.OpenSession())
    {
        Repository1 rep1 = new Repository1(session);
        Repository2 rep1 = new Repository2(session);
        Repository3 rep1 = new Repository3(session);
    
        // some logics
    
        using (var tx = session.BeginTransaction())
            tx.Commit();
    }
    

    . . .

提交回复
热议问题