ASP.NET MVC5网站开发之实现数据存储层功能(三)
数据存储层在项目ninesky.datalibrary中实现,整个项目只有一个类repository。
repository中实现增删改查询等方法供业务逻辑层调用,主要功能如下图:
具体步骤
一、添加实体框架的引用
1、打开解决方案,选择项目ninesky.datalibrary,在引用上右键,选择管理nuget程序包。
在nuget包管理器中的浏览标签中点击entityframework,点击右侧栏的安装按钮。
在搜索框输入entityframework.zh-hans,安装假体中文资源包。
二、实现数据仓储类
打开解决方案,选择项目ninesky.datalibrary,将class1.cs重命名为repository.cs,在文档头添加using system.data.entity;将类名重命名为public class repository。改为public class repository<t> where t :class
1、dbcontext属性
在repository类中添加如下代码 public dbcontext dbcontext { get; set; }
2、构造函数
为类中添加够高函数,可以直接传递dbcontex。
public repository() { } public repository(dbcontext dbcontext) { dbcontext = dbcontext; }
3、查找实体方法find
find有一个重载。两个方法分别可以根据id和根据lamdba表达式查找实体。
/// <summary> /// 查找实体 /// </summary> /// <param name="id">实体主键值</param> /// <returns></returns> public t find(int id) { return dbcontext.set<t>().find(id); } /// <summary> /// 查找实体 /// </summary> /// <param name="where">查询lambda表达式</param> /// <returns></returns> public t find(expression<func<t,bool>> where) { return dbcontext.set<t>().singleordefault(where); }
4、查找实体列表方法findlist
根据需要findlist进行多次重载
/// <summary> /// 查找实体列表 /// </summary> /// <returns></returns> public iqueryable<t> findlist() { return dbcontext.set<t>(); } /// <summary> /// 查找实体列表 /// </summary> /// <typeparam name="tkey">排序建类型</typeparam> /// <param name="order">排序表达式</param> /// <param name="asc">是否正序</param> /// <returns></returns> public iqueryable<t> findlist<tkey>(expression<func<t, tkey>> order, bool asc) { return asc ? dbcontext.set<t>().orderby(order) : dbcontext.set<t>().orderbydescending(order); } /// <summary> /// 查找实体列表 /// </summary> /// <typeparam name="tkey">排序键类型</typeparam> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> /// <param name="number">获取的记录数量</param> /// <returns></returns> public iqueryable<t> findlist<tkey>(expression<func<t, tkey>> order, bool asc,int number) { return asc ? dbcontext.set<t>().orderby(order).take(number) : dbcontext.set<t>().orderbydescending(order).take(number); } /// <summary> /// 查找实体列表 /// </summary> /// <param name="where">查询lambda表达式</param> /// <returns></returns> public iqueryable<t> findlist(expression<func<t, bool>> where) { return dbcontext.set<t>().where(where); } /// <summary> /// 查找实体列表 /// </summary> /// <param name="where">查询lambda表达式</param> /// <param name="number">获取的记录数量</param> /// <returns></returns> public iqueryable<t> findlist(expression<func<t, bool>> where, int number) { return dbcontext.set<t>().where(where).take(number); } /// <summary> /// 查找实体列表 /// </summary> /// <typeparam name="tkey">排序键类型</typeparam> /// <param name="where">查询lambda表达式</param> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> /// <returns></returns> public iqueryable<t> findlist<tkey>(expression<func<t, bool>> where, expression<func<t, tkey>> order, bool asc) { return asc ? dbcontext.set<t>().where(where).orderby(order) : dbcontext.set<t>().where(where).orderbydescending(order); } /// <summary> /// 查找实体列表 /// </summary> /// <typeparam name="tkey">排序键类型</typeparam> /// <param name="where">查询lambda表达式</param> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> /// <param name="number">获取的记录数量</param> /// <returns></returns> public iqueryable<t> findlist<tkey>(expression<func<t, bool>> where, expression<func<t, tkey>> order, bool asc, int number) { return asc ? dbcontext.set<t>().where(where).orderby(order).take(number) : dbcontext.set<t>().where(where).orderbydescending(order).take(number); }
5、查找实体分页列表方法findpagelist
根据需要findpagelist进行多次重载
/// <summary> /// 查找分页列表 /// </summary> /// <param name="pagesize">每页记录数。必须大于1</param> /// <param name="pageindex">页码。首页从1开始,页码必须大于1</param> /// <param name="totalnumber">总记录数</param> /// <returns></returns> public iqueryable<t> findpagelist(int pagesize, int pageindex, out int totalnumber) { if (pageindex < 1) pageindex = 1; if (pagesize < 1) pagesize = 10; iqueryable<t> _list = dbcontext.set<t>(); totalnumber = _list.count(); return _list.skip((pageindex - 1) * pageindex).take(pagesize); } /// <summary> /// 查找分页列表 /// </summary> /// <param name="pagesize">每页记录数。必须大于1</param> /// <param name="pageindex">页码。首页从1开始,页码必须大于1</param> /// <param name="totalnumber">总记录数</param> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> /// <returns></returns> public iqueryable<t> findpagelist<tkey>(int pagesize, int pageindex, out int totalnumber, expression<func<t, tkey>> order, bool asc) { if (pageindex < 1) pageindex = 1; if (pagesize < 1) pagesize = 10; iqueryable<t> _list = dbcontext.set<t>(); _list = asc ? _list.orderby(order) : _list.orderbydescending(order); totalnumber = _list.count(); return _list.skip((pageindex - 1) * pageindex).take(pagesize); } /// <summary> /// 查找分页列表 /// </summary> /// <param name="pagesize">每页记录数。必须大于1</param> /// <param name="pageindex">页码。首页从1开始,页码必须大于1</param> /// <param name="totalnumber">总记录数</param> /// <param name="where">查询表达式</param> public iqueryable<t> findpagelist(int pagesize, int pageindex, out int totalnumber, expression<func<t, bool>> where) { if (pageindex < 1) pageindex = 1; if (pagesize < 1) pagesize = 10; iqueryable<t> _list = dbcontext.set<t>().where(where); totalnumber = _list.count(); return _list.skip((pageindex - 1) * pageindex).take(pagesize); } /// <summary> /// 查找分页列表 /// </summary> /// <param name="pagesize">每页记录数。必须大于1</param> /// <param name="pageindex">页码。首页从1开始,页码必须大于1</param> /// <param name="totalnumber">总记录数</param> /// <param name="where">查询表达式</param> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> public iqueryable<t> findpagelist<tkey>(int pagesize, int pageindex, out int totalnumber, expression<func<t, bool>> where, expression<func<t, tkey>> order, bool asc) { if (pageindex < 1) pageindex = 1; if (pagesize < 1) pagesize = 10; iqueryable<t> _list = dbcontext.set<t>().where(where); _list = asc ? _list.orderby(order) : _list.orderbydescending(order); totalnumber = _list.count(); return _list.skip((pageindex - 1) * pageindex).take(pagesize); }
6、添加实体方法add
add 方法有一个重载,重载方法可以选择是否立即保存
/// <summary> /// 添加实体 /// </summary> /// <param name="entity">实体</param> /// <returns>受影响的对象的数目</returns> public int add(t entity) { return add(entity, true); } /// <summary> /// 添加实体 /// </summary> /// <param name="entity">实体</param> /// <param name="issave">是否立即保存</param> /// <returns>在“issave”为true时返回受影响的对象的数目,为false时直接返回0</returns> public int add(t entity,bool issave) { dbcontext.set<t>().add(entity); return issave ? dbcontext.savechanges() : 0; }
7、更新实体方法update
updae 方法有一个重载,重载方法可以选择是否立即保存
#region update /// <summary> /// 更新实体【立即保存】 /// </summary> /// <param name="entity">实体</param> /// <returns>在“issave”为true时返回受影响的对象的数目,为false时直接返回0</returns> public int update(t entity) { return update(entity, true); } /// <summary> /// 更新实体 /// </summary> /// <param name="entity">实体</param> /// <param name="issave">是否立即保存</param> /// <returns>在“issave”为true时返回受影响的对象的数目,为false时直接返回0</returns> public int update(t entity, bool issave) { dbcontext.set<t>().attach(entity); dbcontext.entry<t>(entity).state = entitystate.modified; return issave ? dbcontext.savechanges() : 0; }
8、删除实体方法delete
delete 方法有两个重载,一个是可以选择否立即保存,另一个是批量删除
/// <summary> /// 删除实体【立即保存】 /// </summary> /// <param name="entity">实体</param> /// <returns>受影响的对象的数目</returns> public int delete(t entity) { return delete(entity, true); } /// <summary> /// 删除实体 /// </summary> /// <param name="entity">实体</param> /// <param name="issave">是否立即保存</param> /// <returns>在“issave”为true时返回受影响的对象的数目,为false时直接返回0</returns> public int delete(t entity,bool issave) { dbcontext.set<t>().remove(entity); return issave ? dbcontext.savechanges() : 0; } /// <summary> /// 批量删除实体 /// </summary> /// <param name="entities">实体集合</param> /// <returns>受影响的对象的数目</returns> public int delete(ienumerable<t> entities) { dbcontext.set<t>().removerange(entities); return dbcontext.savechanges(); }
9、统计记录数方法count
count 方法有一个重载,可以根据表达式进行统计
/// <summary> /// 记录数 /// </summary> /// <returns></returns> public int count() { return dbcontext.set<t>().count(); } /// <summary> /// 记录数 /// </summary> /// <param name="predicate">表达式</param> /// <returns></returns> public int count(expression<func<t, bool>> predicate) { return dbcontext.set<t>().count(predicate); }
10、是否存在
/// <summary> /// 记录是否存在 /// </summary> /// <param name="predicate">表达式</param> /// <returns></returns> public bool iscontains(expression<func<t, bool>> predicate) { return count(predicate) > 0; }
11、保存到数据库
/// <summary> /// 保存数据【在add、upate、delete未立即保存的情况下使用】 /// </summary> /// <returns>受影响的记录数</returns> public int save() { return dbcontext.savechanges(); }