예제 #1
0
        public static void LoadObjectDefaults(IDefaultsHolder defaultsHolder)
        {
            Type t = defaultsHolder.GetType();

            if (BusinessObject.ClassXmlSerializationCache.ContainsKey(t))
            {
                XmlSerializationCache classCache = BusinessObject.ClassXmlSerializationCache[t];

                XElement defaultSettings = defaultsHolder.DefaultsXml.Root.Element(classCache.Attribute.XmlField);

                XmlSerializationCache[] cache = BusinessObject.PropertiesXmlSerializationCache[t];

                for (int i = 0; i < cache.Length; i++)
                {
                    XmlSerializationCache c = cache[i];

                    XmlSerializableAttribute propAttr = c.Attribute;
                    if (!propAttr.UseAttribute && defaultSettings.Element(propAttr.XmlField) != null ||
                        propAttr.UseAttribute && defaultSettings.Attribute(propAttr.XmlField) != null) //zeby nie nullowal juz ustawionych ustawien
                    {
                        BusinessObjectHelper.DeserializeSingleValue(defaultsHolder, c.Property, propAttr, defaultSettings);
                    }
                }
            }
        }
예제 #2
0
        public static string XmlSerializeCodeGen <T>(T obj)
        {
            Func <T, string> serializer = XmlSerializationCache <T> .Serializer;

            if (serializer == null)
            {
                serializer = XmlSerializationCache <T> .GenerateSerializer();
            }
            return(serializer(obj));
        }
예제 #3
0
        public virtual XElement Serialize(bool selfOnly)
        {
            XElement returnVal = null;

            Type t = this.GetType();

            if (BusinessObject.ClassXmlSerializationCache.ContainsKey(t))
            {
                XmlSerializationCache classCache = BusinessObject.ClassXmlSerializationCache[t];

                returnVal = new XElement(classCache.Attribute.XmlField);

                if (this.BOType != BusinessObjectType.Other)
                {
                    returnVal.Add(new XAttribute("type", this.BOType.ToString()));
                }

                XmlSerializationCache[] cache = BusinessObject.PropertiesXmlSerializationCache[t];

                for (int i = 0; i < cache.Length; i++)
                {
                    XmlSerializationCache c = cache[i];

                    object propertyValue = c.Property.GetValue(this, null);

                    if (propertyValue != null)
                    {
                        XObject element = BusinessObjectHelper.SerializeSingleValue(c.Property.PropertyType,
                                                                                    c.Attribute.XmlField,
                                                                                    propertyValue,
                                                                                    false,
                                                                                    c.Attribute.UseAttribute,
                                                                                    c.Attribute.SelfOnlySerialization, selfOnly);

                        if (String.IsNullOrEmpty(c.Attribute.EncapsulatingXmlField))
                        {
                            returnVal.Add(element);
                        }
                        else
                        {
                            if (returnVal.Element(c.Attribute.EncapsulatingXmlField) != null)
                            {
                                returnVal.Element(c.Attribute.EncapsulatingXmlField).Add(element);
                            }
                            else
                            {
                                returnVal.Add(new XElement(c.Attribute.EncapsulatingXmlField, element));
                            }
                        }
                    }
                }
            }

            return(returnVal);
        }
예제 #4
0
        /// <summary>
        /// Recursively creates new children (BusinessObjects) and loads settings from provided xml.
        /// </summary>
        /// <param name="element">Xml element to attach.</param>
        public virtual void Deserialize(XElement element)
        {
            Type t = this.GetType();

            if (!BusinessObject.PropertiesXmlSerializationCache.ContainsKey(t))
            {
                BusinessObject.CacheClass(t);
            }

            XmlSerializationCache[] cache = BusinessObject.PropertiesXmlSerializationCache[t];

            for (int i = 0; i < cache.Length; i++)
            {
                XmlSerializationCache c = cache[i];

                if (c.Attribute.AutoDeserialization)
                {
                    BusinessObjectHelper.DeserializeSingleValue(this, c.Property, c.Attribute, element);
                }
            }
        }
예제 #5
0
        private static void CacheClass(Type t)
        {
            if (!BusinessObject.IsClassCached.ContainsKey(t))
            {
                bool isObjectToCache = false;

                // get the class attributes
                object[] obj = t.GetCustomAttributes(typeof(XmlSerializableAttribute), true);

                if (obj != null && obj.Length == 1)
                {
                    XmlSerializableAttribute attr = (XmlSerializableAttribute)obj[0];
                    BusinessObject.ClassXmlSerializationCache.Add(t, new XmlSerializationCache()
                    {
                        Attribute = attr
                    });
                    isObjectToCache = true;
                }

                obj = t.GetCustomAttributes(typeof(DatabaseMappingAttribute), true);

                if (obj != null && obj.Length > 0)
                {
                    DatabaseMappingCache[] cache = new DatabaseMappingCache[obj.Length];

                    for (int i = 0; i < obj.Length; i++)
                    {
                        DatabaseMappingAttribute attr = (DatabaseMappingAttribute)obj[i];
                        cache[i] = new DatabaseMappingCache()
                        {
                            Attribute = attr
                        };
                    }

                    BusinessObject.ClassDatabaseMappingCache.Add(t, cache);
                    isObjectToCache = true;
                }

                if (isObjectToCache)
                {
                    //get properties XmlSerializableAttribiute
                    LinkedList <XmlSerializationCache> xmlSerializableCacheList = new LinkedList <XmlSerializationCache>();

                    foreach (PropertyInfo propertyInfo in t.GetProperties())
                    {
                        obj = propertyInfo.GetCustomAttributes(typeof(XmlSerializableAttribute), true);

                        if (obj != null && obj.Length == 1)
                        {
                            XmlSerializableAttribute attr  = (XmlSerializableAttribute)obj[0];
                            XmlSerializationCache    cache = new XmlSerializationCache()
                            {
                                Attribute = attr, Property = propertyInfo
                            };

                            if (attr.ProcessLast)
                            {
                                xmlSerializableCacheList.AddLast(cache);
                            }
                            else
                            {
                                xmlSerializableCacheList.AddFirst(cache);
                            }
                        }
                    }

                    XmlSerializationCache[] xmlCache = new XmlSerializationCache[xmlSerializableCacheList.Count];

                    int u = 0;

                    foreach (XmlSerializationCache c in xmlSerializableCacheList)
                    {
                        xmlCache[u++] = c;
                    }

                    BusinessObject.PropertiesXmlSerializationCache.Add(t, xmlCache);

                    //get properties ComparableAttribiute
                    List <ComparableCache> comparableCacheList = new List <ComparableCache>();

                    foreach (PropertyInfo propertyInfo in t.GetProperties())
                    {
                        obj = propertyInfo.GetCustomAttributes(typeof(ComparableAttribute), true);

                        if (obj != null && obj.Length == 1)
                        {
                            ComparableAttribute attr  = (ComparableAttribute)obj[0];
                            ComparableCache     cache = new ComparableCache()
                            {
                                Attribute = attr, Property = propertyInfo
                            };
                            comparableCacheList.Add(cache);
                        }
                    }

                    BusinessObject.PropertiesComparableCache.Add(t, comparableCacheList.ToArray());

                    //get properties DatabaseMappingCache
                    List <DatabaseMappingCache> databaseMappingCacheList = new List <DatabaseMappingCache>();

                    foreach (PropertyInfo propertyInfo in t.GetProperties())
                    {
                        obj = propertyInfo.GetCustomAttributes(typeof(DatabaseMappingAttribute), true);

                        if (obj != null && obj.Length > 0)
                        {
                            foreach (object objAttr in obj)
                            {
                                DatabaseMappingAttribute attr  = (DatabaseMappingAttribute)objAttr;
                                DatabaseMappingCache     cache = new DatabaseMappingCache()
                                {
                                    Attribute = attr, Property = propertyInfo
                                };
                                databaseMappingCacheList.Add(cache);
                            }
                        }
                    }

                    BusinessObject.PropertiesDatabaseMappingCache.Add(t, databaseMappingCacheList.ToArray());

                    BusinessObject.IsClassCached.Add(t, true);
                }
            }
        }
예제 #6
0
        public static void DeserializeSingleValue(object target, PropertyInfo propertyInfo, XmlSerializableAttribute attribute, XElement boElement)
        {
            Type type = propertyInfo.PropertyType;

            if (!String.IsNullOrEmpty(attribute.EncapsulatingXmlField))
            {
                boElement = boElement.Element(attribute.EncapsulatingXmlField);
            }

            /*if (boElement == null ||
             *  (!attribute.UseAttribute && attribute.XmlField != null && boElement.Element(attribute.XmlField) == null) ||
             *  (attribute.UseAttribute && attribute.XmlField != null && boElement.Attribute(attribute.XmlField) == null))
             *  propertyInfo.SetValue(target, null, null);
             * else*/
            if (boElement != null && attribute.XmlField != null)
            {
                if (boElement.Element(attribute.XmlField) != null || boElement.Attribute(attribute.XmlField) != null)
                {
                    if (propertyInfo.PropertyType == typeof(Boolean))
                    {
                        if (BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute) == "1")
                        {
                            propertyInfo.SetValue(target, true, null);
                        }
                        else
                        {
                            propertyInfo.SetValue(target, false, null);
                        }
                    }
                    else if (type == typeof(String))
                    {
                        propertyInfo.SetValue(target, BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), null);
                    }
                    else if (type == typeof(Decimal) || type == typeof(Decimal?))
                    {
                        propertyInfo.SetValue(target, Convert.ToDecimal(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(Int32) || type == typeof(Int32?))
                    {
                        propertyInfo.SetValue(target, Convert.ToInt32(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(Guid) || type == typeof(Guid?))
                    {
                        propertyInfo.SetValue(target, new Guid(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute)), null);
                    }
                    else if (type == typeof(DateTime) || type == typeof(DateTime?))
                    {
                        propertyInfo.SetValue(target, DateTime.Parse(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (type == typeof(XElement))
                    {
                        XElement element = BusinessObjectHelper.ReadXmlValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute);

                        if (element.FirstNode is XElement && !attribute.UseAttribute)
                        {
                            propertyInfo.SetValue(target, new XElement((XElement)element.FirstNode), null);
                        }
                        else if (!attribute.UseAttribute)
                        {
                            propertyInfo.SetValue(target, new XElement(attribute.XmlField, element.Value), null);
                        }
                        else
                        {
                            propertyInfo.SetValue(target, element, null);
                        }
                    }
                    else if (typeof(IBusinessObject).IsAssignableFrom(type))
                    {
                        if (!String.IsNullOrEmpty(attribute.XmlField))
                        {
                            IBusinessObject obj = BusinessObjectHelper.CreateRelatedBusinessObjectFromXmlElement((XElement)boElement.Element(attribute.XmlField).FirstNode, attribute.RelatedObjectType);
                            propertyInfo.SetValue(target, obj, null);
                        }
                        else                         //check if the nested object has attribute indicating from which node to deserialize him
                        {
                            if (BusinessObject.ClassXmlSerializationCache.ContainsKey(type))
                            {
                                XmlSerializationCache cache = BusinessObject.ClassXmlSerializationCache[type];

                                IBusinessObject obj = (IBusinessObject)Activator.CreateInstance(type, target);                                 //constructor(parent)
                                obj.Deserialize((XElement)boElement.Element(cache.Attribute.XmlField));
                                propertyInfo.SetValue(target, obj, null);
                            }
                        }
                    }
                    else if (type.IsEnum)
                    {
                        propertyInfo.SetValue(target, Convert.ToInt32(BusinessObjectHelper.ReadValueFromXml(boElement, attribute.XmlField, attribute.UseAttribute), CultureInfo.InvariantCulture), null);
                    }
                    else if (typeof(ISerializableBusinessObjectContainer).IsAssignableFrom(type))
                    {
                        ((ISerializableBusinessObjectContainer)propertyInfo.GetValue(target, null)).Deserialize(boElement.Element(attribute.XmlField));
                    }
                    else
                    {
                        throw new InvalidOperationException("Unknown type to deserialize");
                    }
                }
            }
            else if (attribute.OverrideWithEmptyValue && type == typeof(Nullable))
            {
                ///DO przemyślenia
                //propertyInfo.SetValue(target, null, null);
            }
        }