在一个小项目应用了NHibernate 0.5 版 后继版看了好象没有什么大的变动.在此仍以0.5版作讨论.
  • 项目体验
  • 个人感觉
  • 一些疑问
项目体验
  1.通过Factory类对NHibernate作统一的设置
using System;
using freedom.data ;
using NHibernate.Cfg ;
using NHibernate.Dialect;
namespace freedom.dal
{
    /// <summary>
    /// Factory 的摘要说明。
    /// </summary>
    public class Factory
    {
        private static NHibernate.Cfg.Configuration  cfg;
        private static NHibernate.ISessionFactory Isession ;

        public Factory()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public static void BuildSessionFactory()
        {
                cfg =new Configuration();
                cfg.AddAssembly("freedom");
                Isession=cfg.BuildSessionFactory();
        }

        /// <summary>
        /// 打开会话
        /// </summary>
        /// <param name="datatype"></param>
        public  static NHibernate.ISession  SesionOpen()
        {
            if (Isession==null)
            {
                BuildSessionFactory();
            }
           
             return Isession.OpenSession();
       
        }
       

    }
}
2.通过EntityControl类对所有操作统一.
using System;
using System.Collections ;
using NHibernate;
using NHibernate.Cfg ;
using NHibernate.Hql;
using NHibernate.Expression;

namespace freedom.dal
{
    /// <summary>
    /// EntityControl 的摘要说明。
    /// </summary>
    public class EntityControl
    {
        public EntityControl()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
   
        //添加实体
        public bool addEntity(Object entity)
        {
            bool isok =true;
            ISession s = Factory.SesionOpen();
            ITransaction t = s.BeginTransaction();

            try
            {
                s.Save(entity);
                t.Commit();
            }
            catch(Exception e)
            {
                t.Rollback();
                isok=false;


                throw e;

           
            }
            finally
            {
                s.Close();
           
            }
            return isok;
        }

        
        //更新实体
        public bool updateEnity(Object entity,Object key)
        {   
            bool isok =true;
            ISession s = Factory.SesionOpen();
            ITransaction t = s.BeginTransaction();

            try
            {
                s.Update(entity,key);
                t.Commit();
            }
            catch(Exception e)
            {
                t.Rollback();
                throw e;
                isok=false;
            }
            finally
            {
                s.Close();
            }
            return isok;   
        }


        //删除实体
        public void DelEntity(object entity)
        {
            ISession s = Factory.SesionOpen();
            ITransaction t = s.BeginTransaction();
            try
            {
                s.Delete(entity);
                t.Commit();

            }
            catch(Exception e)
            {
                t.Rollback();
                throw e;
            }
            finally
            {
                s.Close();
            }

           
        }
       

        public string getdeptid(string dwdm)
        {
            IList lst;
            string ifquery="(";
            string query="select d.deptid  from department  as d where dwdm like '"+dwdm+"%'";
            ISession s = Factory.SesionOpen();
            lst=s.Find(query);
            ifquery="deptid in (";
            for(int i=0;  i<lst.Count;i++ )
            {
                ifquery+=lst[i].ToString();
                if(i<lst.Count -1)
                {
                    ifquery+=",";
                }
            }
            ifquery+=")   ";
           
            s.Close();
            return ifquery;

        }

        public IList GetEntities(String query)
        {
            IList lst;
            ISession s = Factory.SesionOpen();
            lst=s.Find(query);
            s.Close();

            return lst;
        }


        public IList  FindEntities(System.Type  thetype,string propertyName ,string Pvalue)
        {
            ISession s = Factory.SesionOpen();
            Expression  ex =  Expression.Eq(propertyName,Pvalue);
            IList retList =s.CreateCriteria(thetype).Add(ex).List( );
            s.Close();
            return retList;
        }

        public Object GetEntity(Type theType,Object id)
        {
            Object obj;
            ISession s = Factory.SesionOpen();
            obj = s.Load(theType,id);
            s.Close();
            return obj;
        }

        public IList GetEntities(System.Type thetype)
        {
            ISession s = Factory.SesionOpen();
            IList objectList = s.CreateCriteria(thetype).List();
            return objectList;

        }
    }
}
3.数据表对象映射类
在这里我要提出一个问题,因为Person表与Department表关连,在此我没有用NHibernate 的一对类,或多对一 来实现,而是通自定义的一个方法来实现的.不知好不好?
using System;
using freedom.dal ;

namespace freedom.data
{
    public class  Person
    {
        public Person()
        {

        }


      
        private System.String  _mz;
        public System.String  mz
        {
            get{return this._mz;}
            set{this._mz =value;}
        }

        private System.String _address;
        public System.String address
        {
             get { return _address; }
            set { _address = value; }
        }

    

        private System.DateTime _birthday;
        public System.DateTime birthday
        {
             get { return _birthday; }
            set { _birthday = value; }
        }

        public System.String sex
        {
             get { return _sex; }
            set { _sex = value; }
        }

        private System.String _notes;
        public System.String notes
        {
             get { return _notes; }
            set { _notes = value; }
        }

        private System.String _tell;
        public System.String tell
        {
             get { return _tell; }
            set { _tell = value; }
        }

        private System.String _name;
        public System.String name
        {
             get { return _name; }
            set { _name = value; }
        }

        private System.String _hfzk;
        public System.String hfzk
        {
             get { return _hfzk; }
            set { _hfzk = value; }
        }
       
     
        private System.String _cardid;
        public System.String cardid
        {
             get { return _cardid; }
            set { _cardid = value; }
        }

      
        private System.Int32 _deptid;
        public System.Int32  deptid
        {
            set{this._deptid =value;}
            get{return this._deptid;}
        }

      
        private department _depart;
        public department depart
        {
            get
            {
                if (this._depart !=null)
                {
                    return this._depart;
                }
                else
                {
                    if (_deptid !=0)
                    {
                        this._depart = (department) (new EntityControl().GetEntity(typeof(department),this._deptid));
                    }
                    else
                    {
                        this._depart =null;
                    }
                    return this._depart;
                }
                  
               
            }

        }
        public string dname
        {
            get
            {
                if (this.depart !=null)
                {
                    return this.depart.dname ;
                }
                else
                {
                    return  String.Empty ;
                }
            }
        }
    }
}


个人感觉和疑问:
  NHibernate对数据库访问是不是有点频繁而且SQL语句不是很简洁( 通过事件探查器得知),这样对于一次要操作数万条记录的系统是不是有点不适合?