コード例 #1
0
 /// <summary>
 /// 构造子项的更新设置
 /// </summary>
 internal ComparedSetter <TSub> GetSubUpdateSet(T syncItem)
 {
     _mainItem = syncItem;
     if (_subSetter == null)
     {
         _subSetter = new ComparedSetter <TSub>();
         if (OnAppendSub != null)
         {
             _subSetter.OnAdd = sub => OnAppendSub(_mainItem, sub);
         }
         if (OnRemoveSub != null)
         {
             _subSetter.OnDelete = sub => OnRemoveSub(_mainItem, sub);
         }
     }
     return(_subSetter);
 }
コード例 #2
0
        /// <summary>
        /// 对比同步数据修改,并执行增、改、删操作
        /// </summary>
        protected void CompareDataChange <T>(ICollection <T> dbData, ICollection <T> syncData, Func <T, DbSet <T> > getDbSet, ComparedSetter <T> setter = null)
            where T : class, IDataComparable <T>
        {
            if (syncData == null)
            {
                if (dbData?.Count > 0)
                {
                    LogService.ErrorFormat("比对数据修改时syncData为null,dbData count:{0}\r\n{1}", dbData.Count, Environment.StackTrace);
                }
                return;
            }
            if (dbData == null)
            {
                dbData = new List <T>();
            }

            //检查修改
            foreach (var newItem in syncData)
            {
                var dbItem = dbData.FirstOrDefault(x => x.SameAs(newItem));
                if (dbItem == null) //新增
                {
                    if (setter?.OnAdd == null || setter.OnAdd(newItem))
                    {
                        getDbSet(newItem).Add(newItem);
                    }
                }
                else //修改
                {
                    dbItem.Update(newItem, setter);
                }
            }

            //检查删除项
            foreach (var item in dbData.Where(d => !syncData.Any(s => s.SameAs(d))))
            {
                if (setter?.OnDelete == null || setter.OnDelete(item))
                {
                    getDbSet(item).Remove(item);
                }
            }
        }