Ejemplo n.º 1
0
        /// <summary>
        /// Adds a new object to the DataObjects dictionary using the Namespace property as the key.
        /// It also reassigns the parent data object if it has not been assigned.
        /// </summary>
        /// <param name="childDataObject">The child data object to add.</param>
        public void Add(TC childDataObject)
        {
            if (childDataObject == null)
            {
                throw new Exception("The child data object should never be null.");
            }

            if (!childDataObject.CanBeAssignedParentMetaObject(ParentDataObject))
            {
                throw new Exception(string.Format(InvalidParentTypeErrorMessage,
                                                  childDataObject.Description, childDataObject.Namespace, ParentDataObject.Description, ParentDataObject.Namespace));
            }

            var tempParentDataObject = childDataObject.ParentMetaObject;

            childDataObject.ParentMetaObject = ParentDataObject;
            if (ChildDataObjects.ContainsKey(childDataObject.Namespace))
            {
                var errorMessage = string.Format(ObjectAlreadyExistsErrorMessage,
                                                 ParentDataObject.Description, ParentDataObject.Namespace, childDataObject.Description, childDataObject.Namespace);
                childDataObject.ParentMetaObject = tempParentDataObject;
                throw new Exception(errorMessage);
            }

            if (!ChildDataObjects.TryAdd(childDataObject.Namespace, childDataObject))
            {
                throw new Exception(string.Format(ObjectAlreadyExistsErrorMessage,
                                                  ParentDataObject.Description, ParentDataObject.Namespace, childDataObject.Description, childDataObject.Namespace));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Specific constructor to initialize with a data object lookup or any IEnumerable collection of type T.
        /// </summary>
        /// <param name="parentDataObject">The parent data objects to initalize with.</param>
        /// <param name="childDataObjects">The chidl data objects dictionary to initalize with.</param>
        public DataObjectLookup(TP parentDataObject, IEnumerable <TC> childDataObjects)
        {
            Init(parentDataObject, null);
            foreach (var childDataObject in childDataObjects)
            {
                if (!ChildDataObjects.TryAdd(childDataObject.Namespace, childDataObject))
                {
                    throw new Exception(string.Format(ObjectAlreadyExistsErrorMessage,
                                                      ParentDataObject.Description, ParentDataObject.Namespace, childDataObject.Description, childDataObject.Namespace));
                }
            }

            _lastUsedDataObject = _childDataObjects.Count > 0
                ? _childDataObjects.First().Value
                : null;
        }