Entity Framework底層操做封裝(2)

http://blog.csdn.net/jacky4955/article/details/9138411(http://blog.csdn.net/jacky4955/article/details/9138411)裏面,是對操做底層的封裝,但對於偶來講,其實並不滿意。由於操做仍是顯得太過繁瑣,每一次都得去實現基礎的幾個方法,即便他的代碼不多,這個也是一種浪費,做爲一個攻城師,堅定不作碼農,不去重複一樣的工做。因而針對DAL的數據操做作了一個父類。數據庫

上代碼:ui

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Objects.DataClasses;
using System.Reflection;
namespace NOAS.PublicOpinionMonitor.Access.Common
{
    public class AccessBase<T> where T : class
    {
        private string _strTableName;
        private string _ColumsName;
        private string _PrimaryKey;
        private Type _PrimaryKeyType;

        public AccessBase(string PrimaryKey = "", string strTableName = "", string ColumsName = "")
        {
            Type t = typeof(T);
            if (string.IsNullOrEmpty(strTableName))
            {
                strTableName = t.Name; //GetType(t).ToString();
            }
            _strTableName = strTableName;
            if (string.IsNullOrEmpty(ColumsName))
            {
                _ColumsName = " * ";
            }

            if (string.IsNullOrEmpty(PrimaryKey))
            {
                PropertyInfo[] infos = t.GetProperties();
                _PrimaryKey = getPrimaryKey(infos);
            }
        }





        /// <summary>
        /// 獲取主鍵,此方式只適用於edmx數據表結構
        /// </summary>
        /// <param name="infos"></param>
        /// <returns></returns>
        private string getPrimaryKey(PropertyInfo[] infos)
        {
            string columnName = string.Empty;
            foreach (PropertyInfo propertyInfo in infos)
            {
                object[] customInfos = propertyInfo.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), true);
                if (customInfos == null
               || customInfos.Length == 0)
                    return string.Empty;

                EdmScalarPropertyAttribute limit = customInfos.GetValue(0) as EdmScalarPropertyAttribute;
                if (limit.EntityKeyProperty)
                {
                    _PrimaryKeyType = propertyInfo.PropertyType;
                    return columnName = propertyInfo.Name;
                }
            }
            return columnName;

        }


        /// <summary>
        /// 執行數據庫操做基礎類方法
        /// </summary>
        protected DataCommon Data = new DataCommon();

        /// <summary>
        /// 增長單個實體
        /// </summary>
        /// <param name="t"></param>
        public virtual void addEntity(T t)
        {
            Data.InsertEntity<T>(t);
        }

        public virtual T getSingleEntity(Expression<Func<T, bool>> query)
        {
            return Data.GetSingleEntity<T>(query);
        }


        public virtual T getSingleEntity(object PrimaryKeyId)
        {
            StringBuilder strWhere = new StringBuilder();
            switch (_PrimaryKeyType.Name.ToLower())
            {
                case "int16":
                case "int32":
                case "int64":
                case "int":
                case "decimal":
                case "double":
                case "float":
                case "short":
                    strWhere.AppendFormat(" {0}={1}", _PrimaryKey, PrimaryKeyId);
                    break;
                case "bool":
                case "boolean":
                    if ((bool)PrimaryKeyId)
                    { strWhere.AppendFormat(" {0}=1", _PrimaryKey);}
                    else 
                    { strWhere.AppendFormat(" {0}=0", _PrimaryKey); }

                    break;
                default:
                    strWhere.AppendFormat(" {0}='{1}'", _PrimaryKey, PrimaryKeyId);
                    break;
            }

            return getListByWhere(strWhere.ToString()).FirstOrDefault();
        }

        /// <summary>
        /// 修改單個實體
        /// </summary>
        /// <param name="t"></param>
        public virtual void updateEntity(T t)
        {
            Data.Update<T>(t);
        }


        /// <summary>
        /// 根據條件刪除信息
        /// </summary>
        /// <param name="query">條件</param>
        public virtual void deleteEntity(Expression<Func<T, bool>> query)
        {
            Data.DeleteEntitys<T>(query);
        }


        /// <summary>
        /// 根據條件獲取相關監測信息表
        /// </summary>
        /// <param name="strWhere">Where條件</param>
        /// <returns>數據集合</returns>
        protected virtual List<T> getListByWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select {1} from {0}", _strTableName, _ColumsName);
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.AppendFormat(" where {0}", strWhere);
            }
            return Data.ExecuteQuery<T>(strSql.ToString()).ToList();
        }

        /// <summary>
        /// 獲取最大主鍵
        /// </summary>
        /// <returns></returns>
        protected virtual int? getMaxPrimaryKey()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select max({1}) from {0}", _strTableName, _PrimaryKey);
            return Data.ExecuteQuery<int>(strSql.ToString()).FirstOrDefault();
        }
    }
}
這樣繼承的子類,就自動擁有了增刪改查的基礎方法。
相關文章
相關標籤/搜索