NHibernate: Repository Session Management

Currently my repository has 2 constructors. When I call these from my mvc website, I always call the first constructor, thus opening a new session .Should I pass it at the meeting? What should I do?

public CompanyRepository()
{
_session = NHibernateHelper.OpenSession();
}

public CompanyRepository(ISession session)
{
_session = session;
}




public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;

private static ISessionFactory SessionFactory
{
get
{
if ( _sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(UserProfile).Assembly);
configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName,
System.Environment.MachineName);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}

public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}

I am using the Ninject IOC container (very new to me). I have the following container. How do I tie the ISession Set to CompanyRepository.

private class EStoreDependencies: NinjectModule
{
public override void Load()
{
Bind().To();
Bind().To();
Bind().To();
Bind().To();
Bind().To();
Bind().To() ;
Bind().To();
Bind().To();
Bind().To();
Bind().To();
}
}

You should use the “one session per request” mode by storing the ISession object in the HttpContext and sharing it between the repository and the queries made during the same HTTP request.

This is an implementation using MVC action attributes.

Simple/basic implementation can also be achieved by simply changing your NHibernateHelper class:

public class NHibernateHelper {
//...

const string SessionKey = "NhibernateSessionPerRequest";

public static ISession OpenSession(){
var context = HttpContext.Current;

if(context != null && context.Items.ContainsKey(SessionKey)){
//Return already open ISession
return ( ISession)context.I tems[SessionKey];
}
else{
//Create new ISession and store in HttpContext
var newSession = SessionFactory.OpenSession();
if(context! = null)
context.Items[SessionKey] = newSession;

return newSession;
}
}
}

Code Neither compiled nor tested…but it should work.

Currently my repository has 2 constructors. When I call these from my mvc website , I always call the first constructor to open a new session. Should I pass it in the meeting? What should I do?

public CompanyRepository()
{
_session = NHibernateHelper.OpenSession();
}

public CompanyRepository(ISession session)
{
_session = session;
}




public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;

private static ISessionFactory SessionFactory
{
get
{
if ( _sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(UserProfile).Assembly);
configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName,
System.Environment.MachineName);
_sessionFactory = con figuration.BuildSessionFactory();
}
return _sessionFactory;
}
}

public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}

I am using the Ninject IOC container (very new to me). I have the following container. How do I tie the ISession Set to CompanyRepository.

private class EStoreDependencies: NinjectModule
{
public override void Load()
{
Bind().To();
Bind().To();
Bind().To();
Bind().To();
Bind().To();
Bind().To() ;
Bind ().To();
Bind().To();
Bind().To();< br /> Bind().To();
}
}

You should use “every One request per session” mode, the method is to store the ISession object in the HttpContext and share it between the repository and the queries made during the same HTTP request.

This is an implementation using MVC action attributes.

Simply changing your NHibernateHelper class can also achieve a simple/basic implementation:

public class NHibernateHelper {
/ /...

const string SessionKey = "NhibernateSessionPerRequest";

public static ISession OpenSession(){
var context = HttpContext.Current;

if(context != null && context.Items.ContainsKey(SessionKey)){
//Return already open ISession
return (ISession)context.Items[SessionKey];
}
else{
//Create new ISession and store in HttpContext
var newSession = SessionFactory.OpenSession();
if(context != null)
context.Items[SessionKey] = newSession;

return newSession;
}
}
}

The code is neither compiled nor tested…but it should work.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4498 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.