예제 #1
0
        public void GetEnumerator_UsesOverrides()
        {
            var parent = new Dictionary <string, object>
            {
                ["first"]  = 1,
                ["second"] = 2
            };

            var dict = new FallbackDictionary <string, object>(parent)
            {
                ["second"] = "two",
                ["third"]  = 3
            };

            var list = new List <KeyValuePair <string, object> >();

            foreach (var kvp in dict)
            {
                list.Add(kvp);
            }

            Assert.Equal(3, list.Count);
            Assert.Contains(1, list.Select(k => k.Value));
            Assert.Contains("two", list.Select(k => k.Value));
            Assert.Contains(3, list.Select(k => k.Value));
        }
예제 #2
0
        public void Add_LocalContainsKey()
        {
            var dict = new FallbackDictionary <string, object>();

            dict.Add("key", true);
            Assert.Throws <ArgumentException>(() => dict.Add("key", false));
        }
예제 #3
0
        public void Count_LocalOnly()
        {
            var dict = new FallbackDictionary <string, object>();

            dict.Add("first", 1);
            dict.Add("second", 2);

            Assert.Equal(2, dict.Count);
        }
예제 #4
0
        public void Contains_LocalOnly()
        {
            var dict = new FallbackDictionary <string, object>();

            dict.Add("first", 1);
            dict.Add("second", 2);

            Assert.Contains(new KeyValuePair <string, object>("first", 1), dict);
            Assert.Contains(new KeyValuePair <string, object>("second", 2), dict);
        }
예제 #5
0
        public void Add_ParentContainsKey()
        {
            var parent = new Dictionary <string, object>();

            parent["key"] = true;

            var dict = new FallbackDictionary <string, object>(parent);

            Assert.Throws <ArgumentException>(() => dict.Add("key", false));
        }
예제 #6
0
        public void Count_LocalOnly()
        {
            var dict = new FallbackDictionary <string, object>
            {
                { "first", 1 },
                { "second", 2 }
            };

            Assert.Equal(2, dict.Count);
        }
예제 #7
0
        public void Count_ParentOnly()
        {
            var parent = new Dictionary <string, object>();

            parent.Add("first", 1);
            parent.Add("second", 2);

            var dict = new FallbackDictionary <string, object>(parent);

            Assert.Equal(2, dict.Count);
        }
예제 #8
0
        public void Contains_ParentOnly()
        {
            var parent = new Dictionary <string, object>();

            parent.Add("first", 1);
            parent.Add("second", 2);

            var dict = new FallbackDictionary <string, object>(parent);

            Assert.True(dict.Contains(new KeyValuePair <string, object>("first", 1)));
            Assert.True(dict.Contains(new KeyValuePair <string, object>("second", 2)));
        }
예제 #9
0
        public void Values_LocalOnly()
        {
            var dict = new FallbackDictionary <string, object>
            {
                { "first", 1 },
                { "second", 2 }
            };

            Assert.Equal(2, dict.Values.Count);
            Assert.Contains(1, dict.Values);
            Assert.Contains(2, dict.Values);
        }
예제 #10
0
        public void Count_ParentOnly()
        {
            var parent = new Dictionary <string, object>
            {
                { "first", 1 },
                { "second", 2 }
            };

            var dict = new FallbackDictionary <string, object>(parent);

            Assert.Equal(2, dict.Count);
        }
예제 #11
0
        public void Contains_ParentOnly()
        {
            var parent = new Dictionary <string, object>
            {
                { "first", 1 },
                { "second", 2 }
            };

            var dict = new FallbackDictionary <string, object>(parent);

            Assert.Contains(new KeyValuePair <string, object>("first", 1), dict);
            Assert.Contains(new KeyValuePair <string, object>("second", 2), dict);
        }
예제 #12
0
        public void Count_LocalAndParent()
        {
            var parent = new Dictionary <string, object>();

            parent.Add("first", 1);
            parent.Add("second", 2);

            var dict = new FallbackDictionary <string, object>(parent);

            dict.Add("third", 3);

            Assert.Equal(3, dict.Count);
        }
예제 #13
0
        public void Count_IgnoresOverrides()
        {
            var parent = new Dictionary <string, object>();

            parent["first"]  = 1;
            parent["second"] = 2;

            var dict = new FallbackDictionary <string, object>(parent);

            dict["second"] = "two";
            dict["third"]  = 3;

            Assert.Equal(3, dict.Count);
        }
예제 #14
0
        public void Count_LocalAndParent()
        {
            var parent = new Dictionary <string, object>
            {
                { "first", 1 },
                { "second", 2 }
            };

            var dict = new FallbackDictionary <string, object>(parent)
            {
                { "third", 3 }
            };

            Assert.Equal(3, dict.Count);
        }
예제 #15
0
        public void Contains_LocalAndParent()
        {
            var parent = new Dictionary <string, object>();

            parent.Add("first", 1);
            parent.Add("second", 2);

            var dict = new FallbackDictionary <string, object>(parent);

            dict.Add("third", 3);

            Assert.Contains(new KeyValuePair <string, object>("first", 1), dict);
            Assert.Contains(new KeyValuePair <string, object>("second", 2), dict);
            Assert.Contains(new KeyValuePair <string, object>("third", 3), dict);
        }
예제 #16
0
        public void Values_UsesOverrides()
        {
            var parent = new Dictionary <string, object>();

            parent["first"]  = 1;
            parent["second"] = 2;

            var dict = new FallbackDictionary <string, object>(parent);

            dict["second"] = "two";
            dict["third"]  = 3;

            Assert.Equal(3, dict.Values.Count);
            Assert.Contains(1, dict.Values);
            Assert.Contains("two", dict.Values);
            Assert.Contains(3, dict.Values);
        }
예제 #17
0
        public void Keys_IgnoresDuplicatesInOverrides()
        {
            var parent = new Dictionary <string, object>();

            parent["first"]  = 1;
            parent["second"] = 2;

            var dict = new FallbackDictionary <string, object>(parent);

            dict["second"] = "two";
            dict["third"]  = 3;

            Assert.Equal(3, dict.Keys.Count);
            Assert.Contains("first", dict.Keys);
            Assert.Contains("second", dict.Keys);
            Assert.Contains("third", dict.Keys);
        }
예제 #18
0
        public void Contains_UsesOverrides()
        {
            var parent = new Dictionary <string, object>();

            parent["first"]  = 1;
            parent["second"] = 2;

            var dict = new FallbackDictionary <string, object>(parent);

            dict["second"] = "two";
            dict["third"]  = 3;

            Assert.Contains(new KeyValuePair <string, object>("first", 1), dict);
            Assert.DoesNotContain(new KeyValuePair <string, object>("second", 2), dict);
            Assert.Contains(new KeyValuePair <string, object>("second", "two"), dict);
            Assert.Contains(new KeyValuePair <string, object>("third", 3), dict);
        }
예제 #19
0
        /// <summary>
        /// Creates and setup the registry for a child scope.
        /// </summary>
        /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
        /// <param name="configurationAction">Action on a <see cref="ContainerBuilder"/>
        /// that adds component registrations visible only in the child scope.</param>
        /// <returns>Registry to use for a child scope.</returns>
        /// <remarks>It is the responsibility of the caller to make sure that the registry is properly
        /// disposed of. This is generally done by adding the registry to the <see cref="Disposer"/>
        /// property of the child scope.</remarks>
        private IComponentRegistryBuilder CreateScopeRestrictedRegistry(object tag, Action <ContainerBuilder> configurationAction)
        {
            var restrictedRootScopeLifetime = new MatchingScopeLifetime(tag);
            var tracker = new ScopeRestrictedRegisteredServicesTracker(restrictedRootScopeLifetime);

            var fallbackProperties = new FallbackDictionary <string, object?>(ComponentRegistry.Properties);
            var registryBuilder    = new ComponentRegistryBuilder(tracker, fallbackProperties);

            var builder = new ContainerBuilder(fallbackProperties, registryBuilder);

            foreach (var source in ComponentRegistry.Sources)
            {
                if (source.IsAdapterForIndividualComponents)
                {
                    builder.RegisterSource(source);
                }
            }

            // Issue #272: Only the most nested parent registry with HasLocalComponents is registered as an external source
            // It provides all non-adapting registrations from itself and from it's parent registries
            ISharingLifetimeScope?parent = this;

            while (parent != null)
            {
                if (parent.ComponentRegistry.HasLocalComponents)
                {
                    var externalSource = new ExternalRegistrySource(parent.ComponentRegistry);
                    builder.RegisterSource(externalSource);
                    break;
                }

                parent = parent.ParentLifetimeScope;
            }

            configurationAction(builder);

            builder.UpdateRegistry(registryBuilder);
            return(registryBuilder);
        }
예제 #20
0
        public void Keys_Values_SameOrder()
        {
            var parent = new Dictionary <string, object>();

            parent["first"]  = 1;
            parent["second"] = 2;

            var dict = new FallbackDictionary <string, object>(parent);

            dict["second"] = "two";
            dict["third"]  = 3;

            var keys   = dict.Keys.ToArray();
            var values = dict.Values.ToArray();

            Assert.Equal(3, keys.Length);
            Assert.Equal(3, values.Length);

            Assert.Equal(values[0], dict[keys[0]]);
            Assert.Equal(values[1], dict[keys[1]]);
            Assert.Equal(values[2], dict[keys[2]]);
        }
예제 #21
0
 public CopyOnWriteRegistry(IComponentRegistry readRegistry, Func <IComponentRegistry> createWriteRegistry)
 {
     _readRegistry        = readRegistry ?? throw new ArgumentNullException(nameof(readRegistry));
     _createWriteRegistry = createWriteRegistry ?? throw new ArgumentNullException(nameof(createWriteRegistry));
     Properties           = new FallbackDictionary <string, object>(readRegistry.Properties);
 }
예제 #22
0
        public void Keys_Empty()
        {
            var dict = new FallbackDictionary <string, object>();

            Assert.Equal(0, dict.Keys.Count);
        }
예제 #23
0
        public void Count_Empty()
        {
            var dict = new FallbackDictionary <string, object>();

            Assert.Empty(dict);
        }
예제 #24
0
        public void Ctor_Default()
        {
            var dict = new FallbackDictionary <string, object>();

            Assert.IsAssignableFrom <IDictionary <string, object> >(dict.Parent);
        }