public static void AddRangeAndCheckItems(this IDomainObjectCollectionData data, IEnumerable <DomainObject> domainObjects, Type requiredItemType)
        {
            ArgumentUtility.CheckNotNull("data", data);
            ArgumentUtility.CheckNotNull("domainObjects", domainObjects);

            var index = 0;

            foreach (var domainObject in domainObjects)
            {
                if (domainObject == null)
                {
                    throw ArgumentUtility.CreateArgumentItemNullException("domainObjects", index);
                }
                if (requiredItemType != null && !requiredItemType.IsInstanceOfType(domainObject))
                {
                    throw ArgumentUtility.CreateArgumentItemTypeException("domainObjects", index, requiredItemType, domainObject.ID.ClassDefinition.ClassType);
                }
                if (data.ContainsObjectID(domainObject.ID))
                {
                    throw new ArgumentException(
                              string.Format("Item {1} of parameter '{0}' is a duplicate ('{2}').", "domainObjects", index, domainObject.ID),
                              "domainObjects");
                }

                data.Add(domainObject);

                ++index;
            }
        }
Esempio n. 2
0
        public void Sort_InvalidatesCache()
        {
            var secondDomainObject = DomainObjectMother.CreateFakeObject <Order> ();

            _wrappedData.Add(secondDomainObject);

            WarmUpCache(_decoratorWithRealData, false);
            Assert.That(_decoratorWithRealData, Is.EqualTo(new[] { _domainObject, secondDomainObject }));
            Assert.That(_decoratorWithRealData.IsCacheUpToDate, Is.True);

            var weights = new Dictionary <DomainObject, int> {
                { _domainObject, 2 }, { secondDomainObject, 1 }
            };
            Comparison <DomainObject> comparison = (one, two) => weights[one].CompareTo(weights[two]);

            _decoratorWithRealData.Sort(comparison);

            Assert.That(_decoratorWithRealData, Is.EqualTo(new[] { secondDomainObject, _domainObject }));
            Assert.That(_decoratorWithRealData.IsCacheUpToDate, Is.False);
        }
Esempio n. 3
0
        /// <summary>
        /// Registers the given <paramref name="domainObject"/> as an original item of this collection. This means the item is added to the
        /// <see cref="OriginalData"/> collection, and it is also added to this <see cref="ChangeCachingCollectionDataDecorator"/> collection. If the
        /// <see cref="OriginalData"/> collection already contains the item, an exception is thrown. If this collection already contains the item, it is
        /// only added to the <see cref="OriginalData"/>. This operation may invalidate the state cache.
        /// </summary>
        /// <param name="domainObject">The <see cref="DomainObject"/> to be registered.</param>
        public void RegisterOriginalItem(DomainObject domainObject)
        {
            ArgumentUtility.CheckNotNull("domainObject", domainObject);

            // Original collection must not contain this item
            if (_originalData.ContainsObjectID(domainObject.ID))
            {
                var message = string.Format("The original collection already contains a domain object with ID '{0}'.", domainObject.ID);
                throw new InvalidOperationException(message);
            }

            // Check if this collection does not contain the item
            if (!_unobservedWrappedData.ContainsObjectID(domainObject.ID))
            {
                // Standard case: Neither collection contains the item; the item is added to both, and the state cache stays valid

                // Add the item to the unobserved inner collection to avoid copy on write: if the contents hasn't been copied, we want to modify both
                // collections at the same time!
                // This way, if the original collection has not yet been copied, it will automatically contain the  item and the state cache remains valid.
                _unobservedWrappedData.Add(domainObject);

                // If the original collection has already been copied, we must add the item manually. The state cache still remains valid because we always add
                // the item at the end. If the collections were equal before, they remain equal now. If they were different before, they remain different.
                if (_originalData.IsContentsCopied)
                {
                    _originalData.Add(domainObject);
                }
            }
            else
            {
                // Special case: The current collection already contains the item

                // We must add the item to the original collection only and raise a potential state change notification
                _originalData.Add(domainObject);
                OnChangeStateUnclear();
            }

            Assertion.IsTrue(ContainsObjectID(domainObject.ID));
            Assertion.IsTrue(_originalData.ContainsObjectID(domainObject.ID));
        }