在NHibernate中如何在View模式中实现Open Session? [英] How implement the Open Session in View pattern in 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
谢谢。
但我不记得在哪里找到它。当您执行以下操作或类似的操作时,您将拥有所需的内容,并将一些代码重复减少到您的存储库中作为奖励。
- 使用(在全球范围内修改问题的代码)
Application_EndRequest不触发404 - 对于Web请求中执行的每个方法,使用相同的会话和事务实例。
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.
- Use this (code from question modified with the answer) in global.asax: Application_EndRequest Doesn't Fire on a 404
- Use the same session and transaction instance for every method executed during a web-request.
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屋!