EF封装类 增加版,增加从缓存中查找数据方法,供参考!

这个类是抽象类,我这里增加了需要子类验证的方法ValidateEntity,方便扩展,若想直接使用该类,可以将该类更改成静态类,里面所有的方法都改成静态方法就可以直接调用了,可能有不足之处,欢迎大家在本文下面评论留言,共同完善,谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using ZBService.Model;
using System.Linq.Expressions;
using System.Web; namespace ZBService
{
public abstract class ServiceBase<T> where T : EntityObject
{
/// <summary>
/// EF上下文对象
/// </summary>
protected mZhaoBiaoEntities zbEntities = new mZhaoBiaoEntities(); /// <summary>
/// 判断是否存在
/// </summary>
/// <param name="whereExpr"></param>
/// <returns></returns>
public bool Exist(Expression<Func<T, bool>> whereExpr)
{
return (this.Count(whereExpr) > 0);
} /// <summary>
/// 获取记录数
/// </summary>
/// <param name="whereExpr"></param>
/// <returns></returns>
public int Count(Expression<Func<T, bool>> whereExpr)
{
return zbEntities.CreateObjectSet<T>().Where(whereExpr).Count();
} /// <summary>
/// 查找实体对象
/// </summary>
/// <param name="whereExpr"></param>
/// <returns></returns>
public T Find(Expression<Func<T, bool>> whereExpr)
{
return zbEntities.CreateObjectSet<T>().Where(whereExpr).FirstOrDefault();
} /// <summary>
/// 从缓存中查找实体对象
/// </summary>
/// <param name="whereExpr"></param>
/// <param name="cacheKey"></param>
/// <param name="expireTime"></param>
/// <returns></returns>
public T FindFromCache(Expression<Func<T, bool>> whereExpr, string cacheKey, DateTime expireTime)
{
T entity;
object obj = HttpRuntime.Cache[cacheKey];
if (obj == null)
{
entity = this.Find(whereExpr);
HttpRuntime.Cache.Insert(cacheKey, entity, null, expireTime, System.Web.Caching.Cache.NoSlidingExpiration);
}
else
{
entity = obj as T;
}
return entity;
} /// <summary>
/// 查找实体对象列表
/// </summary>
/// <param name="whereExpr"></param>
/// <returns></returns>
public IEnumerable<T> FindList<TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TKey>> orderbyExpr, int orderDirection)
{
return this.FindList<T, TKey>(whereExpr, t => t, orderbyExpr, orderDirection);
} /// <summary>
/// 从缓存中查找实体对象列表
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereExpr"></param>
/// <param name="orderbyExpr"></param>
/// <param name="orderDirection"></param>
/// <param name="cacheKey"></param>
/// <param name="expireTime"></param>
/// <returns></returns>
public IEnumerable<T> FindListFromCache<TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TKey>> orderbyExpr, int orderDirection, string cacheKey, DateTime expireTime)
{
return this.FindListFromCache(whereExpr, t => t, orderbyExpr, orderDirection, -1, cacheKey, expireTime);
} /// <summary>
/// 查找实体对象列表
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereExpr"></param>
/// <param name="selectExpr"></param>
/// <param name="orderbyExpr"></param>
/// <param name="orderDirection"></param>
/// <param name="returnCount"></param>
/// <returns></returns>
public IEnumerable<TResult> FindList<TResult, TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TResult>> selectExpr, Expression<Func<TResult, TKey>> orderbyExpr, int orderDirection, int returnCount = -1)
{
var result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
if (result != null && result.Count() > 0)
{
if (orderDirection > 0)
{
result = result.OrderByDescending(orderbyExpr);
}
else
{
result = result.OrderBy(orderbyExpr);
} if (returnCount > 0)
{
result = result.Take(returnCount);
} return result.ToList();
}
return null;
} /// <summary>
/// 从缓存中查找对象列表
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereExpr"></param>
/// <param name="selectExpr"></param>
/// <param name="orderbyExpr"></param>
/// <param name="orderDirection"></param>
/// <param name="returnCount"></param>
/// <param name="cacheKey"></param>
/// <param name="expireTime"></param>
/// <returns></returns>
public IEnumerable<TResult> FindListFromCache<TResult, TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TResult>> selectExpr, Expression<Func<TResult, TKey>> orderbyExpr, int orderDirection,
int returnCount, string cacheKey, DateTime expireTime)
{
IEnumerable<TResult> resultList;
object obj = HttpRuntime.Cache[cacheKey];
if (obj == null)
{
resultList = this.FindList(whereExpr, selectExpr,orderbyExpr, orderDirection,returnCount);
HttpRuntime.Cache.Insert(cacheKey, resultList, null, expireTime, System.Web.Caching.Cache.NoSlidingExpiration);
}
else
{
resultList = obj as IEnumerable<TResult>;
}
return resultList;
} /// <summary>
/// 分页查找实体对象列表
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereExpr"></param>
/// <param name="selectExpr"></param>
/// <param name="orderbyExpr"></param>
/// <param name="orderDirection"></param>
/// <param name="pageSize"></param>
/// <param name="pageNo"></param>
/// <param name="recordCount"></param>
/// <returns></returns>
public IEnumerable<TResult> FindListByPage<TResult, TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TResult>> selectExpr, Expression<Func<TResult, TKey>> orderbyExpr, int orderDirection, int pageSize, int pageNo, out int recordCount)
{
recordCount = 0;
var result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
if (result == null) return null;
recordCount = result.Count(); if (pageNo > recordCount) pageNo = recordCount;
if (pageNo <= 0) pageNo = 1; if (recordCount > 0)
{
if (recordCount > pageSize)
{
if (orderDirection > 0)
{
return result.OrderByDescending(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
}
else
{
return result.OrderBy(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if (orderDirection > 0)
{
return result.OrderByDescending(orderbyExpr).ToList();
}
else
{
return result.OrderBy(orderbyExpr).ToList();
}
} }
return null;
} /// <summary>
/// 从缓存中分页查找实体对象列表
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereExpr"></param>
/// <param name="selectExpr"></param>
/// <param name="orderbyExpr"></param>
/// <param name="orderDirection"></param>
/// <param name="pageSize"></param>
/// <param name="pageNo"></param>
/// <param name="recordCount"></param>
/// <param name="cacheKey"></param>
/// <param name="expireTime"></param>
/// <returns></returns>
public IEnumerable<TResult> FindListByPageFromCache<TResult, TKey>(Expression<Func<T, bool>> whereExpr, Expression<Func<T, TResult>> selectExpr, Expression<Func<TResult, TKey>> orderbyExpr, int orderDirection, int pageSize, int pageNo, out int recordCount,
string cacheKey, DateTime expireTime)
{
recordCount = 0;
IQueryable<TResult> result;
object obj = HttpRuntime.Cache[cacheKey];
if (obj == null)
{
result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
}
else
{
result = obj as IQueryable<TResult>;
} if (result == null) return null;
recordCount = result.Count(); if (pageNo > recordCount) pageNo = recordCount;
if (pageNo <= 0) pageNo = 1; if (recordCount > 0)
{
if (recordCount > pageSize)
{
if (orderDirection > 0)
{
return result.OrderByDescending(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
}
else
{
return result.OrderBy(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if (orderDirection > 0)
{
return result.OrderByDescending(orderbyExpr).ToList();
}
else
{
return result.OrderBy(orderbyExpr).ToList();
}
} }
return null; } /// <summary>
/// 增加实体
/// </summary>
/// <param name="entity"></param>
public virtual void Add(T entity)
{
this.ValidateEntity(entity, ValidateMode.Add);
zbEntities.CreateObjectSet<T>().AddObject(entity);
} /// <summary>
/// 增加实体列表
/// </summary>
/// <param name="entities"></param>
public virtual void AddList(IEnumerable<T> entities)
{
var objSet = zbEntities.CreateObjectSet<T>();
foreach (T entity in entities)
{
this.ValidateEntity(entity, ValidateMode.Add);
objSet.AddObject(entity);
}
} /// <summary>
/// 更新已分离实体,若未分离则不需要执行该方法
/// </summary>
/// <param name="entity"></param>
public virtual void Update(T entity)
{
this.ValidateEntity(entity, ValidateMode.Update);
zbEntities.CreateObjectSet<T>().ApplyCurrentValues(entity);
} /// <summary>
/// 删除实体
/// </summary>
/// <param name="entity"></param>
public virtual void Delete(T entity)
{
this.ValidateEntity(entity, ValidateMode.Delete);
zbEntities.CreateObjectSet<T>().DeleteObject(entity);
} /// <summary>
/// 删除实体
/// </summary>
/// <param name="whereExpr"></param>
public virtual void Delete(Expression<Func<T, bool>> whereExpr)
{
var objSet = zbEntities.CreateObjectSet<T>();
T entity = objSet.Where(whereExpr).Single();
//this.ValidateEntity(entity, ValidateMode.Delete);
objSet.DeleteObject(entity);
} /// <summary>
/// 删除实体列表
/// </summary>
/// <param name="entities"></param>
public virtual void DeleteList(IEnumerable<T> entities)
{
var objSet = zbEntities.CreateObjectSet<T>();
foreach (T entity in entities)
{
//this.ValidateEntity(entity, ValidateMode.Delete);
objSet.DeleteObject(entity);
}
} /// <summary>
/// 提交保存所有变更操作
/// </summary>
public void SubmitSave()
{
zbEntities.SaveChanges();
} /// <summary>
/// 验证
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
protected virtual void ValidateEntity(T entity, ValidateMode mode = ValidateMode.Add)
{ } /// <summary>
/// 验证模式
/// </summary>
protected enum ValidateMode
{
Add = 0,
Update = 1,
Delete = -1
} }
}

之前发表的文章详见:http://www.cnblogs.com/zuowj/p/4259515.html

EF封装类 增加版,增加从缓存中查找数据方法,供参考!的更多相关文章

  1. springboot中如何向redis缓存中存入数据

    package com.hope;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jack ...

  2. 获取apache ignite缓存中的数据行数少于实际行数

    我将ignite项目打包放到linux下,在linux下获取window中存放在oracle数据库中的数据,linux服务器作为ignite的服务端节点,我在本地启动tomact,作为ignite客户 ...

  3. 【EF学习笔记05】----------操作内存中的数据

    SingleOrDefault实验 //SingleOrDefault实验 using (var db = new Entities()) { var classes = new Classes() ...

  4. linux上查找文件存放地点和文件中查找字符串方法

    一.查找文件存放地点 1.locate 语法:locate <filename> locate命令实际是"find -name"的另一种写法,但是查找方式跟find不同 ...

  5. sql server 如何在全库中查找数据在哪个表

    1.查找字段在库中哪个表 如果要查找FName select   a.name,b.name   from   syscolumns a   inner   join   sysobjects   b ...

  6. volley6--CacheDispatcher从缓存中获取数据

    源码: /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, V ...

  7. 数据库default null字段用基本类型映射,改成包装类型后缓存中旧数据反序列化失败

    rt,spring Temp不知道用的什么反序列化,int不能反序列化为Integer,后实验hissing是可以的int->Integer  Integer(不为null)->int均可

  8. MySQL 删除数据库中重复数据方法

    1. 查询需要删除的记录,会保留一条记录. select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RE ...

  9. Python中查找字符串方法的速度比较

随机推荐

  1. Revit如何修改云线批注外观

    Revit云线批注属于注释族类别,有两种方式可以修改云线批注的外观,有两处设置可以修改云线批注的颜色线宽等外观,一个是视图属性"可见性/图形替换"对话框,另一个是菜单"管 ...

  2. KVM 基础使用(一)

    Host OS:CentOS release 6.3 (Final) x64 安装时选择Virtual Host 1.测试KVM有没有启动 运行 lsmod | grep kvm 和 stat /de ...

  3. 特征工程(Feature Enginnering)学习记要

     最近学习特征工程(Feature Enginnering)的相关技术,主要包含两块:特征选取(Feature Selection)和特征抓取(Feature Extraction).这里记录一些要点 ...

  4. 【C++沉思录】句柄1

    1.在[C++沉思录]代理类中,使用了代理类,存在问题: a.代理复制,每次创建一个副本,这个开销有可能很大 b.有些对象不能轻易创建副本,比如文件2.怎么解决这个问题? 使用引用计数句柄,对动态资源 ...

  5. 用Jquery load text文本到網頁遇到的問題

    HTML <div id="divText"></div> Javascript $('#divText').load(fileName ,function ...

  6. Linux64位服务器编译安装MySQL5.6(CentOS6.4)

    首先到MySQL官网下载MySQL最新版(目前是mysql-5.6.12)上传到服务器上,下面说一下详细的安装过程. 安装依赖包,可以在线更新也可以配置本地源(CentOS本地源配置)yum -y i ...

  7. iOS touch事件单击双击区分响应

    如果您的 iPhone 应用里有个 view,既有单击操作又有双击操作.用户双击 view 时,总是先执行一遍单击的操作再执行双击的操作.所以直接判断时就会发现不能直接进入双击操作.下面是区分 tou ...

  8. Mac eclipse找不到source的解决办法

    因为要搞hadoop,最终还是逃不过写java的命运... eclipse里想查具体函数源代码时,如果报错说找不到源: 试试ls -l which java,在这个目录周围看看能不能找到src.zip ...

  9. 带着问题学 Spring MVC 源码: 一、概述

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! 简单就好,生活可以很德国 Q:什么是 Spring MVC ? ※ Spring MVC 是 S ...

  10. CSS基础(一):开篇

    背景 HTML是一种超文本标记语言,用来定义文档的结构和内容,例如标题.段落和列表等等,而文档内容如何渲染.如何展示,这就需要样式来修饰了.CSS正是可以与HTML很好地结合.如果将HTML比作水,那 ...