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 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 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 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;
         }
     }
 }