Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

PixelDragonsMVC.NET - An open source MVC framework

, 29 Jun 2007
An MVC framework with built-in NHibernate support that makes creating ASP.NET 2 applications easier by minimizing code and configuration
pixeldragonsmvc_demo.zip
PixelDragons.MVCSample.Domain
bin
Debug
Iesi.Collections.dll
PixelDragons.MVCSample.Domain.dll
PixelDragons.MVCSample.Domain.pdb
Dependencies
Iesi.Collections.dll
hbm
obj
Debug
PixelDragons.EasyMVCSample.Domain.dll
PixelDragons.EasyMVCSample.Domain.pdb
PixelDragons.MVCSample.Domain.dll
PixelDragons.MVCSample.Domain.pdb
SimpleMVCSample.Domain.dll
SimpleMVCSample.Domain.pdb
TempPE
PixelDragons.MVCSample.Domain.csproj.user
Properties
PixelDragons.MVC
bin
Debug
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll
PixelDragons.MVC.dll
PixelDragons.MVC.pdb
Release
Common
Configuration
Controllers
Dependencies
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll
Exceptions
Interfaces
obj
Debug
PixelDragons.EasyMVC.dll
PixelDragons.EasyMVC.pdb
PixelDragons.MVC.dll
PixelDragons.MVC.pdb
Refactor
PixelDragons.MVC.dll
ResolveAssemblyReference.cache
SimpleMVC.dll
SimpleMVC.pdb
TempPE
Release
Persistence
PixelDragons.MVC.csproj.user
Properties
PixelDragons.MVCSample
App_Code
Controllers
Base
Bin
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll
PixelDragons.MVC.dll
PixelDragons.MVC.pdb
PixelDragons.MVCSample.Domain.dll
PixelDragons.MVCSample.Domain.pdb
Documentation
PixelDragonsMVC.NET v0.2 BETA.odt
PixelDragonsMVC.NET v0.2 BETA.pdf
Thumbs.db
SQL Scripts
UI
controls
css
images
background.gif
buttons
back.gif
login.gif
next.gif
save.gif
Thumbs.db
icons
error.gif
info.gif
Thumbs.db
shadow_content.png
shadow_footer.png
shadow_header.png
Thumbs.db
javascript
masterPages
viewParts
views
contacts
users
/***********************************************************
 * 
 * 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.Collections.Generic;
using System.Text;
using System.Collections;

using NHibernate;
using NHibernate.Expression;

namespace PixelDragons.MVC.Persistence
{
    public class GenericDao<T> where T : new()
    {
		#region Fields
        private SessionManager _sessionManager = null;
		#endregion
		
		#region Constructors
        public GenericDao(SessionManager sessionManager)
		{
            _sessionManager = sessionManager;
		}
		#endregion
		
		#region Properties
		public ISession ActiveSession
		{
            get { return _sessionManager.ActiveSession; }
		}
		#endregion
		
		#region CRUD Methods
        public T Create()
        {
            T item = new T();

            return item;
        }

        public T Load(Guid itemUid)
        {
            ISession session = ActiveSession;
            T item = default(T);

            bool disconnect = false;
            if (!session.IsConnected)
            {
                session.Reconnect();
                disconnect = true;
            }
            try
            {
                item = (T)session.Load(typeof(T), itemUid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (session.IsConnected && disconnect)
                {
                    session.Disconnect();
                }
            }

            return item;
        }

        public T Get(Guid itemUid)
        {
            ISession session = ActiveSession;
            T item = default(T);

            bool disconnect = false;
            if (!session.IsConnected)
            {
                session.Reconnect();
                disconnect = true;
            }
            try
            {
                item = (T)session.Get(typeof(T), itemUid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (session.IsConnected && disconnect)
                {
                    session.Disconnect();
                }
            }

            return item;
        }

        public T Store(T item)
        {
            ISession session = ActiveSession;

            bool disconnect = false;
            if (!session.IsConnected)
            {
                session.Reconnect();
                disconnect = true;
            }
            try
            {
                session.SaveOrUpdate(item);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (session.IsConnected && disconnect)
                {
                    session.Disconnect();
                }
            }
            return item;
        }

        public void Delete(Guid itemUid)
        {
            ISession session = ActiveSession;

            bool disconnect = false;
            if (!session.IsConnected)
            {
                session.Reconnect();
                disconnect = true;
            }
            try
            {
                T item = (T)session.Load(typeof(T), itemUid);
                session.Delete(item);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (session.IsConnected && disconnect)
                {
                    session.Disconnect();
                }
            }
        }
        #endregion

        #region List Methods
        public List<T> ListAll()
		{
			List<T> list = null;
			ISession session = ActiveSession;
			bool disconnect = false;
			if (!session.IsConnected)
			{
				session.Reconnect();
				disconnect = true;
			}
			try
			{
                list = (List<T>)session.CreateCriteria(typeof(T)).List<T>();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (session.IsConnected && disconnect)
				{
					session.Disconnect();
				}
			}

            return list;
		}

        public List<T> ListPaged(PageSettings pageSettings)
		{
            List<T> list = null;
			ISession session = ActiveSession;
			bool disconnect = false;
			if (!session.IsConnected)
			{
				session.Reconnect();
				disconnect = true;
			}
			try
			{
				ICriteria criteria =  session.CreateCriteria(typeof(T));

                foreach (SimpleExpression expression in pageSettings.FilterExpressions)
				{
                    if (expression != null)
					{
                        criteria.Add(expression as ICriterion);
					}
				}

                if (pageSettings.CalculateTotal)
				{
                    //Slow way to get the total count, refactor later to use supplied hql that uses count(*)
                    list = (List<T>)criteria.List<T>();
					pageSettings.TotalItemCount = list.Count;
				}

                foreach (string columnName in pageSettings.ColumnSorting.Keys)
				{
                    SortDirection sort = pageSettings.ColumnSorting[columnName];
                    
                    if (sort == SortDirection.Ascending)
					{
                        criteria.AddOrder(Order.Asc(columnName));
					}
					else
					{
                        criteria.AddOrder(Order.Desc(columnName));
					}
				}

                list = (List<T>)criteria.SetFirstResult(pageSettings.CurrentPageIndex * pageSettings.PageSize).SetMaxResults(pageSettings.PageSize).List();

                return list;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (session.IsConnected && disconnect)
				{
					session.Disconnect();
				}
			}
		}

		public List<T> ListUsingQuery(string hql, params object[] args)
		{
            hql = String.Format(hql, args);

			List<T> list = null;
			ISession session = ActiveSession;
			bool disconnect = false;
			if (!session.IsConnected)
			{
				session.Reconnect();
				disconnect = true;
			}
			try
			{
                IQuery query = session.CreateQuery(hql);
                list = (List<T>)query.List<T>();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (session.IsConnected && disconnect)
				{
					session.Disconnect();
				}
			}

            return list;
		}

        public List<T> ListPagedUsingQuery(PageSettings pageSettings, string hql, params object[] args)
        {
            hql = String.Format(hql, args);

            List<T> list = null;
            ISession session = ActiveSession;
            bool disconnect = false;
            if (!session.IsConnected)
            {
                session.Reconnect();
                disconnect = true;
            }
            try
            {
                IQuery query = session.CreateQuery(hql);

                if (pageSettings.CalculateTotal)
                {
                    //Slow way to get the total count, refactor later to use supplied hql that uses count(*)
                    list = (List<T>)query.List<T>();
                    pageSettings.TotalItemCount = list.Count;
                }

                list = (List<T>)query.SetFirstResult(pageSettings.CurrentPageIndex * pageSettings.PageSize).SetMaxResults(pageSettings.PageSize).List<T>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (session.IsConnected && disconnect)
                {
                    session.Disconnect();
                }
            }

            return list;
        }
        #endregion

        #region Other Methods
        public void Evict(T item)
        {
            ActiveSession.Evict(item);
        }
		#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

Share

About the Author

BigAndy
Web Developer
United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 29 Jun 2007
Article Copyright 2007 by BigAndy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid