public void ReadInstanceKeyMetadata(Guid key, IDictionary<XName, InstanceValue> metadata, bool complete)
        {
            if (key == Guid.Empty)
            {
                throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument);
            }
            ThrowIfNoInstance();
            ThrowIfNotActive("ReadInstanceKeyMetadata");

            InstanceKeyView keyView;
            if (!InstanceView.InstanceKeys.TryGetValue(key, out keyView))
            {
                if (InstanceView.InstanceKeysConsistency == InstanceValueConsistency.None)
                {
                    throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyNotAssociated));
                }

                Dictionary<Guid, InstanceKeyView> copy = new Dictionary<Guid, InstanceKeyView>(InstanceView.InstanceKeys);
                keyView = new InstanceKeyView(key);
                if (complete)
                {
                    keyView.InstanceKeyMetadata = metadata.ReadOnlyCopy(false);
                    keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.None;
                }
                else
                {
                    keyView.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(null, false);
                    keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial;
                }
                if (!InstanceView.IsBoundToLock && InstanceView.InstanceState != InstanceState.Completed)
                {
                    keyView.InstanceKeyMetadataConsistency |= InstanceValueConsistency.InDoubt;
                }
                copy[keyView.InstanceKey] = keyView;
                InstanceView.InstanceKeys = new ReadOnlyDictionaryInternal<Guid, InstanceKeyView>(copy);
            }
            else
            {
                if (keyView.InstanceKeyMetadataConsistency == InstanceValueConsistency.None)
                {
                    return;
                }

                if (complete)
                {
                    keyView.InstanceKeyMetadata = metadata.ReadOnlyCopy(false);
                    keyView.InstanceKeyMetadataConsistency = InstanceView.IsBoundToLock || InstanceView.InstanceState == InstanceState.Completed ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt;
                }
                else
                {
                    if ((InstanceView.IsBoundToLock || InstanceView.InstanceState == InstanceState.Completed) && (keyView.InstanceKeyMetadataConsistency & InstanceValueConsistency.InDoubt) != 0)
                    {
                        // In this case, prefer throwing out old data and keeping only authoritative data.
                        keyView.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(null, false);
                        keyView.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial;
                    }
                    else
                    {
                        keyView.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(keyView.InstanceKeyMetadata, false);
                        keyView.InstanceKeyMetadataConsistency |= InstanceValueConsistency.Partial;
                    }
                }
            }
        }
        public void ReadInstanceOwnerMetadata(IDictionary<XName, InstanceValue> metadata, bool complete)
        {
            ThrowIfNoOwner();
            ThrowIfNotActive("ReadInstanceOwnerMetadata");

            if (InstanceView.InstanceOwnerMetadataConsistency == InstanceValueConsistency.None)
            {
                return;
            }

            if (complete)
            {
                InstanceView.InstanceOwnerMetadata = metadata.ReadOnlyCopy(false);
                InstanceView.InstanceOwnerMetadataConsistency = InstanceValueConsistency.InDoubt;
            }
            else
            {
                InstanceView.InstanceOwnerMetadata = metadata.ReadOnlyMergeInto(InstanceView.InstanceOwnerMetadata, false);
                InstanceView.InstanceOwnerMetadataConsistency |= InstanceValueConsistency.Partial;
            }
        }
        public void LoadedInstance(InstanceState state, IDictionary<XName, InstanceValue> instanceData, IDictionary<XName, InstanceValue> instanceMetadata, IDictionary<Guid, IDictionary<XName, InstanceValue>> associatedInstanceKeyMetadata, IDictionary<Guid, IDictionary<XName, InstanceValue>> completedInstanceKeyMetadata)
        {
            if (state == InstanceState.Uninitialized)
            {
                if (instanceData != null && instanceData.Count > 0)
                {
                    throw Fx.Exception.AsError(new InvalidOperationException(SRCore.UninitializedCannotHaveData));
                }
            }
            else if (state == InstanceState.Completed)
            {
                if (associatedInstanceKeyMetadata != null && associatedInstanceKeyMetadata.Count > 0)
                {
                    throw Fx.Exception.AsError(new InvalidOperationException(SRCore.CompletedMustNotHaveAssociatedKeys));
                }
            }
            else if (state != InstanceState.Initialized)
            {
                throw Fx.Exception.Argument("state", SRCore.InvalidInstanceState);
            }
            ThrowIfNoInstance();
            ThrowIfNotActive("PersistedInstance");

            InstanceValueConsistency consistency = InstanceView.IsBoundToLock || state == InstanceState.Completed ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt;

            ReadOnlyDictionaryInternal<XName, InstanceValue> instanceDataCopy = instanceData.ReadOnlyCopy(false);
            ReadOnlyDictionaryInternal<XName, InstanceValue> instanceMetadataCopy = instanceMetadata.ReadOnlyCopy(false);

            Dictionary<Guid, InstanceKeyView> keysCopy = null;
            int totalKeys = (associatedInstanceKeyMetadata != null ? associatedInstanceKeyMetadata.Count : 0) + (completedInstanceKeyMetadata != null ? completedInstanceKeyMetadata.Count : 0);
            if (totalKeys > 0)
            {
                keysCopy = new Dictionary<Guid, InstanceKeyView>(totalKeys);
            }
            if (associatedInstanceKeyMetadata != null && associatedInstanceKeyMetadata.Count > 0)
            {
                foreach (KeyValuePair<Guid, IDictionary<XName, InstanceValue>> keyMetadata in associatedInstanceKeyMetadata)
                {
                    InstanceKeyView view = new InstanceKeyView(keyMetadata.Key);
                    view.InstanceKeyState = InstanceKeyState.Associated;
                    view.InstanceKeyMetadata = keyMetadata.Value.ReadOnlyCopy(false);
                    view.InstanceKeyMetadataConsistency = InstanceView.IsBoundToLock ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt;
                    keysCopy.Add(view.InstanceKey, view);
                }
            }

            if (completedInstanceKeyMetadata != null && completedInstanceKeyMetadata.Count > 0)
            {
                foreach (KeyValuePair<Guid, IDictionary<XName, InstanceValue>> keyMetadata in completedInstanceKeyMetadata)
                {
                    InstanceKeyView view = new InstanceKeyView(keyMetadata.Key);
                    view.InstanceKeyState = InstanceKeyState.Completed;
                    view.InstanceKeyMetadata = keyMetadata.Value.ReadOnlyCopy(false);
                    view.InstanceKeyMetadataConsistency = consistency;
                    keysCopy.Add(view.InstanceKey, view);
                }
            }

            InstanceView.InstanceState = state;

            InstanceView.InstanceData = instanceDataCopy;
            InstanceView.InstanceDataConsistency = consistency;

            InstanceView.InstanceMetadata = instanceMetadataCopy;
            InstanceView.InstanceMetadataConsistency = consistency;

            InstanceView.InstanceKeys = keysCopy == null ? null : new ReadOnlyDictionaryInternal<Guid, InstanceKeyView>(keysCopy);
            InstanceView.InstanceKeysConsistency = consistency;
        }
        public void ReadInstanceMetadata(IDictionary<XName, InstanceValue> metadata, bool complete)
        {
            ThrowIfNoInstance();
            ThrowIfNotActive("ReadInstanceMetadata");

            if (InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
            {
                return;
            }

            if (complete)
            {
                InstanceView.InstanceMetadata = metadata.ReadOnlyCopy(false);
                InstanceView.InstanceMetadataConsistency = InstanceView.IsBoundToLock || InstanceView.InstanceState == InstanceState.Completed ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt;
            }
            else
            {
                if ((InstanceView.IsBoundToLock || InstanceView.InstanceState == InstanceState.Completed) && (InstanceView.InstanceMetadataConsistency & InstanceValueConsistency.InDoubt) != 0)
                {
                    // In this case, prefer throwing out old data and keeping only authoritative data.
                    InstanceView.InstanceMetadata = metadata.ReadOnlyMergeInto(null, false);
                    InstanceView.InstanceMetadataConsistency = InstanceValueConsistency.Partial;
                }
                else
                {
                    InstanceView.InstanceMetadata = metadata.ReadOnlyMergeInto(InstanceView.InstanceMetadata, false);
                    InstanceView.InstanceMetadataConsistency |= InstanceValueConsistency.Partial;
                }
            }
        }
        public void PersistedInstance(IDictionary<XName, InstanceValue> data)
        {
            ThrowIfNotLocked();
            ThrowIfCompleted();
            ThrowIfNotTransactional("PersistedInstance");

            InstanceView.InstanceData = data.ReadOnlyCopy(true);
            InstanceView.InstanceDataConsistency = InstanceValueConsistency.None;
            InstanceView.InstanceState = InstanceState.Initialized;
        }
 public void ReadInstanceOwnerMetadata(IDictionary<XName, InstanceValue> metadata, bool complete)
 {
     this.ThrowIfNoOwner();
     this.ThrowIfNotActive("ReadInstanceOwnerMetadata");
     if (this.InstanceView.InstanceOwnerMetadataConsistency != InstanceValueConsistency.None)
     {
         if (complete)
         {
             this.InstanceView.InstanceOwnerMetadata = metadata.ReadOnlyCopy(false);
             this.InstanceView.InstanceOwnerMetadataConsistency = InstanceValueConsistency.InDoubt;
         }
         else
         {
             this.InstanceView.InstanceOwnerMetadata = metadata.ReadOnlyMergeInto(this.InstanceView.InstanceOwnerMetadata, false);
             System.Runtime.DurableInstancing.InstanceView instanceView = this.InstanceView;
             instanceView.InstanceOwnerMetadataConsistency |= InstanceValueConsistency.Partial;
         }
     }
 }
 public void ReadInstanceMetadata(IDictionary<XName, InstanceValue> metadata, bool complete)
 {
     this.ThrowIfNoInstance();
     this.ThrowIfNotActive("ReadInstanceMetadata");
     if (this.InstanceView.InstanceMetadataConsistency != InstanceValueConsistency.None)
     {
         if (complete)
         {
             this.InstanceView.InstanceMetadata = metadata.ReadOnlyCopy(false);
             this.InstanceView.InstanceMetadataConsistency = (this.InstanceView.IsBoundToLock || (this.InstanceView.InstanceState == InstanceState.Completed)) ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt;
         }
         else if ((this.InstanceView.IsBoundToLock || (this.InstanceView.InstanceState == InstanceState.Completed)) && ((this.InstanceView.InstanceMetadataConsistency & InstanceValueConsistency.InDoubt) != InstanceValueConsistency.None))
         {
             this.InstanceView.InstanceMetadata = metadata.ReadOnlyMergeInto(null, false);
             this.InstanceView.InstanceMetadataConsistency = InstanceValueConsistency.Partial;
         }
         else
         {
             this.InstanceView.InstanceMetadata = metadata.ReadOnlyMergeInto(this.InstanceView.InstanceMetadata, false);
             System.Runtime.DurableInstancing.InstanceView instanceView = this.InstanceView;
             instanceView.InstanceMetadataConsistency |= InstanceValueConsistency.Partial;
         }
     }
 }
 public void ReadInstanceKeyMetadata(Guid key, IDictionary<XName, InstanceValue> metadata, bool complete)
 {
     InstanceKeyView view;
     if (key == Guid.Empty)
     {
         throw Fx.Exception.Argument("key", SRCore.InvalidKeyArgument);
     }
     this.ThrowIfNoInstance();
     this.ThrowIfNotActive("ReadInstanceKeyMetadata");
     if (!this.InstanceView.InstanceKeys.TryGetValue(key, out view))
     {
         if (this.InstanceView.InstanceKeysConsistency == InstanceValueConsistency.None)
         {
             throw Fx.Exception.AsError(new InvalidOperationException(SRCore.KeyNotAssociated));
         }
         Dictionary<Guid, InstanceKeyView> dictionary = new Dictionary<Guid, InstanceKeyView>(this.InstanceView.InstanceKeys);
         view = new InstanceKeyView(key);
         if (complete)
         {
             view.InstanceKeyMetadata = metadata.ReadOnlyCopy(false);
             view.InstanceKeyMetadataConsistency = InstanceValueConsistency.None;
         }
         else
         {
             view.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(null, false);
             view.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial;
         }
         if (!this.InstanceView.IsBoundToLock && (this.InstanceView.InstanceState != InstanceState.Completed))
         {
             view.InstanceKeyMetadataConsistency |= InstanceValueConsistency.InDoubt;
         }
         dictionary[view.InstanceKey] = view;
         this.InstanceView.InstanceKeys = new ReadOnlyDictionary<Guid, InstanceKeyView>(dictionary, false);
     }
     else if (view.InstanceKeyMetadataConsistency != InstanceValueConsistency.None)
     {
         if (complete)
         {
             view.InstanceKeyMetadata = metadata.ReadOnlyCopy(false);
             view.InstanceKeyMetadataConsistency = (this.InstanceView.IsBoundToLock || (this.InstanceView.InstanceState == InstanceState.Completed)) ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt;
         }
         else if ((this.InstanceView.IsBoundToLock || (this.InstanceView.InstanceState == InstanceState.Completed)) && ((view.InstanceKeyMetadataConsistency & InstanceValueConsistency.InDoubt) != InstanceValueConsistency.None))
         {
             view.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(null, false);
             view.InstanceKeyMetadataConsistency = InstanceValueConsistency.Partial;
         }
         else
         {
             view.InstanceKeyMetadata = metadata.ReadOnlyMergeInto(view.InstanceKeyMetadata, false);
             view.InstanceKeyMetadataConsistency |= InstanceValueConsistency.Partial;
         }
     }
 }
 public void LoadedInstance(InstanceState state, IDictionary<XName, InstanceValue> instanceData, IDictionary<XName, InstanceValue> instanceMetadata, IDictionary<Guid, IDictionary<XName, InstanceValue>> associatedInstanceKeyMetadata, IDictionary<Guid, IDictionary<XName, InstanceValue>> completedInstanceKeyMetadata)
 {
     if (state == InstanceState.Uninitialized)
     {
         if ((instanceData != null) && (instanceData.Count > 0))
         {
             throw Fx.Exception.AsError(new InvalidOperationException(SRCore.UninitializedCannotHaveData));
         }
     }
     else if (state == InstanceState.Completed)
     {
         if ((associatedInstanceKeyMetadata != null) && (associatedInstanceKeyMetadata.Count > 0))
         {
             throw Fx.Exception.AsError(new InvalidOperationException(SRCore.CompletedMustNotHaveAssociatedKeys));
         }
     }
     else if (state != InstanceState.Initialized)
     {
         throw Fx.Exception.Argument("state", SRCore.InvalidInstanceState);
     }
     this.ThrowIfNoInstance();
     this.ThrowIfNotActive("PersistedInstance");
     InstanceValueConsistency consistency = (this.InstanceView.IsBoundToLock || (state == InstanceState.Completed)) ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt;
     ReadOnlyDictionary<XName, InstanceValue> dictionary = instanceData.ReadOnlyCopy(false);
     ReadOnlyDictionary<XName, InstanceValue> dictionary2 = instanceMetadata.ReadOnlyCopy(false);
     Dictionary<Guid, InstanceKeyView> dictionary3 = null;
     int capacity = ((associatedInstanceKeyMetadata != null) ? associatedInstanceKeyMetadata.Count : 0) + ((completedInstanceKeyMetadata != null) ? completedInstanceKeyMetadata.Count : 0);
     if (capacity > 0)
     {
         dictionary3 = new Dictionary<Guid, InstanceKeyView>(capacity);
     }
     if ((associatedInstanceKeyMetadata != null) && (associatedInstanceKeyMetadata.Count > 0))
     {
         foreach (KeyValuePair<Guid, IDictionary<XName, InstanceValue>> pair in associatedInstanceKeyMetadata)
         {
             InstanceKeyView view = new InstanceKeyView(pair.Key) {
                 InstanceKeyState = InstanceKeyState.Associated,
                 InstanceKeyMetadata = pair.Value.ReadOnlyCopy(false),
                 InstanceKeyMetadataConsistency = this.InstanceView.IsBoundToLock ? InstanceValueConsistency.None : InstanceValueConsistency.InDoubt
             };
             dictionary3.Add(view.InstanceKey, view);
         }
     }
     if ((completedInstanceKeyMetadata != null) && (completedInstanceKeyMetadata.Count > 0))
     {
         foreach (KeyValuePair<Guid, IDictionary<XName, InstanceValue>> pair2 in completedInstanceKeyMetadata)
         {
             InstanceKeyView view2 = new InstanceKeyView(pair2.Key) {
                 InstanceKeyState = InstanceKeyState.Completed,
                 InstanceKeyMetadata = pair2.Value.ReadOnlyCopy(false),
                 InstanceKeyMetadataConsistency = consistency
             };
             dictionary3.Add(view2.InstanceKey, view2);
         }
     }
     this.InstanceView.InstanceState = state;
     this.InstanceView.InstanceData = dictionary;
     this.InstanceView.InstanceDataConsistency = consistency;
     this.InstanceView.InstanceMetadata = dictionary2;
     this.InstanceView.InstanceMetadataConsistency = consistency;
     this.InstanceView.InstanceKeys = (dictionary3 == null) ? null : new ReadOnlyDictionary<Guid, InstanceKeyView>(dictionary3, false);
     this.InstanceView.InstanceKeysConsistency = consistency;
 }