Пример #1
0
        protected virtual DalMergeOperationResult <V> MergeList(List <V> items, Expression <Func <T, bool> > predicate,
                                                                bool mergeSubItems = true)
        {
            var result = new DalMergeOperationResult <V>();

            using (var s = CreateStrategy())
            {
                var entities = s.GetQuery(predicate).ToList();

                foreach (var entity in entities)
                {
                    var oldValue = new V();
                    CopyDataToModel(entity, oldValue);
                    result.OldValues.Add(oldValue);
                }

                var dictinary = new Dictionary <V, T>();
                foreach (var item in items)
                {
                    var entity = entities.Where(MatchItems(item)).FirstOrDefault();
                    if (entity == null)
                    {
                        entity = new T();
                        s.Add(entity);
                    }

                    dictinary[item] = entity;
                    CopyModelToData(entity, item);
                }

                var deletedEntities = entities.Where(NotMatchItems(items)).ToList();
                foreach (var entity in deletedEntities)
                {
                    DeleteAllSubItems(entity);

                    s.Delete(entity);
                }

                result.Success = s.SubmitChanges();

                foreach (var pair in dictinary)
                {
                    var item   = pair.Key;
                    var entity = pair.Value;
                    CopyDataToModel(entity, item);
                }
            }

            if (mergeSubItems)
            {
                foreach (var item in items)
                {
                    MergeSubItems(item);
                }
            }

            return(result);
        }
Пример #2
0
        protected virtual DalMergeOperationResult <V> MergeList(List <V> items, Expression <Func <T, bool> > predicate,
                                                                bool useModelCulture = false, bool mergeSubItems = true)
        {
            var result = new DalMergeOperationResult <V>();

            using (var s = CreateStrategy())
            {
                var entities = GetJoinedQuery(s, predicate, true).ToList();

                foreach (var entity in entities)
                {
                    var oldValue = new V();
                    CopyDataToModel(entity, oldValue);
                    result.OldValues.Add(oldValue);
                }

                var dictinary = new Dictionary <V, JoinedItem <T, TT> >();
                foreach (var item in items)
                {
                    var entity = entities.Where(MatchItems(item)).FirstOrDefault();
                    if (entity == null)
                    {
                        entity = new JoinedItem <T, TT>();
                        entity.ItemTranslation = null;
                        s.Add(entity.Item);
                    }

                    dictinary[item] = entity;
                    CopyModelToItem(entity, item);
                }

                result.Success = s.SubmitChanges();

                foreach (var pair in dictinary)
                {
                    var item   = pair.Key;
                    var entity = pair.Value;

                    var createTrans = false;
                    if (entity.ItemTranslation == null)
                    {
                        createTrans            = true;
                        entity.ItemTranslation = new TT();
                    }

                    CopyModelToTrans(entity, item);
                    UpdateTransCulture(entity.ItemTranslation, item as ILocalizableModel, useModelCulture);

                    if (createTrans && HasTranslation(entity.ItemTranslation))
                    {
                        s.AddEntity(entity.ItemTranslation);
                    }
                }

                var deletedEntities = entities.Where(NotMatchItems(items)).ToList();
                foreach (var entity in deletedEntities)
                {
                    DeleteAllSubItems(entity.Item);

                    s.Delete(entity.Item);
                }

                result.Success &= s.SubmitChanges();

                foreach (var pair in dictinary)
                {
                    var item   = pair.Key;
                    var entity = pair.Value;
                    CopyDataToModel(entity, item);
                }
            }

            if (mergeSubItems)
            {
                foreach (var item in items)
                {
                    MergeSubItems(item);
                }
            }

            return(result);
        }