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

NHibernate Best Practices with ASP.NET, 1.2nd Ed.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (322 votes)
11 Jun 2008CPOL60 min read 8.6M   35.9K   1.1K  
This article describes best practices for leveraging the benefits of NHibernate 1.2, ASP.NET, generics and unit testing together.
using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Expression;
using NHibernateSample.Core.DataInterfaces;

namespace NHibernateSample.Data
{
    public abstract class GenericNHibernateDao<T, ID> : IGenericDao<T, ID>
    {
        /// <summary>
        /// Exposes the ISession used within the DAO.
        /// </summary>
        private ISession session {
            get {
                return NHibernateSessionManager.Instance.GetSession();
            }
        }

        /// <summary>
        /// Loads an instance of type T from the DB based on its ID.
        /// </summary>
        public T GetById(ID id, bool shouldLock) {
            T entity;

            if (shouldLock) {
                entity = (T)session.Load(persitentType, id, LockMode.Upgrade);
            }
            else {
                entity = (T)session.Load(persitentType, id);
            }

            return entity;
        }

        /// <summary>
        /// Loads every instance of the requested type with no filtering.
        /// </summary>
        public List<T> GetAll() {
            return GetByCriteria();
        }

        /// <summary>
        /// Loads every instance of the requested type using the supplied <see cref="ICriterion" />.
        /// If no <see cref="ICriterion" /> is supplied, this behaves like <see cref="GetAll" />.
        /// </summary>
        public List<T> GetByCriteria(params ICriterion[] criterion) {
            ICriteria criteria = session.CreateCriteria(persitentType);

            foreach (ICriterion criterium in criterion) {
                criteria.Add(criterium);
            }

            return ConvertToGenericList(criteria.List());
        }

        public List<T> GetByExample(T exampleInstance, params string[] propertiesToExclude) {
            ICriteria criteria = session.CreateCriteria(persitentType);
            Example example = Example.Create(exampleInstance);

            foreach (string propertyToExclude in propertiesToExclude) {
                example.ExcludeProperty(propertyToExclude);
            }

            criteria.Add(example);

            return ConvertToGenericList(criteria.List());
        }

        /// <summary>
        /// Looks for a single instance using the example provided.
        /// </summary>
        /// <exception cref="NonUniqueResultException" />
        public T GetUniqueByExample(T exampleInstance, params string[] propertiesToExclude) {
            List<T> foundList = GetByExample(exampleInstance, propertiesToExclude);

            if (foundList.Count > 1) {
                throw new NonUniqueResultException(foundList.Count);
            }

            if (foundList.Count > 0) {
                return foundList[0];
            }
            else {
                return default(T);
            }
        }

        /// <summary>
        /// For entities that have assigned ID's, you must explicitly call Save to add a new one.
        /// See http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-id-assigned.
        /// </summary>
        public T Save(T entity) {
            session.Save(entity);
            return entity;
        }

        /// <summary>
        /// For entities with automatatically generated IDs, such as identity, SaveOrUpdate may 
        /// be called when saving a new entity.  SaveOrUpdate can also be called to update any 
        /// entity, even if its ID is assigned.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T SaveOrUpdate(T entity) {
            session.SaveOrUpdate(entity);
            return entity;
        }

        public void Delete(T entity) {
            session.Delete(entity);
        }

        public void CommitChanges() {
            NHibernateSessionManager.Instance.CommitTransaction();
        }

        /// <summary>
        /// Accepts a list of objects and converts them to a strongly typed list.
        /// </summary>
        /// <remarks>This should be put into a utility class as it's not directly
        /// related to data access.</remarks>
        private List<T> ConvertToGenericList(System.Collections.IList listObjects) {
            List<T> convertedList = new List<T>();

            foreach (object listObject in listObjects) {
                convertedList.Add((T)listObject);
            }

            return convertedList;
        }

        private Type persitentType = typeof(T);
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions