Entity Framework底层操作封装(2)

http://blog.csdn.net/jacky4955/article/details/9138411(http://blog.csdn.net/jacky4955/article/details/9138411)里面,是对操作底层的封装,但对于偶来说,其实并不满意。因为操作还是显得太过繁琐,每一次都得去实现基础的几个方法,即使他的代码很少,这个也是一种浪费,作为一个攻城师,坚决不做码农,不去重复同样的工作。于是针对DAL的数据操作做了一个父类。

上代码:

 

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Linq.Expressions;  
  6. using System.Data.Objects.DataClasses;  
  7. using System.Reflection;  
  8. namespace NOAS.PublicOpinionMonitor.Access.Common  
  9. {  
  10.     public class AccessBase<T> where T : class  
  11.     {  
  12.         private string _strTableName;  
  13.         private string _ColumsName;  
  14.         private string _PrimaryKey;  
  15.         private Type _PrimaryKeyType;  
  16.   
  17.         public AccessBase(string PrimaryKey = "", string strTableName = "", string ColumsName = "")  
  18.         {  
  19.             Type t = typeof(T);  
  20.             if (string.IsNullOrEmpty(strTableName))  
  21.             {  
  22.                 strTableName = t.Name; //GetType(t).ToString();  
  23.             }  
  24.             _strTableName = strTableName;  
  25.             if (string.IsNullOrEmpty(ColumsName))  
  26.             {  
  27.                 _ColumsName = " * ";  
  28.             }  
  29.   
  30.             if (string.IsNullOrEmpty(PrimaryKey))  
  31.             {  
  32.                 PropertyInfo[] infos = t.GetProperties();  
  33.                 _PrimaryKey = getPrimaryKey(infos);  
  34.             }  
  35.         }  
  36.   
  37.   
  38.   
  39.   
  40.   
  41.         /// <summary>  
  42.         /// 获取主键,此方式只适用于edmx数据表结构  
  43.         /// </summary>  
  44.         /// <param name="infos"></param>  
  45.         /// <returns></returns>  
  46.         private string getPrimaryKey(PropertyInfo[] infos)  
  47.         {  
  48.             string columnName = string.Empty;  
  49.             foreach (PropertyInfo propertyInfo in infos)  
  50.             {  
  51.                 object[] customInfos = propertyInfo.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), true);  
  52.                 if (customInfos == null  
  53.                || customInfos.Length == 0)  
  54.                     return string.Empty;  
  55.   
  56.                 EdmScalarPropertyAttribute limit = customInfos.GetValue(0) as EdmScalarPropertyAttribute;  
  57.                 if (limit.EntityKeyProperty)  
  58.                 {  
  59.                     _PrimaryKeyType = propertyInfo.PropertyType;  
  60.                     return columnName = propertyInfo.Name;  
  61.                 }  
  62.             }  
  63.             return columnName;  
  64.   
  65.         }  
  66.   
  67.   
  68.         /// <summary>  
  69.         /// 执行数据库操作基础类方法  
  70.         /// </summary>  
  71.         protected DataCommon Data = new DataCommon();  
  72.   
  73.         /// <summary>  
  74.         /// 增加单个实体  
  75.         /// </summary>  
  76.         /// <param name="t"></param>  
  77.         public virtual void addEntity(T t)  
  78.         {  
  79.             Data.InsertEntity<T>(t);  
  80.         }  
  81.   
  82.         public virtual T getSingleEntity(Expression<Func<T, bool>> query)  
  83.         {  
  84.             return Data.GetSingleEntity<T>(query);  
  85.         }  
  86.   
  87.   
  88.         public virtual T getSingleEntity(object PrimaryKeyId)  
  89.         {  
  90.             StringBuilder strWhere = new StringBuilder();  
  91.             switch (_PrimaryKeyType.Name.ToLower())  
  92.             {  
  93.                 case "int16":  
  94.                 case "int32":  
  95.                 case "int64":  
  96.                 case "int":  
  97.                 case "decimal":  
  98.                 case "double":  
  99.                 case "float":  
  100.                 case "short":  
  101.                     strWhere.AppendFormat(" {0}={1}", _PrimaryKey, PrimaryKeyId);  
  102.                     break;  
  103.                 case "bool":  
  104.                 case "boolean":  
  105.                     if ((bool)PrimaryKeyId)  
  106.                     { strWhere.AppendFormat(" {0}=1", _PrimaryKey);}  
  107.                     else   
  108.                     { strWhere.AppendFormat(" {0}=0", _PrimaryKey); }  
  109.   
  110.                     break;  
  111.                 default:  
  112.                     strWhere.AppendFormat(" {0}='{1}'", _PrimaryKey, PrimaryKeyId);  
  113.                     break;  
  114.             }  
  115.   
  116.             return getListByWhere(strWhere.ToString()).FirstOrDefault();  
  117.         }  
  118.   
  119.         /// <summary>  
  120.         /// 修改单个实体  
  121.         /// </summary>  
  122.         /// <param name="t"></param>  
  123.         public virtual void updateEntity(T t)  
  124.         {  
  125.             Data.Update<T>(t);  
  126.         }  
  127.   
  128.   
  129.         /// <summary>  
  130.         /// 根据条件删除信息  
  131.         /// </summary>  
  132.         /// <param name="query">条件</param>  
  133.         public virtual void deleteEntity(Expression<Func<T, bool>> query)  
  134.         {  
  135.             Data.DeleteEntitys<T>(query);  
  136.         }  
  137.   
  138.   
  139.         /// <summary>  
  140.         /// 根据条件获取相关监测信息表  
  141.         /// </summary>  
  142.         /// <param name="strWhere">Where条件</param>  
  143.         /// <returns>数据集合</returns>  
  144.         protected virtual List<T> getListByWhere(string strWhere)  
  145.         {  
  146.             StringBuilder strSql = new StringBuilder();  
  147.             strSql.AppendFormat("select {1} from {0}", _strTableName, _ColumsName);  
  148.             if (!string.IsNullOrEmpty(strWhere))  
  149.             {  
  150.                 strSql.AppendFormat(" where {0}", strWhere);  
  151.             }  
  152.             return Data.ExecuteQuery<T>(strSql.ToString()).ToList();  
  153.         }  
  154.   
  155.         /// <summary>  
  156.         /// 获取最大主键  
  157.         /// </summary>  
  158.         /// <returns></returns>  
  159.         protected virtual int? getMaxPrimaryKey()  
  160.         {  
  161.             StringBuilder strSql = new StringBuilder();  
  162.             strSql.AppendFormat("select max({1}) from {0}", _strTableName, _PrimaryKey);  
  163.             return Data.ExecuteQuery<int>(strSql.ToString()).FirstOrDefault();  
  164.         }  
  165.     }  
  166. }  


这样继承的子类,就自动拥有了增删改查的基础方法。

http://blog.csdn.net/jacky4955/article/details/9138549

posted @ 2014-12-30 15:16  关中秦人  阅读(101)  评论(0编辑  收藏  举报