如何在 NHibernate 中实现 Open Session in View 模式?

发布于 2024-08-27 10:03:44 字数 1695 浏览 5 评论 0原文

我正在使用 ASP.NET MVC + NHibernate + Fluent NHibernate 并遇到延迟加载问题。

通过这个问题(如何修复 NHibernate 延迟加载错误“没有会话或会话已关闭”?),我发现我必须在视图模式中实现“打开会话”,但我不这样做不知道怎么办。

在我的存储库类中,我使用这样的方法

    public ImageGallery GetById(int id) {
        using(ISession session = NHibernateSessionFactory.OpenSession()) {
            return session.Get<ImageGallery>(id);
        }
    }

    public void Add(ImageGallery imageGallery) {
        using(ISession session = NHibernateSessionFactory.OpenSession()) {
            using(ITransaction transaction = session.BeginTransaction()) {
                session.Save(imageGallery);
                transaction.Commit();
            }
        }
    }

这是我的会话工厂帮助程序类:

public class NHibernateSessionFactory {
    private static ISessionFactory _sessionFactory;
    private static ISessionFactory SessionFactory {
        get {
            if(_sessionFactory == null) {
                _sessionFactory = Fluently.Configure()
                    .Database(MySQLConfiguration.Standard.ConnectionString(MyConnString))
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ImageGalleryMap>())
                    .ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"))
                    .BuildSessionFactory();
            }
            return _sessionFactory;
        }
    }
    public static ISession OpenSession() {
        return SessionFactory.OpenSession();
    }
}

有人可以帮助我在视图模式中实现打开会话吗?

谢谢。

I'm using ASP.NET MVC + NHibernate + Fluent NHibernate and having a problem with lazy loading.

Through this question (How to fix a NHibernate lazy loading error "no session or session was closed"?), I've discovered that I have to implement the Open Session in View pattern , but I don't know how.

In my repositories classes, I use methods like this

    public ImageGallery GetById(int id) {
        using(ISession session = NHibernateSessionFactory.OpenSession()) {
            return session.Get<ImageGallery>(id);
        }
    }

    public void Add(ImageGallery imageGallery) {
        using(ISession session = NHibernateSessionFactory.OpenSession()) {
            using(ITransaction transaction = session.BeginTransaction()) {
                session.Save(imageGallery);
                transaction.Commit();
            }
        }
    }

And this is my Session Factory helper class:

public class NHibernateSessionFactory {
    private static ISessionFactory _sessionFactory;
    private static ISessionFactory SessionFactory {
        get {
            if(_sessionFactory == null) {
                _sessionFactory = Fluently.Configure()
                    .Database(MySQLConfiguration.Standard.ConnectionString(MyConnString))
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ImageGalleryMap>())
                    .ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"))
                    .BuildSessionFactory();
            }
            return _sessionFactory;
        }
    }
    public static ISession OpenSession() {
        return SessionFactory.OpenSession();
    }
}

Someone could help me to implements Open Session in View pattern?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

清醇 2024-09-03 10:03:44

以前已经问过这个问题,但我不记得在哪里可以找到它。当您执行以下操作或类似操作时,您将获得所需的内容,并且作为奖励,您的存储库中会减少一些代码重复。

 

public class Repository
{
  private readonly ISession session;

  public Repository()
  {
    session = CurrentSessionContext.CurrentSession();
  } 

  public ImageGallery GetById(int id) 
  {
    return session.Get<ImageGallery>(id);
  }

  public void Add(ImageGallery imageGallery)
  {
    session.Save(imageGallery);
  }
}

您还可以使用 ioc 容器和工作单元包装器(而不是当前会话上下文)来管理会话。

This is already asked before, but I don't remember where to find it. When you do the following or something similar, you have what you want and some code duplication reduce in your repositories as bonus.

 

public class Repository
{
  private readonly ISession session;

  public Repository()
  {
    session = CurrentSessionContext.CurrentSession();
  } 

  public ImageGallery GetById(int id) 
  {
    return session.Get<ImageGallery>(id);
  }

  public void Add(ImageGallery imageGallery)
  {
    session.Save(imageGallery);
  }
}

You can also manage the session with an ioc container and a unit of work wrapper instead of the current session context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文