在NHibernate的视图模式如何实现打开会话? [英] How implement the Open Session in View pattern in NHibernate?

查看:149
本文介绍了在NHibernate的视图模式如何实现打开会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC + +的NHibernate流利的NHibernate和具有延迟加载的问题。

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

通过这个问题(http://stackoverflow.com/questions/2519964/how-to-fix-a-nhibernate-lazy-loading-error-no-session-or-session-was-closed),我发现,我要实现在View模式公开会议,但我不知道怎么办。

Through this question (http://stackoverflow.com/questions/2519964/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();
    }
}

有人可以帮助我在View模式来实现打开会话?

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

感谢您。

推荐答案

这是之前已经问过,但我不记得在哪里可以找到它。当你做类似下面的什么的,你有你想要的东西,有些code复制在你的仓库作为奖金减少。

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.


  • 使用在Global.asax中这(从与答案修改的问题code):
    <一href=\"http://stackoverflow.com/questions/2450687/application-endrequest-dosent-fire-on-a-404/2450821\">Application_EndRequest在404 不火

  • 使用一个Web请求期间执行每个方法相同的会话和​​交易实例。

&NBSP;

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容器和包装工作,而不是当前会话上下文的单元管理会话。

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

这篇关于在NHibernate的视图模式如何实现打开会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆