码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-09 22:18:17      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:des   blog   class   code   ext   c   

这个框架到现在最大的变化马上就要出现了,哪就是对缓存的使用。因为系统经常要去读取数据库数据,但是大家知道,数据库的处理能力是有限的,所以对于一些数据量不大,但是又 需要经常去读取的功能来说,更好的方法就是使用缓存。 上面4的方法是不适用缓存的

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JFrame.AccessCommon;
using System.Data.Objects.DataClasses;
using JFrame.Utility;
using System.Linq.Expressions;
using System.Reflection;

namespace JFrame.Dal
{
    /// <summary>
    /// 使用缓存进行数据更新,缓存暂时只支持lamda表达式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DalBaseDataCache<T> : AccessBase<T> where T : EntityObject
    {
        static DataCacheCommon<T> CacheCommon; //DataCacheCommon<T>.Instance(GetListByWhere);
        public DalBaseDataCache()
            : base(ConnectionString: DataBaseConnectionString.GetConnectionString())
        {
            CacheCommon = DataCacheCommon<T>.Instance(GetListByWhere);
        }

        public DalBaseDataCache(string PrimaryKey = "", string strTableName = "", string QueryColums = "")
            : base(ConnectionString: DataBaseConnectionString.GetConnectionString(), PrimaryKey: PrimaryKey, strTableName: strTableName, QueryColums: QueryColums)
        {
            CacheCommon = DataCacheCommon<T>.Instance(GetListByWhere);
        }



        /// <summary>
        /// 增加单个实体
        /// </summary>
        /// <param name="t"></param>
        public virtual void AddEntity(T t)
        {
            Data.InsertEntity<T>(t);
            CacheCommon.Add(t);
        }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public virtual T GetSingleEntity(Expression<Func<T, bool>> query)
        {
            return CacheCommon.GetDataList(query).FirstOrDefault(); //    Data.GetSingleEntity<T>(query);
        }



        public virtual List<T> GetAllEntityByPage(Expression<Func<T, bool>> query, PagingInfo PageInfo, Func<T, object> orderByDesc)
        {
            return CacheCommon.GetDataList(query, PageInfo, orderByDesc);
        }





        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="express">查询条件</param>
        /// <returns></returns>
        public virtual T TryGetSingleEntity(Expression<Func<T, bool>> query)
        {
            try
            {
                return CacheCommon.GetDataList(query).FirstOrDefault();// Data.GetSingleEntity<T>(query);
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// 修改单个实体
        /// </summary>
        /// <param name="t"></param>
        public virtual void UpdateEntity(T t)
        {

            Type type = typeof(T);
            PropertyInfo[] infos = type.GetProperties();
            PropertyInfo info = infos.Where(p => p.Name == _PrimaryKey).FirstOrDefault();
            object value = null;
            if (info != null)
            {
                value = info.GetValue(t, null);
            }
            else
            {
                return;
            }
            Data.Update<T>(t, _PrimaryKey, value);
            CacheCommon.NextDataUpdate = DateTime.Now;

        }
        /// <summary>
        /// 更新实体,不会从数据库同步
        /// </summary>
        /// <param name="t"></param>
        /// <param name="query"></param>
        public virtual void UpdateEntity(T t, Expression<Func<T, bool>> query) 
        {
            Type type = typeof(T);
            PropertyInfo[] infos = type.GetProperties();
            PropertyInfo info = infos.Where(p => p.Name == _PrimaryKey).FirstOrDefault();
            object value = null;
            if (info != null)
            {
                value = info.GetValue(t, null);
            }
            else
            {
                return;
            }
            Data.Update<T>(t, _PrimaryKey, value);
            CacheCommon.Update(t, query.Compile());
        }

        /// <summary>
        /// 根据条件删除信息
        /// </summary>
        /// <param name="query">条件</param>
        public virtual void Delete(Expression<Func<T, bool>> query)
        {
            Data.DeleteEntitys<T>(query);
            CacheCommon.Delete(query);
        }

        /// <summary>
        /// (缓存中)根据条件获取相关监测信息表
        /// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns>数据集合</returns>
        public virtual List<T> GetListByWhere(Expression<Func<T, bool>> query)
        {

            

            //new System.Linq.Expressions.Expression.BinaryExpressionProxy  (query.Body)
            //(new System.Linq.Expressions.Expression.BinaryExpressionProxy((new System.Linq.Expressions.Expression.LambdaExpressionProxy(query)).Body)).DebugView


            return CacheCommon.GetDataList(query);
        }

        /// <summary>
        /// 根据条件获取相关监测信息表
        /// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns>数据集合</returns>
        public virtual List<T> GetALLCacheList()
        {
            return CacheCommon.GetDataList();
        }
       
    }
}


 

Entity Framework底层操作封装V2版本(5),布布扣,bubuko.com

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

标签:des   blog   class   code   ext   c   

原文地址:http://blog.csdn.net/jacky4955/article/details/25413509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!