Esempio n. 1
0
        public virtual void Clear()
        {
            if (Model.IsTransactional && Model.CurrentTransaction == null)
            {
                throw new Exception("Operation out of transaction");
            }

            if (!OwningEntity.Activated)
            {
                Model.Activate(OwningEntity);
            }

            var    oldItems = Internal.ToArray();
            Action doAction = () =>
            {
                Internal.Clear();
                NotifyCollectionChanged(NotifyCollectionChangedAction.Reset);
                NotifyCountChanged();
            };

            if (!Model.IsTransactional)
            {
                doAction();
                return;
            }

            Action undoAction = () =>
            {
                Internal.AddRange(oldItems);
                NotifyCollectionChanged(NotifyCollectionChangedAction.Add, oldItems);
                NotifyCountChanged();
            };

            Model.CurrentTransaction.DoReversibleAction(doAction, undoAction, OwningEntity, ChangeType.Modified, Property);
        }
Esempio n. 2
0
        protected void Activate()
        {
            //only activate once in a lifetime
            if (_activated)
            {
                return;
            }

            Model.Activate(this);
        }
Esempio n. 3
0
        public void AddRange(IEnumerable <T> values)
        {
            if (Model.IsTransactional && Model.CurrentTransaction == null)
            {
                throw new XbimException("Operation out of transaction");
            }

            var enumerable = values as T[] ?? values.ToArray();

            if (IsEntitySet && enumerable.Any(v => v != null && !ReferenceEquals(((IPersistEntity)v).Model, Model)))
            {
                throw new XbimException("Cross model entity assignment");
            }


            //activate owning entity for write in case it is not active yet
            if (!OwningEntity.Activated)
            {
                Model.Activate(OwningEntity);
            }

            var    items    = values as T[] ?? enumerable.ToArray();
            Action doAction = () =>
            {
                Internal.AddRange(items);
                NotifyCollectionChanged(NotifyCollectionChangedAction.Add, items);
                NotifyCountChanged();
            };


            if (!Model.IsTransactional)
            {
                doAction();
                return;
            }

            Action undoAction = () =>
            {
                foreach (var value in items)
                {
                    Internal.Remove(value);
                }
                NotifyCollectionChanged(NotifyCollectionChangedAction.Remove, items);
                NotifyCountChanged();
            };

            Model.CurrentTransaction.DoReversibleAction(doAction, undoAction, OwningEntity, ChangeType.Modified, Property);
        }
Esempio n. 4
0
        public T this[int index]
        {
            get
            {
                return(Internal[index]);
            }
            set
            {
                if (Model.IsTransactional && Model.CurrentTransaction == null)
                {
                    throw new XbimException("Operation out of transaction");
                }

                if (IsEntitySet && value != null && !ReferenceEquals(((IPersistEntity)value).Model, Model))
                {
                    throw new XbimException("Cross model entity assignment");
                }

                if (!OwningEntity.Activated)
                {
                    Model.Activate(OwningEntity);
                }

                var    oldValue = Internal[index];
                Action doAction = () =>
                {
                    Internal[index] = value;
                    NotifyCollectionChanged(NotifyCollectionChangedAction.Replace, value);
                };


                if (!Model.IsTransactional)
                {
                    doAction();
                    return;
                }

                Action undoAction = () =>
                {
                    Internal[index] = oldValue;
                    NotifyCollectionChanged(NotifyCollectionChangedAction.Replace, oldValue);
                };

                Model.CurrentTransaction.DoReversibleAction(doAction, undoAction, OwningEntity, ChangeType.Modified, Property);
            }
        }
Esempio n. 5
0
        public virtual void Add(T item)
        {
            if (Model.IsTransactional && Model.CurrentTransaction == null)
            {
                throw new Exception("Operation out of transaction");
            }

            if (IsEntitySet && item != null && !ReferenceEquals(((IPersistEntity)item).Model, Model))
            {
                throw new XbimException("Cross model entity assignment");
            }

            //activate owning entity for write in case it is not active yet
            if (!OwningEntity.Activated)
            {
                Model.Activate(OwningEntity);
            }

            Action doAction = () =>
            {
                Internal.Add(item);
                NotifyCollectionChanged(NotifyCollectionChangedAction.Add, item);
                NotifyCountChanged();
            };


            if (!Model.IsTransactional)
            {
                doAction();
                return;
            }

            Action undoAction = () =>
            {
                Internal.Remove(item);
                NotifyCollectionChanged(NotifyCollectionChangedAction.Remove, item);
                NotifyCountChanged();
            };

            Model.CurrentTransaction.DoReversibleAction(doAction, undoAction, OwningEntity, ChangeType.Modified, Property);
        }
Esempio n. 6
0
        public virtual bool Remove(T item)
        {
            if (Model.IsTransactional && Model.CurrentTransaction == null)
            {
                throw new Exception("Operation out of transaction");
            }

            if (!OwningEntity.Activated)
            {
                Model.Activate(OwningEntity);
            }


            Action doAction = () =>
            {
                Internal.Remove(item);
                NotifyCollectionChanged(NotifyCollectionChangedAction.Remove, item);
                NotifyCountChanged();
            };
            Action undoAction = () =>
            {
                Internal.Add(item);
                NotifyCollectionChanged(NotifyCollectionChangedAction.Add, item);
                NotifyCountChanged();
            };

            if (!Model.IsTransactional)
            {
                doAction();
                return(true);
            }

            Model.CurrentTransaction.DoReversibleAction(doAction, undoAction, OwningEntity, ChangeType.Modified, Property);

            return(true);
        }