Good afternoon,

I have start implement a asp.net webpage using Nhibernate as Database 
Mapping. I am quiet new to the technology and would like to get start with 
a right direction. I have followup this 
turtorial 
http://www.codeproject.com/Articles/13390/NHibernate-Best-Practices-with-ASP-NET-nd-Ed
 
but still can not get thing done in the right way. 

The following is my script that I have modified in abstract class. Could 
you please let me know does the modification is correct? I have added 
method *this.CommitChanges();  to all Save, SaveOrUpdate, and Delete 
methode.*


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Criterion;
using Tec.Core.DataInterfaces;
using Tec.Data.SessionManagement;



namespace Tec.Data
{
    public abstract class NHibernateRepository<T, IdT> : IRepository<T, IdT>
    {
        public T GetById(IdT id, bool shouldLock)
        {
            T entity;

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

            return entity;
        }


        public List<T> GetAll()
        {
            return GetByCriteria();
        }


        public List<T> GetByCriteria(params ICriterion[] criterion)
        {
            ICriteria criteria = 
NHibernateSession.CreateCriteria(persitentType);

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

            return criteria.List<T>() as List<T>;
        }

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

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

            criteria.Add(example);

            return criteria.List<T>() as List<T>;
        }


        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);
            }
        }


        public T Save(T entity) {
            NHibernateSession.Save(entity);
            *this.CommitChanges(); // manually added*
            return entity;
        }


        public T SaveOrUpdate(T entity)
        {
            NHibernateSession.SaveOrUpdate(entity);
            *this.CommitChanges(); //manually added      *  
            return entity;
        }

        public void Delete(T entity) {
            NHibernateSession.Delete(entity);
            *this.CommitChanges(); //manually added   *        
        }


        public void CommitChanges(){
            if (NHibernateSessionFactory.Instance.HasOpenTransaction()) {
                NHibernateSessionFactory.Instance.CommitTransaction();
            } else {
                // If there's no transaction, just flush the changes
                NHibernateSessionFactory.Instance.GetSession().Flush();
            }
        }


        private ISession NHibernateSession
        {
            get
            {
                return NHibernateSessionFactory.Instance.GetSession();
            }
        }

        private Type persitentType = typeof(T);
    }




    
//----------------------------------------------------------------------------------//




    public class NHibernateSessionFactory
    {
        public static NHibernateSessionFactory Instance
        {
            get
            {
                return Nested.NHibernateSessionFactory;
            }
        }


        private NHibernateSessionFactory()
        {
            InitSessionFactory();
        }


        private class Nested
        {
            static Nested() { }
            internal static readonly NHibernateSessionFactory 
NHibernateSessionFactory = new NHibernateSessionFactory();
        }



        private void InitSessionFactory()
        {
            sessionFactory = new 
Configuration().Configure().BuildSessionFactory();
        }


        public void RegisterInterceptor(IInterceptor interceptor)
        {
            ISession session = ContextSession;
            if (session != null && session.IsOpen)
            {
                throw new CacheException("You cannot register an 
interceptor once a session has already been opened");
            }
            GetSession(interceptor);
        }

        public ISession GetSession()
        {
            return GetSession(null);
        }


        private ISession GetSession(IInterceptor interceptor)
        {
            ISession session = ContextSession;
            if (session == null)
            {
                if (interceptor != null)
                {
                    session = sessionFactory.OpenSession(interceptor);
                }
                else
                {
                    session = sessionFactory.OpenSession();
                }
                ContextSession = session;
            }
            return session;
        }


        public void CloseSession()
        {
            ISession session = ContextSession;
            if (session != null && session.IsOpen)
            {
                session.Flush();
                session.Close();
            }
            ContextSession = null;
        }

        public void BeginTransaction()
        {
            ITransaction transaction = ContextTransaction;
            if (transaction == null)
            {
                transaction = GetSession().BeginTransaction();
                ContextTransaction = transaction;
            }
        }

        public void CommitTransaction()
        {
            ITransaction transaction = ContextTransaction;
            try
            {
                if (HasOpenTransaction())
                {
                    transaction.Commit();
                    ContextTransaction = null;
                }
            }
            catch (HibernateException)
            {
                RollbackTransaction();
                throw;
            }
        }

        public bool HasOpenTransaction()
        {
            ITransaction transaction = ContextTransaction;
            return transaction != null && !transaction.WasCommitted && 
!transaction.WasRolledBack;
        }

        public void RollbackTransaction()
        {
            ITransaction transaction = ContextTransaction;

            try
            {
                if (HasOpenTransaction())
                {
                    transaction.Rollback();
                }
                ContextTransaction = null;
            }
            finally
            {
                CloseSession();
            }
        }


        private ITransaction ContextTransaction
        {
            get
            {
                if (IsInWebContext())
                {
                    return 
(ITransaction)HttpContext.Current.Items[TRANSACTION_KEY];
                }
                else
                {
                    return 
(ITransaction)CallContext.GetData(TRANSACTION_KEY);
                }
            }
            set
            {
                if (IsInWebContext())
                {
                    HttpContext.Current.Items[TRANSACTION_KEY] = value;
                }
                else
                {
                    CallContext.SetData(TRANSACTION_KEY, value);
                }
            }
        }


        private ISession ContextSession
        {
            get
            {
                if (IsInWebContext())
                {
                    return (ISession)HttpContext.Current.Items[SESSION_KEY];
                }
                else
                {
                    return (ISession)CallContext.GetData(SESSION_KEY);
                }
            }
            set
            {
                if (IsInWebContext())
                {
                    HttpContext.Current.Items[SESSION_KEY] = value;
                }
                else
                {
                    CallContext.SetData(SESSION_KEY, value);
                }
            }
        }

        private bool IsInWebContext()
        {
            return HttpContext.Current != null;
        }

        private const string TRANSACTION_KEY = "CONTEXT_TRANSACTION";
        private const string SESSION_KEY = "CONTEXT_SESSION";
        private ISessionFactory sessionFactory;

    }


}

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to