Entity Framework底层操作封装V2版本(6)

这个方法是数据缓存操作的基本类:它作用是,在加载的时候,从数据库把数据读取出来放在缓存里面,同时每隔一段时间,从数据库再读取一次,以保证数据的真实性。

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. using System.Runtime.Serialization;  
  7. using System.Runtime.Serialization.Formatters.Binary;  
  8. using System.Reflection;  
  9. using System.Linq.Expressions;  
  10. using System.Threading;  
  11. using JFrame.Utility;  
  12. using JFrame.Entity;  
  13. using System.Data.Objects;  
  14.   
  15. namespace JFrame.Dal  
  16. {  
  17.     public class DataCacheCommon<T> where T : class  
  18.     {  
  19.         private List<T> DataList = new List<T>();  
  20.         private static object ulock = new object();  
  21.         private DateTime nextDataUpdate = DateTime.Now;  
  22.         /// <summary>  
  23.         /// 每次从数据库更新的间隔为60分钟  
  24.         /// </summary>  
  25.         private int updateInterval = 60;  
  26.         public delegate List<T> UpdateDataFunction();  
  27.   
  28.   
  29.         /// <summary>  
  30.         /// 实例化对象时,必须提供更新数据的方法  
  31.         /// </summary>  
  32.         /// <param name="?"></param>  
  33.         private DataCacheCommon(UpdateDataFunction Function)  
  34.         {  
  35.             Type type = typeof(T);  
  36.   
  37.   
  38.             new DalBaseData<SysRunLogs>().AddEntity(new SysRunLogs()  
  39.             {  
  40.                 AddDateTime = DateTime.Now,  
  41.                 LoginInfo = "启动实例化 " + type.Name  
  42.             });  
  43.   
  44.             OnTimeUpdateDataList(Function);  
  45.         }  
  46.         private DataCacheCommon()  
  47.         { }  
  48.   
  49.   
  50.         private static DataCacheCommon<T> instance = null;  
  51.         private static readonly object padlock = new object();  
  52.         /// <summary>  
  53.         /// 单例模式创建  
  54.         /// </summary>  
  55.         /// <param name="updateTime"></param>  
  56.         /// <returns></returns>  
  57.         public static DataCacheCommon<T> Instance(UpdateDataFunction updateTime)  
  58.         {  
  59.             if (instance == null)  
  60.             {  
  61.                 lock (padlock)  
  62.                 {  
  63.                     if (instance == null)  
  64.                     {  
  65.                         instance = new DataCacheCommon<T>(updateTime);  
  66.                     }  
  67.                 }  
  68.             }  
  69.             return instance;  
  70.         }  
  71.   
  72.   
  73.   
  74.   
  75.   
  76.   
  77.         /// <summary>  
  78.         /// 操作类型  
  79.         /// </summary>  
  80.         private enum OperType  
  81.         {  
  82.             Select = 1,  
  83.             Update = 2,  
  84.             Delete = 3,  
  85.             Add = 4,  
  86.             Set=5  
  87.         }  
  88.   
  89.   
  90.         /// <summary>  
  91.         /// 更新实体  
  92.         /// </summary>  
  93.         /// <param name="model">新的实体对象</param>  
  94.         /// <param name="query">原有的实体对象</param>  
  95.         public void Update(T model, Func<T, bool> query)  
  96.         {  
  97.             DataOper(OperType.Update, model, query);  
  98.         }  
  99.   
  100.   
  101.   
  102.         /// <summary>  
  103.         /// 增加实体  
  104.         /// </summary>  
  105.         /// <param name="model"></param>  
  106.         public void Add(T model)  
  107.         {  
  108.             DataOper(OperType.Add, model, null);  
  109.         }  
  110.         /// <summary>  
  111.         /// 根据条件删除数据方法  
  112.         /// </summary>  
  113.         /// <param name="query"></param>  
  114.         public void Delete(Func<T, bool> query)  
  115.         {  
  116.             DataOper(OperType.Delete, null, query);  
  117.         }  
  118.         /// <summary>  
  119.         /// 根据尸体数据删除方法  
  120.         /// </summary>  
  121.         /// <param name="Entity"></param>  
  122.         public void Delete(T Entity)  
  123.         {  
  124.             DataOper(OperType.Delete, Entity, null);  
  125.         }  
  126.   
  127.         /// <summary>  
  128.         /// 根据尸体数据删除方法  
  129.         /// </summary>  
  130.         /// <param name="Entity"></param>  
  131.         public void Delete(Expression<Func<T, bool>> query)  
  132.         {  
  133.             DataOper(OperType.Delete, null, query.Compile());  
  134.         }  
  135.   
  136.         public List<T> GetDataList(Func<T, bool> query)  
  137.         {  
  138.             return DataOper(OperType.Select, null, query);  
  139.         }  
  140.   
  141.         public List<T> GetDataList(Expression<Func<T, bool>> query)  
  142.         {  
  143.             return DataOper(OperType.Select, null, query.Compile());  
  144.         }  
  145.         public List<T> GetDataList()  
  146.         {  
  147.             return DataOper(OperType.Select, null, null);  
  148.         }  
  149.         public List<T> GetDataList(Expression<Func<T, bool>> query, PagingInfo PageInfo, Func<T, object> orderByDesc)  
  150.         {  
  151.             List<T> DataWhere = DataOper(OperType.Select, null, query.Compile());  
  152.             PageInfo.TotalRecord = DataWhere.Count();  
  153.             PageInfo.TotalPage = PageInfo.TotalRecord / PageInfo.PageSize + 1;  
  154.   
  155.             return (from t in DataWhere  
  156.                     select t).OrderByDescending(orderByDesc).Skip(PageInfo.PageSize * (PageInfo.PageIndex - 1)).Take(PageInfo.PageSize).ToList();  
  157.         }  
  158.   
  159.         /// <summary>  
  160.         /// 下一次更新时间  
  161.         /// </summary>  
  162.         public DateTime NextDataUpdate  
  163.         {  
  164.             set  
  165.             {  
  166.                 nextDataUpdate = value;  
  167.             }  
  168.         }  
  169.   
  170.         /// <summary>  
  171.         /// 每次从数据更新时间,默认为60分钟更新一次  
  172.         /// </summary>  
  173.         public int UpdateInterval  
  174.         {  
  175.             get  
  176.             {  
  177.                 return updateInterval;  
  178.             }  
  179.             set  
  180.             {  
  181.                 updateInterval = value;  
  182.             }  
  183.         }  
  184.   
  185.         /// <summary>  
  186.         /// 调用更新数据的线程方法  
  187.         /// </summary>  
  188.         /// <param name="Function"></param>  
  189.         private void OnTimeUpdateDataList(UpdateDataFunction Function)  
  190.         {  
  191.             ParameterizedThreadStart ParStart = new ParameterizedThreadStart(UpdateData);  
  192.             Thread myThread = new Thread(ParStart);  
  193.             object o = Function;  
  194.             myThread.Start(o);  
  195.         }  
  196.         /// <summary>  
  197.         /// 数据定时更新的方法  
  198.         /// </summary>  
  199.         private void UpdateData(object Function)  
  200.         {  
  201.             while (true)  
  202.             {  
  203.                 if (nextDataUpdate <= DateTime.Now)  
  204.                 {  
  205.                     List<T> NewData = (List<T>)((UpdateDataFunction)Function)();  
  206.                     DataOper(OperType.Set, null, null, NewData);  
  207.   
  208.                     Type type = typeof(T);  
  209.                     new DalBaseData<SysRunLogs>().AddEntity(new SysRunLogs()  
  210.                     {  
  211.                         AddDateTime = DateTime.Now,  
  212.                         LoginInfo = "更新缓存 " + type.Name + "   数据 " + DataList.Count() + "  条!"  
  213.                     });  
  214.   
  215.   
  216.                     nextDataUpdate = nextDataUpdate.AddMinutes(UpdateInterval);//  UpdateInterval*60*1000;  
  217.                 }  
  218.                 Thread.Sleep(1000);  
  219.             }  
  220.         }  
  221.   
  222.         /// <summary>  
  223.         /// 公共列表操作类  
  224.         /// </summary>  
  225.         /// <param name="OperType">操作类型(select update delete add/insert)</param>  
  226.         /// <param name="model">实体对象 修改和增加 删除的时候必须传入</param>  
  227.         /// <param name="query">当为select时候的查询条件</param>  
  228.         /// <returns>当为select则返回 list列表 否则不进行返回</returns>  
  229.         private List<T> DataOper(OperType operType, T model, Func<T, bool> query,List<T> SetList=null)  
  230.         {  
  231.   
  232.             if (DataList == null)  
  233.             {  
  234.                 DataList = new List<T>();  
  235.             }  
  236.   
  237.             if (operType == OperType.Select)  
  238.             {  
  239.                 if (query == null)  
  240.                 {  
  241.                     return DataList;  
  242.                 }  
  243.                 List<T> SelectList = new List<T>();  
  244.                 foreach (var item in DataList.Where(query).ToList())  
  245.                 {  
  246.                     SelectList.Add(Copy(item));  
  247.                 }  
  248.                 return SelectList;  
  249.             }  
  250.   
  251.             lock (ulock)  
  252.             {  
  253.                 switch (operType)  
  254.                 {  
  255.                     case OperType.Update:  
  256.                         T tmodel = DataList.Where(query).FirstOrDefault();  
  257.                         if (tmodel != null)  
  258.                         {  
  259.                             tmodel = Copy(model);  
  260.                         }  
  261.                         return null;  
  262.                     case OperType.Delete:  
  263.                         if (query != null)  
  264.                         {  
  265.                             List<T> DeleteList = DataList.Where(query).ToList();  
  266.                             if (DeleteList != null)  
  267.                             {  
  268.                                 foreach (var item in DeleteList)  
  269.                                 {  
  270.                                     DataList.Remove(item);  
  271.                                 }  
  272.                             }  
  273.                         }  
  274.                         else if (model != null)  
  275.                         {  
  276.                             DataList.Remove(model);  
  277.                         }  
  278.                         return null;  
  279.                     case OperType.Add:  
  280.                         DataList.Add(model);  
  281.                         return null;  
  282.                     case OperType.Set:  
  283.                         DataList = new List<T>();  
  284.                         foreach (var item in SetList)  
  285.                         {  
  286.                             DataList.Add(Copy(item));  
  287.                         }  
  288.                         return null;  
  289.                 }  
  290.   
  291.                 return null;  
  292.   
  293.             }  
  294.         }  
  295.   
  296.   
  297.         /// <summary>  
  298.         /// 数据复制  
  299.         /// </summary>  
  300.         /// <typeparam name="T"></typeparam>  
  301.         /// <param name="entity"></param>  
  302.         /// <returns></returns>  
  303.         public T Copy<T>(T entity) where T : class  
  304.         {  
  305.             Type type = typeof(T);  
  306.             T newEntity = System.Activator.CreateInstance<T>(); ;  
  307.   
  308.   
  309.   
  310.             object propertyValue = null;  
  311.             PropertyInfo[] properties1 = newEntity.GetType().GetProperties();  
  312.             foreach (PropertyInfo property in properties1)  
  313.             {  
  314.                 propertyValue = null;  
  315.                 if (null != property.GetSetMethod())  
  316.                 {  
  317.                     PropertyInfo entityProperty =  
  318.                           entity.GetType().GetProperty(property.Name);  
  319.                     if (entityProperty.PropertyType.BaseType ==  
  320.                         Type.GetType("System.ValueType") ||  
  321.                         entityProperty.PropertyType ==  
  322.                         Type.GetType("System.String"))  
  323.   
  324.                         propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);  
  325.                     if (propertyValue == null)  
  326.                     {  
  327.                         propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);  
  328.                     }  
  329.                     if (null != propertyValue)  
  330.                     {  
  331.                         try  
  332.                         {  
  333.                             string Name = property.Name;// "Reference";  
  334.                             if (Name.IndexOf("Reference") < 0)  
  335.                             {  
  336.                                 property.SetValue(newEntity, propertyValue, null);  
  337.                             }  
  338.                         }  
  339.                         catch (Exception ex) { }  
  340.                     }  
  341.                 }  
  342.             }  
  343.             return newEntity;  
  344.         }  
  345.     }  
  346. }  

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

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