예제 #1
0
        private void InternalAttach(object item, bool needAttachRelations = true)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            Action <object> attachAction = (x) =>
            {
                var trackingEntry = GetTrackingEntry(x);
                if (trackingEntry == null)
                {
                    trackingEntry = new TrackingEntry()
                    {
                        Entity = x, EntryState = EntryState.Unchanged
                    };
                }
                trackingEntry.Entity = x;

                _TrackingEntries[KeySelector(x)] = trackingEntry;

                if (!trackingEntry.IsSubscribed)
                {
                    lock (_lockObject)
                    {
                        if (!trackingEntry.IsSubscribed)
                        {
                            PopulateRelations(x);
                            SubscribeObjectChanges(x);
                            trackingEntry.IsSubscribed = true;
                        }
                    }
                }
            };

            TraverseRelation(item, new List <object>(), attachAction, null, null, needAttachRelations, needAttachRelations);
        }
        private bool TryChangeState(TrackingEntry trackingEntity, EntryState newState)
        {
            if (trackingEntity == null)
            {
                throw new ArgumentNullException("trackingEntity");
            }

            var error = String.Format("Unable to change entity state from {0} -> {1}", trackingEntity.EntryState, newState);
            bool throwError = false;
            if (newState == EntryState.Modified)
            {
                switch (trackingEntity.EntryState)
                {
                    case EntryState.Detached:
                    case EntryState.Unchanged:
                        trackingEntity.EntryState = EntryState.Modified;
                        break;
                    case EntryState.Added:
                    case EntryState.Deleted:
                    case EntryState.Modified:
                        break;
                    default:
                        throwError = true;
                        break;
                }
            }
            else if (newState == EntryState.Added)
            {
                switch (trackingEntity.EntryState)
                {
                    case EntryState.Detached:
                    case EntryState.Unchanged:
                        trackingEntity.EntryState = EntryState.Added;
                        break;
                    default:
                        throwError = true;
                        break;
                }
            }
            else if (newState == EntryState.Deleted)
            {
                switch (trackingEntity.EntryState)
                {
                    case EntryState.Unchanged:
                    case EntryState.Modified:
                        trackingEntity.EntryState = EntryState.Deleted;
                        break;
                    default:
                        throwError = true;
                        break;
                }
            }
            else if (newState == EntryState.Unchanged)
            {
                trackingEntity.EntryState = EntryState.Unchanged;
            }

            if (throwError)
            {
                Debug.WriteLine(error);
                //  throw new InvalidOperationException(error);
            }
            return !throwError;
        }
        private void InternalAttach(object item, bool needAttachRelations = true)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            Action<object> attachAction = (x) =>
            {
                var trackingEntry = GetTrackingEntry(x);
                if (trackingEntry == null)
                {
                    trackingEntry = new TrackingEntry() { Entity = x, EntryState = EntryState.Unchanged };
                }
                trackingEntry.Entity = x;

                _TrackingEntries[KeySelector(x)] = trackingEntry;

                if (!trackingEntry.IsSubscribed)
                {
                    lock (_lockObject)
                    {
                        if (!trackingEntry.IsSubscribed)
                        {
                            PopulateRelations(x);
                            SubscribeObjectChanges(x);
                            trackingEntry.IsSubscribed = true;
                        }
                    }
                }
            };

            TraverseRelation(item, new List<object>(), attachAction, null, null, needAttachRelations, needAttachRelations);
        }
예제 #4
0
        private bool TryChangeState(TrackingEntry trackingEntity, EntryState newState)
        {
            if (trackingEntity == null)
            {
                throw new ArgumentNullException("trackingEntity");
            }

            var  error      = String.Format("Unable to change entity state from {0} -> {1}", trackingEntity.EntryState, newState);
            bool throwError = false;

            if (newState == EntryState.Modified)
            {
                switch (trackingEntity.EntryState)
                {
                case EntryState.Detached:
                case EntryState.Unchanged:
                    trackingEntity.EntryState = EntryState.Modified;
                    break;

                case EntryState.Added:
                case EntryState.Deleted:
                case EntryState.Modified:
                    break;

                default:
                    throwError = true;
                    break;
                }
            }
            else if (newState == EntryState.Added)
            {
                switch (trackingEntity.EntryState)
                {
                case EntryState.Detached:
                case EntryState.Unchanged:
                    trackingEntity.EntryState = EntryState.Added;
                    break;

                default:
                    throwError = true;
                    break;
                }
            }
            else if (newState == EntryState.Deleted)
            {
                switch (trackingEntity.EntryState)
                {
                case EntryState.Unchanged:
                case EntryState.Modified:
                    trackingEntity.EntryState = EntryState.Deleted;
                    break;

                default:
                    throwError = true;
                    break;
                }
            }
            else if (newState == EntryState.Unchanged)
            {
                trackingEntity.EntryState = EntryState.Unchanged;
            }

            if (throwError)
            {
                Debug.WriteLine(error);
                //  throw new InvalidOperationException(error);
            }
            return(!throwError);
        }