说明什么的就不写了,直接上代码,如果不了解Repository,那就先去百度或者谷歌下……
IRepository接口约束
using IBatisNet.DataAccess.Interfaces; public interface IRepository<T> : IDao where T : class { /// <summary> /// 新增 /// </summary> /// <param name="entity"></param> /// <returns></returns> bool Insert(T entity); /// <summary> /// 编辑 /// </summary> /// <param name="entity"></param> /// <returns></returns> bool Update(T entity); /// <summary> /// 删除 /// </summary> /// <param name="entity"></param> /// <returns></returns> bool Delete(object entity); /// <summary> /// 单个查询 /// </summary> /// <param name="entity"></param> /// <returns></returns> T QueryByKey(object entity); /// <summary> /// 查找数据集合 /// </summary> /// <param name="searchEntity"></param> /// <param name="totalCount"></param> /// <returns></returns> IList<T> FindAll<T1>(T1 searchEntity, out int totalCount) where T1 : BaseQuery; }
BaseQuery查询实体
[Serializable] public class BaseQuery { public int StartIndex { get { if (this.PageSize <= 0 || this.PageIndex < 0) { return 0; } return this.PageSize * this.PageIndex; } } public int EndIndex { get { if (this.PageSize <= 0 || this.PageIndex < 0) { return 0; } return this.PageSize * this.PageIndex + this.PageSize; } } public int PageSize { get; set; } /// <summary> /// 页码,从0开始计算 /// </summary> public int PageIndex { get; set; } /// <summary> /// 修正页码,如果传入的页码索引大于最大页码数,将页码修正为最后一页 /// </summary> /// <param name="totalCount"></param> public void FixPageIndex(int totalCount) { if (this.PageIndex < 0 || this.PageSize <= 0 || totalCount <= 0) { this.PageIndex = 0; return; } if (this.StartIndex >= totalCount) { this.PageIndex = totalCount / this.PageSize; if (totalCount % this.PageSize > 0) { this.PageIndex++; } this.PageIndex--; } } }
Repository抽象基类,所有定义的抽象属性均为SqlMap中statements节点sql对应的id,需在子类中实际指定
using IBatisNet.DataMapper; using System.Linq.Expressions; public abstract class Repository<T> : IRepository<T> where T : class { protected virtual ISqlMapper Mapper { get { return SqlMapperHelper.GetMapper(); } } /// <summary> /// 新增 /// </summary> protected abstract string InsertStatement { get; } /// <summary> /// 编辑 /// </summary> protected abstract string UpdateStatement { get; } /// <summary> /// 删除 /// </summary> protected abstract string DeleteStatement { get; } /// <summary> /// 单查 /// </summary> protected abstract string QueryObjectStatement { get; } /// <summary> /// Count /// </summary> protected abstract string QueryCountStatement { get; } /// <summary> /// 分页查询 /// </summary> protected abstract string QueryPagedListStatement { get; } /// <summary> /// Full Query /// </summary> protected abstract string QueryAllStatement { get; } public virtual bool Insert(T entity) { return this.Mapper.Insert(this.InsertStatement, entity) != null; } public virtual bool Update(T entity) { return this.Mapper.Update(this.UpdateStatement, entity) > 0; } public virtual bool Delete(object entity) { return this.Mapper.Delete(this.DeleteStatement, entity) > 0; } public virtual T QueryByKey(object entity) { return this.Mapper.QueryForObject<T>(this.QueryObjectStatement, entity); } public virtual IList<T> FindAll<T1>(T1 searchEntity, out int totalCount) where T1 : BaseQuery { return this.FindAll<T, T1>(searchEntity, this.QueryCountStatement, this.QueryPagedListStatement, this.QueryAllStatement, out totalCount); } /// <summary> /// 分页查询(如果searchEntity没有指定或者指定Size,Index不正确,则查询所有数据) /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <param name="searchEntity"></param> /// <param name="queryCountStatement"></param> /// <param name="queryPagedListStatement"></param> /// <param name="queryAllStatement"></param> /// <param name="totalCount"></param> /// <returns></returns> protected IList<T1> FindAll<T1, T2>(T2 searchEntity, string queryCountStatement, string queryPagedListStatement, string queryAllStatement, out int totalCount) where T2 : BaseQuery { IList<T1> list = null; if (searchEntity != null && searchEntity.PageIndex >= 0 && searchEntity.PageSize > 0) { //分页 totalCount = this.Mapper.QueryForObject<int>(queryCountStatement, searchEntity); searchEntity.FixPageIndex(totalCount);//修正页码 if (totalCount > 0) { list = this.Mapper.QueryForList<T1>(queryPagedListStatement, searchEntity); } } else { //获取所有数据 list = this.Mapper.QueryForList<T1>(queryAllStatement, searchEntity); totalCount = list.Count; } return list; } /// <summary> /// 单数据库事务 /// </summary> /// <param name="fun"></param> /// <returns></returns> protected bool Transaction(Func<bool> fun) { this.Mapper.BeginTransaction(); try { bool result = fun(); if (result) { this.Mapper.CommitTransaction(); } else { this.Mapper.RollBackTransaction(); } return result; } catch { this.Mapper.RollBackTransaction(); //TODO:log throw; } finally { } } }
SqlMapperHelper帮助类,此处暂时随便写的,如果多数据库,此部分可以与Repository部分联合调整
internal static class SqlMapperHelper { public static ISqlMapper GetMapper() { return Mapper.Instance(); } }
实际的使用例子,IMenu继承自IRepository<Menu>,接口声明就不贴了,只贴实现类代码
public class MenuDataAccess : Repository<Menu>, IMenu { #region Repository protected override string InsertStatement { get { return "RBAC.insertMenu"; } } protected override string UpdateStatement { get { return "RBAC.updateMenu"; } } protected override string DeleteStatement { get { return "RBAC.deleteMenu"; } } protected override string QueryObjectStatement { get { return null; } } protected override string QueryCountStatement { get { return null; } } protected override string QueryPagedListStatement { get { return null; } } protected override string QueryAllStatement { get { return null; } } #endregion #region IMenu public MenuView GetMenu(int moduleID) { return this.Mapper.QueryForObject<MenuView>("RBAC.selectMenuView", moduleID); } public IList<MenuView> FindAllView(int appID) { return this.Mapper.QueryForList<MenuView>("RBAC.selectMenuViews", appID); } public bool AddMenu(Module module, Menu menu) { return this.Transaction(() => new ModuleDataAccess().Insert(module) && this.FillInsertMenu(module, menu)); } private bool FillInsertMenu(Module module, Menu menu) { menu.ModuleID = module.ModuleID; return this.Insert(menu); } public bool UpdateMenu(Module module, Menu menu) { return this.Transaction(() => new ModuleDataAccess().Update(module) && this.Update(menu)); } public bool DeleteMenu(int moduleID) { return this.Transaction(() => this.Delete(moduleID) && new ModuleDataAccess().Delete(moduleID)); } public bool UpdateSortNum(int moduleID, byte sortNum, int? parentModuleID) { return this.Mapper.Update("RBAC.updateMenuSortNum", new { ModuleID = moduleID, SortNum = sortNum, ParentModuleID = parentModuleID }) > 0; } #endregion }
时间: 2024-11-05 18:56:15