1. 程式人生 > >Lind.DDD.Repositories.Mongo層介紹

Lind.DDD.Repositories.Mongo層介紹

回到目錄

之前已經發生了

大叔之前講過被倉儲化了的Mongodb,而在大叔開發了Lind.DDD之後,決定把這個東西再搬到本框架的倉儲層來,這也是大勢所趨的,畢竟mongodb是最像關係資料庫的NoSql,它的使用場景是其它nosql所不能及的,這點是毋庸置疑的!

下面是大叔總結的Mongodb文章目錄,選自<大叔Mongodb系列>

Lind.DDD裡的倉儲模組,Mongodb有一席之地

大叔的Mongodb倉儲結構

大叔在設計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)); } } }

讓程式碼去改變我們的生活,改變我們的世界吧!

回到目錄