예제 #1
0
        public virtual void AddPropertyToMapping(Property property, object value, bool fromModel)
        {
            if (value == null)
            {
                return;
            }

            if (property.Uri.OriginalString == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
            {
                Uri uri;

                if (value is IResource)
                {
                    uri = (value as IResource).Uri;
                }
                else if (value is Uri)
                {
                    uri = value as Uri;
                }
                else
                {
                    return;
                }

                if (GetTypes().Any(t => t.Uri == uri))
                {
                    return;
                }
            }

            IPropertyMapping propertyMapping = GetPropertyMapping(property, value.GetType());

            if (propertyMapping != null)
            {
                // yes, so we try to set or add it
                if (fromModel && value is IResource && propertyMapping.DataType != typeof(Uri) && propertyMapping.GenericType != typeof(Uri))
                {
                    // we generate the resource from the model, so we cache all mapped resources
                    ResourceCache.CacheValue(propertyMapping, (value as IResource).Uri);
                }
                else
                {
                    propertyMapping.SetOrAddMappedValue(value);
                }
            }
            // no, we add it to the property
            else if (_properties.ContainsKey(property))
            {
                _properties[property].Add(value);
            }
            else
            {
                _properties.Add(property, new HashSet <object>()
                {
                    value
                });
            }
        }
예제 #2
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);
        }
예제 #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;

            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);
        }