Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
 // 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);
     }
 }
Ejemplo n.º 3
0
        internal override object Serialize(object source, TypeCache typeCache, Cache cache)
        {
            if (source == null) return null;

            // Serialize the object
            // Use an empty namespace object to prevent default namespace declarations
            XmlSerializer serializer = new XmlSerializer(typeCache.Type);
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(String.Empty, String.Empty);
            StringBuilder content = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(content, _writerSettings))
            {
                serializer.Serialize(writer, source, namespaces);
            }
            return content.ToString();
        }
Ejemplo n.º 4
0
        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);
            }
        }
Ejemplo n.º 5
0
        internal override object Serialize(object source, TypeCache typeCache, Cache cache)
        {
            if (source == null) return null;

            List<SerializedValue> values = new List<SerializedValue>();

            // Do custom serialization
            ICustomStore custom = source as ICustomStore;
            if(custom != null)
            {
                values.Add(new SerializedValue(null, custom.Serialize(), null, null));
            }

            // Serialize the members
            foreach (PersistentMemberInfo memberInfo
                in typeCache.PersistentMemberInfo.Where(m => m.Attribute.Store))
            {
                object member = memberInfo.GetValue(source);
                TypeCache memberTypeCache = cache.GetTypeCache(memberInfo.Type);
                object serialized = memberInfo.Attribute.SerializeValue(member, memberTypeCache, cache);
                values.Add(new SerializedValue(memberInfo.Attribute, serialized, member, memberTypeCache));
            }
            return values;
        }
Ejemplo n.º 6
0
 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);
 }
Ejemplo n.º 7
0
 internal override object Serialize(object source, TypeCache typeCache, Cache cache)
 {
     if (source == null) return null;
     return ((PersistentCollection)source).Serialize(cache);
 }
Ejemplo n.º 8
0
 internal override void Fetch(Element element, object target, TypeCache typeCache, Cache cache)
 {
     if (target == null) return;
     ((PersistentCollection)target).Fetch(element, cache);
 }
Ejemplo n.º 9
0
 internal virtual void Store(Element element, object serialized, object source, TypeCache typeCache, Cache cache)
 {
     Store(element, serialized, source);
 }
Ejemplo n.º 10
0
 internal virtual object Serialize(object source, TypeCache typeCache, Cache cache)
 {
     return Serialize(source);
 }
Ejemplo n.º 11
0
 internal virtual void Fetch(Element element, object target, TypeCache typeCache, Cache cache)
 {
     Fetch(element, target);
 }
Ejemplo n.º 12
0
        // 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);
            }
        }
Ejemplo n.º 13
0
 public SerializedValue(PersistentMemberAttribute persistentMemberAttribute,
     object serialized, object member, TypeCache memberTypeCache)
 {
     PersistentMemberAttribute = persistentMemberAttribute;
     Serialized = serialized;
     Member = member;
     MemberTypeCache = memberTypeCache;
 }
Ejemplo n.º 14
0
        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();
                }
            }
        }