Exemplo n.º 1
0
        public void Add()
        {
            var person = new Person("Adult1", 50);

            _updater.AddOrUpdate(person, "Adult1");
            IChangeSet <Person, string> updates = _updater.AsChangeSet();

            Assert.AreEqual(person, _cache.Lookup("Adult1").Value);
            Assert.AreEqual(1, _cache.Count);
            Assert.AreEqual(updates.Count, 1, "Should be 1 updates");
            Assert.AreEqual(new Change <Person, string>(ChangeReason.Add, person.Name, person), updates.First(),
                            "Should be 1 updates");
        }
Exemplo n.º 2
0
        private IChangeSet <TObject, TKey> UpdateCombined(IChangeSet <TObject, TKey> updates)
        {
            //child caches have been updated before we reached this point.
            var updater = new IntermediateUpdater <TObject, TKey>(_combinedCache);

            foreach (var update in updates)
            {
                TKey key = update.Key;
                switch (update.Reason)
                {
                case ChangeReason.Add:
                case ChangeReason.Update:

                {
                    // get the current key.
                    //check whether the item should belong to the cache
                    Optional <TObject> cached = updater.Lookup(key);
                    bool contained            = cached.HasValue;

                    bool match = MatchesConstraint(key);

                    if (match)
                    {
                        if (contained)
                        {
                            if (!ReferenceEquals(update.Current, cached.Value))
                            {
                                updater.AddOrUpdate(update.Current, key);
                            }
                        }
                        else
                        {
                            updater.AddOrUpdate(update.Current, key);
                        }
                    }
                    else
                    {
                        if (contained)
                        {
                            updater.Remove(key);
                        }
                    }
                }
                break;

                case ChangeReason.Remove:
                {
                    var  cached           = updater.Lookup(key);
                    var  contained        = cached.HasValue;
                    bool shouldBeIncluded = MatchesConstraint(key);

                    if (shouldBeIncluded)
                    {
                        var firstOne = _sourceCaches.Select(s => s.Lookup(key))
                                       .SelectValues()
                                       .First();

                        if (!cached.HasValue)
                        {
                            updater.AddOrUpdate(firstOne, key);
                        }
                        else if (!ReferenceEquals(firstOne, cached.Value))
                        {
                            updater.AddOrUpdate(firstOne, key);
                        }
                    }
                    else
                    {
                        if (contained)
                        {
                            updater.Remove(key);
                        }
                    }
                }


                break;

                case ChangeReason.Evaluate:
                {
                    updater.Evaluate(key);
                }

                break;
                }
            }
            return(updater.AsChangeSet());

            // }
            // return up
        }