1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 
5 namespace System.Runtime.DurableInstancing
6 {
7     using System;
8     using System.Collections.Generic;
9     using System.Collections.ObjectModel;
10     using System.Runtime;
11     using System.Xml.Linq;
12 
13     [Fx.Tag.XamlVisible(false)]
14     public sealed class InstanceKeyView
15     {
16         static readonly ReadOnlyDictionaryInternal<XName, InstanceValue> emptyProperties = new ReadOnlyDictionaryInternal<XName, InstanceValue>(new Dictionary<XName, InstanceValue>(0));
17 
18         IDictionary<XName, InstanceValue> metadata;
19         Dictionary<XName, InstanceValue> accumulatedMetadataWrites;
20 
InstanceKeyView(Guid key)21         internal InstanceKeyView(Guid key)
22         {
23             InstanceKey = key;
24             InstanceKeyMetadataConsistency = InstanceValueConsistency.InDoubt | InstanceValueConsistency.Partial;
25         }
26 
InstanceKeyView(InstanceKeyView source)27         InstanceKeyView(InstanceKeyView source)
28         {
29             InstanceKey = source.InstanceKey;
30             InstanceKeyState = source.InstanceKeyState;
31 
32             InstanceKeyMetadata = source.InstanceKeyMetadata;
33             InstanceKeyMetadataConsistency = source.InstanceKeyMetadataConsistency;
34         }
35 
36         public Guid InstanceKey { get; private set; }
37         public InstanceKeyState InstanceKeyState { get; internal set; }
38 
39         public InstanceValueConsistency InstanceKeyMetadataConsistency { get; internal set; }
40         public IDictionary<XName, InstanceValue> InstanceKeyMetadata
41         {
42             get
43             {
44                 IDictionary<XName, InstanceValue> pendingWrites = this.accumulatedMetadataWrites;
45                 this.accumulatedMetadataWrites = null;
46                 this.metadata = pendingWrites.ReadOnlyMergeInto(this.metadata ?? InstanceKeyView.emptyProperties, true);
47                 return this.metadata;
48             }
49             internal set
50             {
51                 this.accumulatedMetadataWrites = null;
52                 this.metadata = value;
53             }
54         }
55         internal Dictionary<XName, InstanceValue> AccumulatedMetadataWrites
56         {
57             get
58             {
59                 if (this.accumulatedMetadataWrites == null)
60                 {
61                     this.accumulatedMetadataWrites = new Dictionary<XName, InstanceValue>();
62                 }
63                 return this.accumulatedMetadataWrites;
64             }
65         }
66 
Clone()67         internal InstanceKeyView Clone()
68         {
69             return new InstanceKeyView(this);
70         }
71     }
72 }
73