Entity Framework底層操作封裝(2)

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

上代碼:

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

這樣繼承的子類,就自動擁有了增刪改查的基礎方法。
發佈了133 篇原創文章 · 獲贊 55 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章