如何在 NHibernate 中实现 Open Session in View 模式?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以前已经问过这个问题,但我不记得在哪里可以找到它。当您执行以下操作或类似操作时,您将获得所需的内容,并且作为奖励,您的存储库中会减少一些代码重复。
Application_EndRequest 不会在 404 上触发
您还可以使用 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.
Application_EndRequest Doesn't Fire on a 404
You can also manage the session with an ioc container and a unit of work wrapper instead of the current session context.