在NHibernate中如何在View模式中实现Open Session? [英] How implement the Open Session in View pattern in NHibernate?

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

问题描述

我正在使用ASP.NET MVC + NHibernate + Fluent NHibernate,并且有一个延迟加载的问题。



通过这个问题( 如何修复NHibernate延迟加载错误没有会话或会话被关闭? ),我发现我必须在View模式下实现Open Session,但是我不知道如何。



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

  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();
}
}

有人可以帮助我在View模式中实现Open Session



谢谢。

解决方案

但我不记得在哪里找到它。当您执行以下操作或类似的操作时,您将拥有所需的内容,并将一些代码重复减少到您的存储库中作为奖励。





  public class Repository 
{
私人只读ISession会话;

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

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

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

您还可以使用ioc容器管理会话,一个工作包装单元,而不是当前的会话环境。


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.

解决方案

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.

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

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