Advertisement
Guest User

Joe

a guest
Sep 9th, 2010
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using NHibernate.Cfg;
  6. using NHibernate;
  7. using FluentNHibernate.Cfg;
  8. using FluentNHibernate.Cfg.Db;
  9. using ShoeParty.Mappings;
  10. using System.Reflection;
  11.  
  12. namespace ShoeParty.Repositories
  13. {
  14.     public class NHibernateHelper
  15.     {
  16.         public Assembly ConfigurationAssembly { get; private set; }
  17.  
  18.         public NHibernateHelper(Assembly configurationAssembly)
  19.         {
  20.             this.ConfigurationAssembly = configurationAssembly;
  21.         }
  22.  
  23.         private static ISessionFactory _sessionFactory;
  24.         private static ISession _currentSession;
  25.  
  26.         public ISession GetSession()
  27.         {
  28.             ISessionFactory factory = this.GetSessionFactory();
  29.             ISession session = this.GetExistingOrNewSession(factory);
  30.             return session;
  31.         }
  32.  
  33.         public virtual Configuration GetConfiguration()
  34.         {
  35.             var configuration = new Configuration();
  36.             configuration.Configure();
  37.             configuration.AddAssembly(this.ConfigurationAssembly);
  38.             return configuration;
  39.         }
  40.  
  41.         private ISession GetExistingOrNewSession(ISessionFactory factory)
  42.         {
  43.             if (HttpContext.Current != null)
  44.             {
  45.                 ISession session = this.GetExistingWebSession();
  46.                 if (session == null)
  47.                 {
  48.                     session = this.OpenSessionAndAddToContext(factory);
  49.                 }
  50.                 else if (!session.IsOpen)
  51.                 {
  52.                     session = this.OpenSessionAndAddToContext(factory);
  53.                 }
  54.  
  55.                 return session;
  56.             }
  57.  
  58.             if (_currentSession == null)
  59.             {
  60.                 _currentSession = factory.OpenSession();
  61.             }
  62.             else if (!_currentSession.IsOpen)
  63.             {
  64.                 _currentSession = factory.OpenSession();
  65.             }
  66.  
  67.             return _currentSession;
  68.         }
  69.  
  70.         public ISession GetExistingWebSession()
  71.         {
  72.             return HttpContext.Current.Items[GetType().FullName] as ISession;
  73.         }
  74.  
  75.         private ISession OpenSessionAndAddToContext(ISessionFactory factory)
  76.         {
  77.             ISession session = factory.OpenSession();
  78.             HttpContext.Current.Items.Remove(this.GetType().FullName);
  79.             HttpContext.Current.Items.Add(this.GetType().FullName, session);
  80.             return session;
  81.         }
  82.  
  83.         public void Dispose()
  84.         {
  85.             if (_sessionFactory != null)
  86.             {
  87.                 GetExistingOrNewSession(_sessionFactory).Dispose();
  88.                 _sessionFactory.Dispose();
  89.             }
  90.         }
  91.  
  92.  
  93.         private ISessionFactory GetSessionFactory()
  94.         {
  95.             if (_sessionFactory == null)
  96.             {
  97.                 Configuration configuration = GetConfiguration();
  98.                 _sessionFactory = configuration.BuildSessionFactory();
  99.             }
  100.  
  101.             return _sessionFactory;
  102.         }
  103.  
  104.         //This is my original code before replacing it with
  105.         // the stuff that uses HttpContext
  106.         //-------------------------------------------------
  107.         //private static ISessionFactory SessionFactory
  108.         //{
  109.         //    get
  110.         //    {
  111.         //        if (_sessionFactory == null)
  112.         //        {
  113.         //            _sessionFactory = Fluently.Configure()
  114.         //                .Database(
  115.         //                    MsSqlConfiguration.MsSql2008
  116.         //                        .ConnectionString(c => c.FromConnectionStringWithKey("ShoesFullAccess"))
  117.         //                )
  118.         //                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<BrandMap>())
  119.         //                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<StyleMap>())
  120.         //                .BuildSessionFactory();
  121.         //        }
  122.  
  123.         //        return _sessionFactory;
  124.         //    }
  125.         //}
  126.  
  127.         //public static ISession OpenSession()
  128.         //{
  129.         //    return SessionFactory.OpenSession();
  130.         //}
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement