예제 #1
0
        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;
            }
        }
        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;
                }
            }
        }