internal override void Fetch(Element element, object target, TypeCache typeCache, Cache cache) { if (target == null) return; // Get all values first so if something goes wrong we haven't started modifying the object List<KeyValuePair<PersistentMemberInfo, object>> values = new List<KeyValuePair<PersistentMemberInfo, object>>(); foreach (PersistentMemberInfo memberInfo in typeCache.PersistentMemberInfo.Where(m => m.Attribute.Fetch)) { object member = memberInfo.GetValue(target); TypeCache memberTypeCache = cache.GetTypeCache(memberInfo.Type); object value = memberInfo.Attribute.FetchValue(element, member, memberTypeCache, cache); if (memberInfo.Attribute.Required && value == null) throw new Exception("Could not get required member."); values.Add(new KeyValuePair<PersistentMemberInfo, object>(memberInfo, value)); } // Now that all conversions have been succesfully performed, set the values foreach (KeyValuePair<PersistentMemberInfo, object> value in values) { value.Key.SetValue(target, value.Value); } // Call any custom logic if implemented ICustomFetch custom = target as ICustomFetch; if(custom != null) { custom.Fetch(element); } }
// This just does both steps in one operation and is the primary entry point for store operations internal void Store(Element element, object source, TypeCache typeCache, Cache cache) { using (new Updates()) { Store(element, Serialize(source, typeCache, cache), source, typeCache, cache); } }
internal override object FetchValue(Element element, object target, TypeCache typeCache, Cache cache) { Node.Attribute attribute; if (!GetNodeFromQuery(Query, null, element, out attribute)) { attribute = element.Attribute(Name); } return attribute == null ? null : GetObjectFromString(attribute.Value, Default, target, typeCache.Type, _typeConverter); }
internal override object FetchValue(Element element, object target, TypeCache typeCache, Cache cache) { Text child; if (!GetNodeFromQuery(Query, null, element, out child)) { child = element.Children.OfType<Text>().FirstOrDefault(); } return child == null ? null : GetObjectFromString(child.Value, Default, target, typeCache.Type, _typeConverter); }
internal override void Fetch(Element element, object target, TypeCache typeCache, Cache cache) { if (target == null) return; // Deserialize the object XmlSerializer serializer = new XmlSerializer(typeCache.Type); object deserialized; using(TextReader reader = new StringReader(element.OuterXml)) { deserialized = serializer.Deserialize(reader); } // Deep copy the deserialized object to the target object foreach(FieldInfo field in typeCache.Fields) { object value = field.GetValue(deserialized); field.SetValue(target, value); } }
internal override void Store(Element element, object serialized, object source, TypeCache typeCache, Cache cache) { if (source == null || serialized == null) return; ((PersistentCollection)source).Store(element, serialized, cache); }
internal override void Fetch(Element element, object target, TypeCache typeCache, Cache cache) { if (target == null) return; ((PersistentCollection)target).Fetch(element, cache); }
internal override object FetchValue(Element element, object target, TypeCache typeCache, Cache cache) { object result = element.EvalSingle(Query); if (result == null) return null; Node.Node node = result as Node.Node; return GetObjectFromString( node != null ? node.Value : result.ToString(), Default, target, typeCache.Type, _typeConverter); }
internal override void StoreValue(Element element, object serialized, object source, TypeCache typeCache, Cache cache) { Node.Node node = element.EvalSingle(Query) as Node.Node; string value = (string) serialized; if (node == null && value != null && !String.IsNullOrEmpty(CreateQuery)) { element.Eval(CreateQuery); node = element.EvalSingle(Query) as Node.Node; } else if (node != null && value == null) { node.Remove(); return; } if (node != null) { node.Value = value; } }
internal DomElement(Element node) : base(node.Prefix, node.LocalName, node.NamespaceUri, (XmlDocument)node.Document.XmlNode) { _node = node; }
internal override void StoreValue(Element element, object serialized, object source, TypeCache typeCache, Cache cache) { Node.Attribute attribute; if (!GetNodeFromQuery(Query, CreateQuery, element, out attribute)) { attribute = element.Attribute(Name); } else if (attribute == null) { return; } string value = (string) serialized; if (attribute == null && value != null) { element.InsertAttribute(Name, value); } else if (attribute != null && value == null) { attribute.Remove(); } else if (attribute != null) { attribute.Value = value; } }
internal virtual void Store(Element element, object serialized, object source, TypeCache typeCache, Cache cache) { Store(element, serialized, source); }
internal virtual void Fetch(Element element, object target, TypeCache typeCache, Cache cache) { Fetch(element, target); }
// This scans over all instance fields in the object and uses a TypeConverter to convert them to string // If any fields cannot be converted an exception is thrown because the entire state was not stored internal override void Store(Element element, object serialized, object source, TypeCache typeCache, Cache cache) { if (source == null || serialized == null) return; // Custom store List<SerializedValue> values = (List<SerializedValue>) serialized; ICustomStore custom = source as ICustomStore; if (custom != null && values.Count > 0) { // The custom serialized value is always first custom.Store(element, values[0]); values.RemoveAt(0); } // Store the members foreach (SerializedValue value in values) { value.PersistentMemberAttribute.StoreValue(element, value.Serialized, value.Member, value.MemberTypeCache, cache); } }
internal void Fetch(Element element, Cache cache) { Initialize(cache); Collection = _attribute.FetchValue(element, Collection, cache.GetTypeCache(_type), cache); }
// Returns the first object in the cache for the specified element (or null if none exists) // Also cleans the cache of disposed objects public object FindObject(Element element) { HashSet<ObjectWrapper> wrappers; if (_elementToWrappers.TryGetValue(element, out wrappers)) { // Try to get an active instance foreach (ObjectWrapper wrapper in wrappers) { object obj = wrapper.Object; if (obj != null) { return obj; } } } return null; }
internal override void Store(Element element, object serialized, object source, TypeCache typeCache, Cache cache) { if (source == null || serialized == null) return; // Replace the element content in the database with the new content using (TextReader textReader = new StringReader((string)serialized)) { using(XmlReader reader = XmlReader.Create(textReader)) { // Move to the root element reader.MoveToContent(); // Replace all attributes element.RemoveAllAttributes(); if(reader.HasAttributes) { while(reader.MoveToNextAttribute()) { element.InsertAttribute(reader.Name, reader.Value); } reader.MoveToElement(); } // Replace the child content // Need to use an intermediate string since there is no way to // get the "inner XML" of an XmlReader without getting the // parent element too element.InnerXml = reader.ReadInnerXml(); } } }
internal override object FetchValue(Element element, object target, TypeCache typeCache, Cache cache) { // Get the primary element Element child; if (!GetNodeFromQuery(Query, null, element, out child)) { child = element.Children.OfType<Element>().Where(e => e.Name.Equals(Name)).FirstOrDefault(); } if (child == null) { return null; } // Get all the item nodes IList<object> items = new List<object>(!String.IsNullOrEmpty(ItemQuery) ? child.Eval(ItemQuery) : child.Children.OfType<Element>().Where(e => e.Name.Equals(ItemName)).Cast<object>()); // Create the collection (or reuse it if appropriate) object collection = _getCollection(target, items.Count); // Populate with values int c = 0; foreach(object item in items) { if(ItemsArePersistentObjects) { // Get, attach, and fetch the persistent object instance Element itemElement = item as Element; if (itemElement == null) throw new Exception("Persistent value node must be an element."); object itemObject = cache.GetObject(_itemTypeCache, itemElement, AttachItems); if(itemObject != null) _setCollectionItem(collection, c++, itemObject); } else { Node.Node itemNode = item as Node.Node; object itemObject = GetObjectFromString( itemNode != null ? itemNode.Value : item.ToString(), null, null, _itemTypeCache.Type, _itemTypeConverter); if (itemObject != null) _setCollectionItem(collection, c++, itemObject); } } return collection; }
private object FetchValue(Element item, string attributeName, string elementName, string query, bool arePersistentObjects, bool attach, TypeCache typeCache, TypeConverter typeConverter, Cache cache) { object value = null; if (!String.IsNullOrEmpty(attributeName)) { Node.Attribute attribute = item.Attribute(attributeName); value = attribute == null ? null : GetObjectFromString(attribute.Value, null, null, typeCache.Type, typeConverter); } else { object result = !String.IsNullOrEmpty(query) ? item.EvalSingle(query) : item.Children.OfType<Element>().FirstOrDefault(e => e.Name.Equals(elementName)); if (arePersistentObjects) { // Get, attach, and fetch the persistent object instance Element element = result as Element; if (element == null) throw new Exception("Persistent value node must be an element."); value = cache.GetObject(typeCache, element, attach); } else { Node.Node itemNode = result as Node.Node; value = GetObjectFromString(itemNode != null ? itemNode.Value : result == null ? null : result.ToString(), null, null, typeCache.Type, typeConverter); } } return value; }
internal override void StoreValue(Element element, object serialized, object source, TypeCache typeCache, Cache cache) { if (!String.IsNullOrEmpty(ItemQuery)) throw new Exception("Can not store persistent collection that uses queries."); // Get the child element Element child; if (!GetNodeFromQuery(Query, CreateQuery, element, out child)) { child = element.Children.OfType<Element>().Where(e => e.Name.Equals(Name)).FirstOrDefault(); } else if (child == null) { return; } // Create or remove the child if needed if (child == null && serialized != null) { element.Append(new Element(Name)); child = (Element)element.Children.Last(); } else if (child != null && serialized == null) { child.Remove(); return; } else if (child == null) { return; } // Remove all elements with the item name List<Element> removeElements = child.Children.OfType<Element>() .Where(e => e.Name == ItemName).ToList(); foreach (Element removeElement in removeElements) { removeElement.Remove(); } // Store all items foreach (KeyValuePair<object, object> kvp in (IEnumerable<KeyValuePair<object, object>>) serialized) { child.Append(new Element(ItemName)); Element itemElement = (Element) child.Children.Last(); if (ItemsArePersistentObjects) { _itemTypeCache.Persister.Store(itemElement, kvp.Value, kvp.Key, _itemTypeCache, cache); } else { itemElement.Value = (string) kvp.Value; } } }
/// <summary> /// Fetches data for the specified object from the specified element. /// </summary> /// <param name="element">The element to fetch data from.</param> /// <param name="target">The object to fetch data for.</param> protected virtual void Fetch(Element element, object target) { }
internal override void StoreValue(Element element, object serialized, object source, TypeCache typeCache, Cache cache) { Text child; if (!GetNodeFromQuery(Query, CreateQuery, element, out child)) { child = element.Children.OfType<Text>().FirstOrDefault(); } else if (child == null) { return; } string value = (string) serialized; if (child == null && value != null) { element.Append(new Text(value)); } else if (child != null && value == null) { child.Remove(); } else if (child != null) { child.Value = value; } }
/// <summary> /// Stores data for the specified object to the specified element. /// </summary> /// <param name="element">The element to store data to.</param> /// <param name="serialized">The serialized object returned by Serialize.</param> /// <param name="source">The object to store data for.</param> protected virtual void Store(Element element, object serialized, object source) { }
internal override object FetchValue(Element element, object target, TypeCache typeCache, Cache cache) { // Get the primary element Element child; if (!GetNodeFromQuery(Query, null, element, out child)) { child = element.Children.OfType<Element>().Where(e => e.Name.Equals(Name)).FirstOrDefault(); } if (child == null) { return null; } // Get all the item nodes (this will throw an exception on Cast<>() if an ItemQuery returns other than Element nodes) IList<Element> items = new List<Element>(!String.IsNullOrEmpty(ItemQuery) ? child.Eval(ItemQuery).Cast<Element>() : child.Children.OfType<Element>().Where(e => e.Name.Equals(ItemName))); // Create the collection object collection = _getCollection(target); // Populate with values foreach(Element item in items) { object keyValue = FetchValue(item, KeyAttributeName, KeyElementName, KeyQuery, KeysArePersistentObjects, AttachKeys, _keyTypeCache, _keyTypeConverter, cache); object valueValue = FetchValue(item, ValueAttributeName, ValueElementName, ValueQuery, ValuesArePersistentObjects, AttachValues, _valueTypeCache, _valueTypeConverter, cache); if(keyValue != null && valueValue != null) _setCollectionItem(collection, keyValue, valueValue); } return collection; }
internal void Store(Element element, object serialized, Cache cache) { Initialize(cache); _attribute.StoreValue(element, serialized, Collection, cache.GetTypeCache(_type), cache); }