NHibernate中数据操作封装类定义

来源:互联网 发布:软件发票备注栏规定 编辑:程序博客网 时间:2024/06/05 22:59

NHibernate中数据操作封装类定义

/// <summary>    /// 定义:数据操作方法接口    /// vp:hsg    /// create date:2013-08-29    /// </summary>    /// <typeparam name="T"></typeparam>    public interface  IDataMethod<T>    {        ISessionFactory dbsf { get; }        //        bool Insert(T entity);        bool Update(T entity);        bool Delete(T entity);        //        bool DeleteList(IList<T> entityList);        bool DeleteList(IList<T> entityList, ISession s);        bool Delete(object id);        //        bool InsertOrUpdate(T entity);        //        T GetRecordByID(object id);        IList<T> GetRecords(string whereStr);        IList<T> GetRecordsALL();    }    /// <summary>    /// 默认的适配器类     /// 定义:抽象的数据操作方法    /// vp:hsg,wangjingbao    /// create date:2013-08-29    /// </summary>    /// <typeparam name="T"></typeparam>    [Serializable()]    public abstract class AbsDataMethod<T>:IDataMethod<T>    {        protected T _object;        protected string _modelName = string.Empty;        //       #region IDataMethod<T> 成员        public ISessionFactory dbsf        {            get { return DBSessionFactory.SF; }        }        public virtual  bool Insert(T entity)        {            bool rbc=false;            using (ISession s = this.dbsf.OpenSession())            {                s.Save(entity);                s.Flush();                rbc=true;            }            return rbc;        }        public virtual  bool Update(T entity)        {            bool rbc = false;            using (ISession s = this.dbsf.OpenSession())            {                s.Update(entity);                s.Flush();                rbc = true;            }            return rbc;        }        public virtual  bool Delete(T entity)        {            bool rbc = false;            using (ISession s = this.dbsf.OpenSession())            {                s.Delete(entity);                s.Flush();                rbc = true;            }            return rbc;        }        public virtual  bool DeleteList(IList<T> entityList)        {            bool rbc = false;            using (ISession s = this.dbsf.OpenSession())            {                foreach (T t in entityList)                {                    s.Delete(t);                }                s.Flush();                rbc = true;            }            return rbc;        }        public virtual  bool DeleteList(IList<T> entityList, ISession s)        {            bool rbc = false;            foreach (T t in entityList)            {                s.Delete(t);            }            s.Flush();            rbc = true;            return rbc;        }        public virtual  bool Delete(object id)        {            bool rbc = false;            T t = this.GetRecordByID(id);            if (t != null)            {                this.Delete(t);                rbc = true;            }            return rbc;        }        public virtual  bool InsertOrUpdate(T entity)        {            bool rbc = false;            using (ISession s = this.dbsf.OpenSession())            {                s.SaveOrUpdate(entity);                s.Flush();                rbc = true;            }            return rbc;        }        public virtual  T GetRecordByID(object id)        {            using (ISession s = this.dbsf.OpenSession())            {                return s.Get<T>(id);            }            }                //        protected string m_tableName="";        public virtual  IList<T> GetRecords(string whereStr)        {            using (ISession s = this.dbsf.OpenSession())            {                string sql = string.Format("select * from {0}   {1}", m_tableName, whereStr);                ISQLQuery sq = s.CreateSQLQuery(sql);                return sq.List<T>();                            }         }                public virtual IList<T> GetRecordsALL()        {            return this.GetRecords("");        }        //---------------------                //        public T Get(object id)        {            using (ISession s = this.dbsf.OpenSession())            {                _object = s.Get<T>(id);                return _object;            }        }        public T Load(object id)        {            using (ISession s = this.dbsf.OpenSession())            {                return s.Load<T>(id);            }        }        public abstract object GetKey(T entity);        public abstract IList<T> LoadALL();        public abstract IList<T> LoadList(object value);        #endregion        //    }    /// <summary>    /// 定义一个父类接口    /// </summary>    /// <typeparam name="C"></typeparam>    public interface INHParent<C>    {        void AddChild(C child);        IList<C> LoadChildren();    }    /// <summary>    /// 定义一个子类接口    /// </summary>    /// <typeparam name="P"></typeparam>    public interface INHChild<P>    {        P Parent { get; }    }


 

0 0
原创粉丝点击