Click here to Skip to main content
15,881,689 members
Articles / Web Development / ASP.NET

PixelDragonsMVC.NET - An open source MVC framework

Rate me:
Please Sign up or sign in to vote.
4.58/5 (8 votes)
29 Jun 200710 min read 84.8K   811   73  
An MVC framework with built-in NHibernate support that makes creating ASP.NET 2 applications easier by minimizing code and configuration
/***********************************************************
 * 
 * Copyright 2007 Pixel Dragons Ltd. All rights reserved.
 * 
 * http://www.codeplex.com/PixelDragonsMVC for licence, more
 * information and latest source code. 
 * 
 * Check out our other stuff at http://www.pixeldragons.com
 * 
 ***********************************************************/

using System;
using System.Web;
using System.Configuration;

using PixelDragons.MVC.Configuration;

using NHibernate;
using log4net;

namespace PixelDragons.MVC.Persistence
{
	public class SessionManager
	{
		#region Fields
        private static readonly ILog _logger = LogManager.GetLogger(typeof(SessionManager));
		private ISessionFactory _sessionFactory = null;
		private ISession _session = null;
		#endregion

        #region Constructors
        public SessionManager(MVCConfigurationHandler settings)
        {
            NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();

            foreach (EntityAssembly assembly in settings.EntityAssemblies)
            {
                config.AddAssembly(assembly.Name);
            }

            _sessionFactory = config.BuildSessionFactory();
        }
        #endregion

		#region Properties
		public ISessionFactory SessionFactory
		{
			get { return _sessionFactory; }
			set { _sessionFactory = value; }
		}

        public ISession ActiveSession
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Items["ActiveSession"] == null)
                    {
                        OpenActiveSession();
                    }
                    return (ISession)HttpContext.Current.Items["ActiveSession"];
                }
                else
                {
                    if (_session == null)
                    {
                        OpenActiveSession();
                    }
                    return _session;
                }
            }
        }

        public ISession TemporarySession
        {
            get
            {
                ISession tempSession = SessionFactory.OpenSession();
                return tempSession;
            }
        }
		#endregion

        #region Methods
        public ISession OpenSession()
		{
            return _sessionFactory.OpenSession();
		}

		public ISession OpenSession(IInterceptor interceptor)
		{
			if (interceptor != null)
			{
                return _sessionFactory.OpenSession(interceptor);
			}
			else
			{
                return _sessionFactory.OpenSession();
			}
		}	

		public void OpenActiveSession()
		{
			if (HttpContext.Current != null)
			{
				HttpContext.Current.Items["ActiveSession"] = OpenSession();
			}
			else
			{
				if (_session == null)
				{
					_session = OpenSession();
				}
			}
		}

		public void CloseActiveSession(TransactionManager transactionManager)
		{
			if (HttpContext.Current != null)
			{
				if (HttpContext.Current.Items["ActiveSession"] != null)
				{
					ISession activeSession = (ISession)HttpContext.Current.Items["ActiveSession"];
					if (activeSession != null && activeSession.IsOpen)
					{
						try
						{
                            foreach (ITransaction transaction in transactionManager.Transactions)
                            {
                                if (transaction != null && (!transaction.WasCommitted) && (!transaction.WasRolledBack))
                                {
                                    _logger.Debug("Committing transaction");
                                    transaction.Commit();
                                }
                            }

                            activeSession.Flush();
						}
						finally
						{
							activeSession.Close();
							HttpContext.Current.Items["ActiveSession"] = null;
						}
					}
					HttpContext.Current.Items["ActiveSession"] = null;
				}
			}
			else
			{
				if (_session != null)
				{
					if (_session != null && _session.IsOpen)
					{
						try
						{
                            foreach (ITransaction transaction in transactionManager.Transactions)
                            {
                                if (transaction != null && (!transaction.WasCommitted) && (!transaction.WasRolledBack))
                                {
                                    _logger.Debug("Committing transaction");
                                    transaction.Commit();
                                }
                            }

                            _session.Flush();                            
						}
						finally
						{
							_session.Close();
							_session = null;
						}
					}
					_session = null;
				}
			}
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions