发布网友 发布时间:2022-04-07 16:01
共4个回答
懂视网 时间:2022-04-07 20:22
<summary> /// 官方驱动,返回带分页的结果集 /// </summary> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> PagedResult<TEntity> GetModel(int pageIndex, int pageSize); /// <summary> /// 官方驱动,返回带条件和分页的结果集 /// </summary> /// <param name="expression"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> PagedResult<TEntity> GetModel(Expression<Func<TEntity, bool>> expression, int pageIndex, int pageSize); /// <summary> /// 官方驱动,返回带排序和分页的结果集 /// </summary> /// <param name="fields"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> PagedResult<TEntity> GetModel(Dictionary<Expression<Func<TEntity, object>>, bool> fields, int pageIndex, int pageSize); /// <summary> /// 官方驱动,返回带条件和排序及分页的结果集 /// </summary> /// <param name="expression"></param> /// <param name="fields"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> PagedResult<TEntity> GetModel(Expression<Func<TEntity, bool>> expression, Dictionary<Expression<Func<TEntity, object>>, bool> fields, int pageIndex, int pageSize);对这四大方法的实现,为了不让MongoDB的更多细节公开,所以,更多的内核只能在仓储中实现了,呵呵
public PagedResult<TEntity> GetModel(int pageIndex, int pageSize) { return GetModel(i => true, pageIndex, pageSize); } public PagedResult<TEntity> GetModel(System.Linq.Expressions.Expression<Func<TEntity, bool>> expression, int pageIndex, int pageSize) { return GetModel(expression, new Dictionary<Expression<Func<TEntity, object>>, bool>(), pageIndex, pageSize); } public PagedResult<TEntity> GetModel( Dictionary<Expression<Func<TEntity, object>>, bool> fields, int pageIndex, int pageSize) { return GetModel(i => true, fields, pageIndex, pageSize); } public PagedResult<TEntity> GetModel( Expression<Func<TEntity, bool>> expression, Dictionary<Expression<Func<TEntity, object>>, bool> fields, int pageIndex, int pageSize) { SortDefinition<TEntity> sorts = new ObjectSortDefinition<TEntity>(new { }); foreach (var item in fields) { if (item.Value) sorts = sorts.Ascending(item.Key); else sorts = sorts.Descending(item.Key); } var skip = (pageIndex - 1) * pageSize; var limit = pageSize; var recordCount = _table.CountAsync<TEntity>(i => true).Result; return new PagedResult<TEntity>( recordCount, (int)(recordCount + pageSize - 1) / pageSize, pageSize, pageIndex, _table.Find(expression) .Sort(sorts) .Skip(skip) .Limit(limit) .ToListAsync().Result); }
OK,现在你可以在业务层去调用它了,基本上面四个方法可以满足你的一切需求了,呵呵!
看下在UI层对它的调用,本DEMO没有BLL层,呵呵
// // GET: /Background/ public ActionResult Index(int sort = 0, int page = 1) { Dictionary<Expression<Func<WebManageUsers, object>>, bool> sortList = new System.Collections.Generic.Dictionary<Expression<Func<WebManageUsers, object>>, bool>(); sortList.Add(i => i.LoginName, sort == 0); var model = _webManageUsersRepository.GetModel(sortList,page, 10); return View(model); }
怎么样,看了上面代码是否心里痒痒了,哈哈,赶快去开发你自己的MongoDB仓储吧!
MongoDB学习笔记~为IMongoRepository接口添加了排序和表达式树,针对官方驱动
标签:
热心网友 时间:2022-04-07 17:30
与HibernateRepository类似,通过继承MongoRepository接口,我们可以非常方便地实现对一个对象的增删改查,要使热心网友 时间:2022-04-07 18:48
没有用spring提供的MongoRepository接口,他好像没有提供较复杂的更新数据方法,我使用了org.springframework.data.mongodb.core.MongoOperations这个类,它提供了更新方法热心网友 时间:2022-04-07 20:23
可以使用