예제 #1
0
        /// <summary>
        /// Clones the mapping of another resource.
        /// </summary>
        /// <param name="other"></param>
        void IPropertyMapping.CloneFrom(IPropertyMapping other)
        {
            if (_dataType != other.DataType)
            {
                return;
            }

            if (_value != null && _isList)
            {
                IList collection = (IList)_value;

                collection.Clear();

                IList otherCollection = (IList)other.GetValueObject();

                foreach (var v in otherCollection)
                {
                    collection.Add(v);
                }

                _isUnsetValue = other.IsUnsetValue;
            }
            else
            {
                _value        = (T)other.GetValueObject();
                _isUnsetValue = other.IsUnsetValue;
            }
        }
예제 #2
0
        private void TransferMappingToProperties(IPropertyMapping mapping)
        {
            if (!_properties.ContainsKey(mapping.Property))
            {
                _properties.Add(mapping.Property, new HashSet <object>());
            }

            if (mapping.IsList)
            {
                foreach (var x in mapping.GetValueObject() as IList)
                {
                    _properties[mapping.Property].Add(x);
                }
            }
            else
            {
                _properties[mapping.Property].Add(mapping.GetValueObject());
            }
        }
예제 #3
0
        /// <summary>
        /// This method loads the cached Resources for the given MappingProperty from the Storage and returns them.
        /// They are instantiated as the defined type. The cache for this mapping property is emptied.
        /// </summary>
        /// <param name="mapping">Mapping property which should be loaded from cache.</param>
        /// <returns>List of formerly cached resources.</returns>
        public void LoadCachedValues(IPropertyMapping mapping)
        {
            if (!Cache.ContainsKey(mapping))
            {
                return;
            }

            Type baseType = (mapping.IsList) ? mapping.GenericType : mapping.DataType;

            List <Uri> cachedUris = Cache[mapping];

            if (!mapping.IsList && cachedUris.Count > 1)
            {
                throw new Exception(string.Format("An error occured while loading the cached resources for property {0}. Found {1} elements but it is mapped to a non-list property. Try to map to a list of objects.", mapping.PropertyName, cachedUris.Count));
            }

            foreach (Uri u in cachedUris)
            {
                object r = null;

                if (Model == null)
                {
                    Debugger.Break();
                }

                if (Model.ContainsResource(u))
                {
                    r = Model.GetResource(u, baseType);
                }
                else
                {
                    r = Activator.CreateInstance(baseType, u);
                }

                if (mapping.IsList)
                {
                    IList l = mapping.GetValueObject() as IList;

                    if (l != null)
                    {
                        l.Add(r);
                    }
                }
                else
                {
                    mapping.SetOrAddMappedValue(r);
                }
            }

            Cache.Remove(mapping);
        }
예제 #4
0
        /// <summary>
        /// This method loads the cached Resources for the given MappingProperty from the Storage and returns them.
        /// They are instantiated as the defined type. The cache for this mapping property is emptied.
        /// </summary>
        /// <param name="mapping">Mapping property which should be loaded from cache.</param>
        /// <returns>List of formerly cached resources.</returns>
        public void LoadCachedValues(IPropertyMapping mapping)
        {
            if (!Cache.ContainsKey(mapping))
            {
                return;
            }

            Type baseType = (mapping.IsList) ? mapping.GenericType : mapping.DataType;

            HashSet <Uri> cachedUris = Cache[mapping];

            if (!mapping.IsList && cachedUris.Count > 1)
            {
                throw new Exception(string.Format("An error occured while loading the cached resources for property {0}. Found {1} elements but it is mapped to a non-list property. Try to map to a list of objects.", mapping.PropertyName, cachedUris.Count));
            }

            foreach (Uri uri in cachedUris)
            {
                object resource = null;

                #if DEBUG
                if (Model == null)
                {
                    Debugger.Break();
                }
                #endif

                if (Model.ContainsResource(uri))
                {
                    resource = Model.GetResource(uri, baseType);
                }
                else
                {
                    resource = Activator.CreateInstance(baseType, uri);
                }

                if (mapping.IsList)
                {
                    // Getting the reference to the mapped list object
                    IList list = mapping.GetValueObject() as IList;

                    if (list != null)
                    {
                        // Make sure the resource exits only one time
                        if (list.Contains(resource))
                        {
                            list.Remove(resource);
                        }

                        // Add ther resource to the mapped list
                        list.Add(resource);
                    }
                }
                else
                {
                    mapping.SetOrAddMappedValue(resource);
                }
            }

            Cache.Remove(mapping);
        }