/// <summary>
        /// Subscribes supplied object to property changed notifications and invokes the provided callback
        /// </summary>
        /// <typeparam name="T">Type of subject</typeparam>
        /// <param name="vmb">Subject</param>
        /// <param name="path">Property path</param>
        /// <param name="callback">Notification callback</param>
        public static void NotifyOn <T>(this T vmb, string path, Action <object, object> callback)
        {
            Dictionary <string, NotificationHelperDp> block;

            if (!_notifiers.TryGetValue(vmb, out block))
            {
                _notifiers.Add(vmb, block = new Dictionary <string, NotificationHelperDp>());
            }
            block.Remove(path);

            NotificationHelperDp binder = new NotificationHelperDp(callback);

            BindingOperations.SetBinding(binder, NotificationHelperDp.BindValueProperty,
#if NET || NETCORE || NETFRAMEWORK
                                         new Binding(path)
            {
                Source = vmb
            });
#else
                                         new Binding {
                Source = vmb, Path = new PropertyPath(path)
            });
#endif
            block.Add(path, binder);
        }
Example #2
0
        public void TestTryGetValue2()
        {
            var dictionary = new WeakKeyDictionary <string, int>();
            int value;

            dictionary.TryGetValue("1", out value).Should().BeFalse();
            value.Should().Be(0);

            dictionary.Add("1", 42);

            dictionary.TryGetValue("2", out value).Should().BeFalse();
            value.Should().Be(0);

            EnsureIntegrity(dictionary);
        }
Example #3
0
        public bool TryGet(object keyObject, out object inGraphObject)
        {
            Entry entry;

            if (entriesByValue.TryGetValue(keyObject, out entry))
            {
                inGraphObject = keyObject;
                TryGetCompatibleValue(entry, keyObject.GetComponentType(), ref inGraphObject);
                return(true);
            }
            else
            {
                inGraphObject = null;
                return(false);
            }
        }
Example #4
0
        /// <summary>
        /// Gets the URI of the controller that outputted the specified <see cref="IMXView"/>.
        /// </summary>
        /// <param name="view">The <see cref="IMXView"/> for which to retrieve the navigated URI.</param>
        public string GetNavigatedURI(IMXView view)
        {
            // used primarily for ShouldNavigate
            string uri = null;

            NavigatedURIs.TryGetValue(view, out uri);
            return(uri);
        }
        private T GetValueOnThread(Thread thread)
        {
            T value;

            lock (values)
                values.TryGetValue(thread, out value);
            return(value);
        }
        public void TryGetValue_ReferenceNotFound()
        {
            var dictionary = new WeakKeyDictionary <string, string>();

            bool result = dictionary.TryGetValue("x", out string v);

            Assert.False(result);
            Assert.Null(v);
            Assert.False(dictionary.ContainsKey("x"));
        }
Example #7
0
        public void TestTryGetValue1()
        {
            var dictionary = new WeakKeyDictionary <string, int>();

            dictionary.Add("1", 42);
            dictionary.Add("2", 9001);
            dictionary.Add("3", 1337);

            int value;

            dictionary.TryGetValue("1", out value).Should().BeTrue();
            value.Should().Be(42);
            dictionary.TryGetValue("2", out value).Should().BeTrue();
            value.Should().Be(9001);
            dictionary.TryGetValue("3", out value).Should().BeTrue();
            value.Should().Be(1337);

            EnsureIntegrity(dictionary);
        }
Example #8
0
        public void WeakKeyDictionary_SetUsingItem_AddsItem()
        {
            var          dictionary = new WeakKeyDictionary <DisposableTestObject, string>();
            var          key        = new DisposableTestObject("howdy");
            const string Value      = "doody";

            dictionary[key] = Value;

            string value;

            Assert.True(dictionary.TryGetValue(key, out value));
        }
Example #9
0
        public void WeakKeyDictionary_AddObjectKeyValuePair_TryGetValueSucceeds()
        {
            var    dictionary = new WeakKeyDictionary <object, object>();
            object key        = "Why, it's a Key!";
            object value      = new object();

            dictionary.Add(key, value);

            object valueOut;

            Assert.True(dictionary.TryGetValue("Why, it's a Key!", out valueOut));
            Assert.True(object.ReferenceEquals(value, valueOut));
        }
Example #10
0
        public void WeakKeyDictionary_AddKeyValue_TryGetValueSucceeds()
        {
            const int Value      = 1;
            var       key        = new DisposableTestObject("key");
            var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();

            dictionary.Add(key, Value);

            int value;

            Assert.True(dictionary.TryGetValue(key, out value));
            Assert.Equal(Value, value);
        }
Example #11
0
        public void WeakKeyDictionary_TryGetRemovedValue_ValueNotRetrieved()
        {
            using (var key = new DisposableTestObject("What? Where'd that one go?"))
            {
                const int FortyTwo   = 42;
                var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
                Assert.True(dictionary.AddEntry(key, FortyTwo));
                Assert.True(dictionary.RemoveEntry(key));

                int value;
                Assert.False(dictionary.TryGetValue(key, out value));
            }
        }
Example #12
0
        public void WeakKeyDictionary_AddEntry_TryGetValueSucceeds()
        {
            using (var key = new DisposableTestObject("Moe"))
            {
                const int FortyTwo   = 42;
                var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
                Assert.True(dictionary.AddEntry(key, FortyTwo));
                Assert.True(dictionary.ContainsKey(key));

                int value;
                Assert.True(dictionary.TryGetValue(key, out value));
                Assert.Equal(FortyTwo, value);
            }
        }
            public void ConcurrencyCheck(IContainSagaData sagaEntity, int currentVersion)
            {
                int v;

                if (!VersionCache.TryGetValue(sagaEntity, out v))
                {
                    throw new Exception(string.Format("InMemorySagaPersister in an inconsistent state: entity Id[{0}] not read.", sagaEntity.Id));
                }

                if (v != currentVersion)
                {
                    throw new Exception(string.Format("InMemorySagaPersister concurrency violation: saga entity Id[{0}] already saved.", sagaEntity.Id));
                }
            }
Example #14
0
        public IServant GetExistingOrCreateNewServant <T>(T subject) where T : class
        {
            lock (_syncRoot)
            {
                IServant servant;
                if (!_servantsBySubject.TryGetValue(subject, out servant))
                {
                    var nextId = _idGenerator.GetGrainId();
                    servant = CreateServant(nextId, subject);
                }

                return(servant);
            }
        }
Example #15
0
        public void TestTryGetValue3()
        {
            var dictionary = new WeakKeyDictionary <string, object>();

            dictionary.Version.Should().Be(0);

            object value;

            dictionary.TryGetValue("foo", out value);

            dictionary.Count.Should().Be(0);
            dictionary.Version.Should().Be(0, "Because the dictionary shouldn't have changed");

            EnsureIntegrity(dictionary);
        }
        public void TryGetValue_ReferenceFound()
        {
            string k1 = "key";
            string v1 = "value";

            var dictionary = new WeakKeyDictionary <string, string>();

            dictionary[k1] = v1;

            // Now look for the same key we inserted
            bool result = dictionary.TryGetValue(k1, out string v2);

            Assert.True(result);
            Assert.True(object.ReferenceEquals(v1, v2));
        }
Example #17
0
        public void WeakKeyDictionary_AddKeyValuePair_TryGetValueSucceeds()
        {
            const int Value          = 2;
            var       key            = new DisposableTestObject("key");
            var       keeperArounder = new List <KeyValuePair <DisposableTestObject, int> >();
            var       dictionary     = new WeakKeyDictionary <DisposableTestObject, int>();

            var entry = new KeyValuePair <DisposableTestObject, int>(key, Value);

            keeperArounder.Add(entry);
            dictionary.Add(entry);

            int value;

            Assert.True(dictionary.TryGetValue(key, out value));
            Assert.Equal(Value, value);
        }
        public void Test_WeakKeyDictionary_CRUD()
        {
            var weakKeyDictionary = new WeakKeyDictionary <TestKey, int>();

            var key = new TestKey("Yohan");

            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary.Count);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Remove(key);
            Assert.AreEqual(0, weakKeyDictionary.Count);

            Exception exception = null;

            try
            {
                weakKeyDictionary.Remove(null);
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                var i = weakKeyDictionary[new TestKey("1")];
            }
            catch (KeyNotFoundException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            key = new TestKey("Yohan2");
            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Clear();
            Assert.AreEqual(0, weakKeyDictionary.Count);

            weakKeyDictionary.Add(key, 1);
            Assert.IsTrue(weakKeyDictionary.ContainsKey(key));
            Assert.IsTrue(weakKeyDictionary.ContainsValue(1));

            weakKeyDictionary[key] = 2;
            Assert.AreEqual(2, weakKeyDictionary[key]);

            bool contains = weakKeyDictionary.ContainsValue(2);

            Assert.IsTrue(contains);

            exception = null;
            try
            {
                weakKeyDictionary[null] = 3;
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                weakKeyDictionary.Add(key, 1);
            }
            catch (ArgumentException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            int value;

            weakKeyDictionary.TryGetValue(key, out value);
            Assert.AreEqual(2, value);

            var count = weakKeyDictionary.Count;

            key = null;
            GC.Collect();

            weakKeyDictionary.Add(new TestKey("yohan9"), 2);

            Assert.AreEqual(count, weakKeyDictionary.Keys.Count);
        }
        public void Test_WeakKeyDictionary_CRUD()
        {
            var weakKeyDictionary = new WeakKeyDictionary<TestKey, int>();

            var key = new TestKey("Yohan");

            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary.Count);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Remove(key);
            Assert.AreEqual(0, weakKeyDictionary.Count);

            Exception exception = null;
            try
            {
                weakKeyDictionary.Remove(null);
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                var i = weakKeyDictionary[new TestKey("1")];
            }
            catch (KeyNotFoundException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            key = new TestKey("Yohan2");
            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Clear();
            Assert.AreEqual(0, weakKeyDictionary.Count);

            weakKeyDictionary.Add(key, 1);
            Assert.IsTrue(weakKeyDictionary.ContainsKey(key));
            Assert.IsTrue(weakKeyDictionary.ContainsValue(1));

            weakKeyDictionary[key] = 2;
            Assert.AreEqual(2, weakKeyDictionary[key]);

            bool contains = weakKeyDictionary.ContainsValue(2);
            Assert.IsTrue(contains);

            exception = null;
            try
            {
                weakKeyDictionary[null] = 3;
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                weakKeyDictionary.Add(key, 1);
            }
            catch (ArgumentException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            int value;
            weakKeyDictionary.TryGetValue(key, out value);
            Assert.AreEqual(2, value);

            var count = weakKeyDictionary.Count;
            key = null;
            GC.Collect();

            weakKeyDictionary.Add(new TestKey("yohan9"), 2);

            Assert.AreEqual(count, weakKeyDictionary.Keys.Count);
        }
Example #20
0
 public bool TryGetValue(WeakKeyDictionary <object, object, object, int, object> d, Tuple <object, object, object, int> k, out object v)
 {
     return(d.TryGetValue(k.Item1, k.Item2, k.Item3, k.Item4, out v));
 }