Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using NHibernate.Cfg;
- using NHibernate;
- using FluentNHibernate.Cfg;
- using FluentNHibernate.Cfg.Db;
- using ShoeParty.Mappings;
- using System.Reflection;
- namespace ShoeParty.Repositories
- {
- public class NHibernateHelper
- {
- public Assembly ConfigurationAssembly { get; private set; }
- public NHibernateHelper(Assembly configurationAssembly)
- {
- this.ConfigurationAssembly = configurationAssembly;
- }
- private static ISessionFactory _sessionFactory;
- private static ISession _currentSession;
- public ISession GetSession()
- {
- ISessionFactory factory = this.GetSessionFactory();
- ISession session = this.GetExistingOrNewSession(factory);
- return session;
- }
- public virtual Configuration GetConfiguration()
- {
- var configuration = new Configuration();
- configuration.Configure();
- configuration.AddAssembly(this.ConfigurationAssembly);
- return configuration;
- }
- private ISession GetExistingOrNewSession(ISessionFactory factory)
- {
- if (HttpContext.Current != null)
- {
- ISession session = this.GetExistingWebSession();
- if (session == null)
- {
- session = this.OpenSessionAndAddToContext(factory);
- }
- else if (!session.IsOpen)
- {
- session = this.OpenSessionAndAddToContext(factory);
- }
- return session;
- }
- if (_currentSession == null)
- {
- _currentSession = factory.OpenSession();
- }
- else if (!_currentSession.IsOpen)
- {
- _currentSession = factory.OpenSession();
- }
- return _currentSession;
- }
- public ISession GetExistingWebSession()
- {
- return HttpContext.Current.Items[GetType().FullName] as ISession;
- }
- private ISession OpenSessionAndAddToContext(ISessionFactory factory)
- {
- ISession session = factory.OpenSession();
- HttpContext.Current.Items.Remove(this.GetType().FullName);
- HttpContext.Current.Items.Add(this.GetType().FullName, session);
- return session;
- }
- public void Dispose()
- {
- if (_sessionFactory != null)
- {
- GetExistingOrNewSession(_sessionFactory).Dispose();
- _sessionFactory.Dispose();
- }
- }
- private ISessionFactory GetSessionFactory()
- {
- if (_sessionFactory == null)
- {
- Configuration configuration = GetConfiguration();
- _sessionFactory = configuration.BuildSessionFactory();
- }
- return _sessionFactory;
- }
- //This is my original code before replacing it with
- // the stuff that uses HttpContext
- //-------------------------------------------------
- //private static ISessionFactory SessionFactory
- //{
- // get
- // {
- // if (_sessionFactory == null)
- // {
- // _sessionFactory = Fluently.Configure()
- // .Database(
- // MsSqlConfiguration.MsSql2008
- // .ConnectionString(c => c.FromConnectionStringWithKey("ShoesFullAccess"))
- // )
- // .Mappings(m => m.FluentMappings.AddFromAssemblyOf<BrandMap>())
- // .Mappings(m => m.FluentMappings.AddFromAssemblyOf<StyleMap>())
- // .BuildSessionFactory();
- // }
- // return _sessionFactory;
- // }
- //}
- //public static ISession OpenSession()
- //{
- // return SessionFactory.OpenSession();
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement