ASP.NET MVC5网站开发之业务逻辑层的架构和基本功(2)

using System.Collections.Generic; namespace Ninesky.Core.Types { public class Paging<T> { /// <summary> /// 当前页。从1计数 /// </summary> public int PageIndex { get; set; } /// <summary> /// 每页记录数。默认20 /// </summary> public int PageSize { get; set; } /// <summary> /// 总记录数 /// </summary> public int TotalNumber;/// <summary> /// 当前页记录列表 /// </summary> public List<T> Items { get; set; } public Paging() { PageIndex = 1; PageSize = 20; } } }

6、BaseManager类

BaseManager类是所有管理类的基类,此类包含了管理类的常用方法。

ASP.NET MVC5网站开发之业务逻辑层的架构和基本功

将Ninesky.Core项目的Class1.cs重命名为BaseManager.cs

引入命名空间System.Data.Entity和Ninesky.Core.Types,实现共有方法。

using Ninesky.Core.Types; using Ninesky.DataLibrary; using System.Data.Entity; using System.Linq; namespace Ninesky.Core { /// <summary> /// 管理类的基类 /// </summary> /// <typeparam>模型类</typeparam> public abstract class BaseManager<T> where T :class { /// <summary> /// 数据仓储类 /// </summary> protected Repository<T> Repository; /// <summary> /// 默认构造函数 /// </summary> public BaseManager():this(ContextFactory.CurrentContext()) { } /// <summary> /// 构造函数 /// </summary> /// <param>数据上下文</param> public BaseManager(DbContext dbContext){ Repository = new Repository<T>(dbContext); } /// <summary> /// 添加 /// </summary> /// <param>实体数据</param> /// <returns>成功时属性【Data】为添加后的数据实体</returns> public virtual Response Add(T entity) { Response _response = new Response(); if(Repository.Add(entity)>0) { _response.Code = 1; _response.Message = "添加数据成功!"; _response.Data = entity; } else { _response.Code = 0; _response.Message = "添加数据失败!"; } return _response; } /// <summary> /// 更新 /// </summary> /// <param>实体数据</param> /// <returns>成功时属性【Data】为更新后的数据实体</returns> public virtual Response Update(T entity) { Response _response = new Response(); if (Repository.Update(entity) > 0) { _response.Code = 1; _response.Message = "更新数据成功!"; _response.Data = entity; } else { _response.Code = 0; _response.Message = "更新数据失败!"; } return _response; } /// <summary> /// 删除 /// </summary> /// <param>主键</param> /// <returns>Code:0-删除失败;1-删除陈功;10-记录不存在</returns> public virtual Response Delete(int ID) { Response _response = new Response(); var _entity = Find(ID); if (_entity == null) { _response.Code = 10; _response.Message = "记录不存在!"; } else { if (Repository.Delete(_entity) > 0) { _response.Code = 1; _response.Message = "删除数据成功!"; } else { _response.Code = 0; _response.Message = "删除数据失败!"; } } return _response; } /// <summary> /// 查找实体 /// </summary> /// <param>主键</param> /// <returns>实体</returns> public virtual T Find(int ID) { return Repository.Find(ID); } /// <summary> /// 查找数据列表-【所有数据】 /// </summary> /// <returns>所有数据</returns> public IQueryable<T> FindList() { return Repository.FindList(); } /// <summary> /// 查找分页数据 /// </summary> /// <param>分页数据</param> /// <returns>分页数据</returns> public Paging<T> FindPageList(Paging<T> paging) { paging.Items = Repository.FindPageList(paging.PageSize, paging.PageIndex, out paging.TotalNumber).ToList(); return paging; } /// <summary> /// 总记录数 /// </summary> /// <returns>总记录数</returns> public virtual int Count() { return Repository.Count(); } } }

=====================================

代码见:https://ninesky.codeplex.com/SourceControl/latest

代码下载:https://ninesky.codeplex.com 点击SOURCE CODE 点击Download下载源文件。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyjpd.html