且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Lind.DDD.Repositories.Mongo层介绍

更新时间:2022-08-14 20:12:46

之前已经发生了

大叔之前讲过被仓储化了的Mongodb,而在大叔开发了Lind.DDD之后,决定把这个东西再搬到本框架的仓储层来,这也是大势所趋的,毕竟mongodb是最像关系数据库的NoSql,它的使用场景是其它nosql所不能及的,这点是毋庸置疑的!

下面是大叔总结的Mongodb文章目录,选自<大叔Mongodb系列>

MongoDB学习笔记~环境搭建 (2015-03-30 10:34)

MongoDB学习笔记~MongoDBRepository仓储的实现 (2015-04-08 12:00)

MongoDB学习笔记~ObjectId主键的设计 (2015-04-09 13:08)

MongoDB学习笔记~客户端命令行的使用 (2015-04-10 13:40)

MongoDB学习笔记~索引提高查询效率 (2015-04-10 15:35)

MongoDB学习笔记~为IMongoRepository接口添加分页取集合的方法 (2015-04-11 22:13)

MongoDB学习笔记~Mongo集群和副本集 (2015-04-17 16:25)

MongoDB学习笔记~为IMongoRepository接口添加了排序和表达式树,针对官方驱动 (2015-04-27 22:11)

MongoDB学习笔记~为IMongoRepository接口添加了增删改方法,针对官方驱动 (2015-04-29 22:36)

MongoDB学习笔记~为IMongoRepository接口更新指定字段(2015-04-30 22:22)

MongoDB学习笔记~关于官方驱动集成IQueryable之后的一些事

MongoDB学习笔记~以匿名对象做为查询参数,方便查询子对象

MongoDB学习笔记~Update方法更新集合属性后的怪问题

MongoDB学习笔记~批量插入方法的实现

MongoDB学习笔记~自己封装的Curd操作(查询集合对象属性,更新集合对象)

MongoDB学习笔记~自己封装的Curd操作(按需更新的先决条件)

MongoDB学习笔记~MongoDB实体中的值对象

MongoDB学习笔记~大叔框架实体更新支持N层嵌套~递归递归我爱你!

MongoDB学习笔记~大叔分享批量添加—批量更新—批量删除

MongoDB学习笔记~MongoVUE对数据进行查询,排序和按需显示

MongoDB学习笔记~管道中的分组实现group+distinct

MongoDB学习笔记~官方驱动的原生Curd操作

MongoDB学习笔记~官方驱动嵌套数组对象的更新

MongoDB学习笔记~使用原生语句实现三层集合关系的更新

MongoDB学习笔记~数据结构与实体对象不一致时,它会怎么样?

Lind.DDD里的仓储模块,Mongodb有一席之地

Lind.DDD.Repositories.Mongo层介绍

大叔的Mongodb仓储结构

Lind.DDD.Repositories.Mongo层介绍

大叔在设计mongodb查询和更新时,使用到了递归

  /// <summary>
        /// 按需要更新的构建者
        /// 递归构建Update操作串
        /// </summary>
        /// <param name="fieldList"></param>
        /// <param name="property"></param>
        /// <param name="propertyValue"></param>
        /// <param name="item"></param>
        /// <param name="fatherValue"></param>
        /// <param name="father"></param>
        private void GenerateRecursionExpress(
          List<UpdateDefinition<TEntity>> fieldList,
          PropertyInfo property,
          object propertyValue,
          TEntity item,
          object fatherValue,
          string father)
        {
            //复杂类型
            if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && propertyValue != null)
            {
                //集合
                if (typeof(IList).IsAssignableFrom(propertyValue.GetType()))
                {
                    var modifyIndex = 0;//要更新的记录索引
                    foreach (var sub in property.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                    {
                        if (sub.PropertyType.IsClass && sub.PropertyType != typeof(string))
                        {
                            var arr = propertyValue as IList;
                            if (arr != null && arr.Count > 0)
                            {

                                var oldValue = property.GetValue(fatherValue ?? item) as IList;
                                if (oldValue != null)
                                {
                                    for (int index = 0; index < arr.Count; index++)
                                    {
                                        for (modifyIndex = 0; modifyIndex < oldValue.Count; modifyIndex++)
                                            if (sub.PropertyType.GetProperty(EntityKey).GetValue(oldValue[modifyIndex]).ToString()
                                                == sub.PropertyType.GetProperty(EntityKey).GetValue(arr[index]).ToString())//比较_id是否相等
                                                break;
                                        foreach (var subInner in sub.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                                        {
                                            if (string.IsNullOrWhiteSpace(father))
                                                GenerateRecursionExpress(fieldList, subInner, subInner.GetValue(arr[index]), item, arr[index], property.Name + "." + modifyIndex);
                                            else
                                                GenerateRecursionExpress(fieldList, subInner, subInner.GetValue(arr[index]), item, arr[index], father + "." + property.Name + "." + modifyIndex);
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
                //实体
                else
                {
                    foreach (var sub in property.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                    {

                        if (string.IsNullOrWhiteSpace(father))
                            GenerateRecursionExpress(fieldList, sub, sub.GetValue(propertyValue), item, property.GetValue(fatherValue), property.Name);
                        else
                            GenerateRecursionExpress(fieldList, sub, sub.GetValue(propertyValue), item, property.GetValue(fatherValue), father + "." + property.Name);
                    }
                }
            }
            //简单类型
            else
            {
                if (property.Name != EntityKey)//更新集中不能有实体键_id
                {
                    if (string.IsNullOrWhiteSpace(father))
                        fieldList.Add(Builders<TEntity>.Update.Set(property.Name, propertyValue));
                    else
                        fieldList.Add(Builders<TEntity>.Update.Set(father + "." + property.Name, propertyValue));
                }
            }
        }

让代码去改变我们的生活,改变我们的世界吧!

本文转自博客园张占岭(仓储大叔)的博客,原文链接:Lind.DDD.Repositories.Mongo层介绍,如需转载请自行联系原博主。