Click here to Skip to main content
15,886,069 members
Articles / Programming Languages / XML

netTierGenerator

Rate me:
Please Sign up or sign in to vote.
4.81/5 (20 votes)
30 Nov 2008CPOL14 min read 67.4K   2.8K   108  
A 3-tier application framework and code generation tool - the way for rapid and effective development.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

using Sample.Common.Util;

namespace Sample.Common.History
{
    public static class HistorySerializer
    {
        #region const
        private const string SERIALIZE_DELETE = "<item type=\"{0}\"><prop name=\"Id\" type=\"{1}\">{2}</prop><prop name=\"WasDeleted\" type=\"System.Boolean\">True</prop></item>";
        #endregion const

        public static string Serialize(object obj)
        {
            if (obj == null)
            {
                return String.Empty;
            }

            Type objType = obj.GetType();

            if (objType.IsDefined(typeof(NonHistoryAttributeAttribute), true))
            {
                return String.Empty;
            }

            if (objType.IsPrimitive || objType.IsValueType || objType == typeof(String))
            {
                return String.Format(
                        "<item type=\"{0}\"><prop name=\"Value\" type=\"{0}\">{1}</prop></item>",
                        objType.FullName,
                        HistorySerializer.ConvertToString(objType, obj)
                    );
            }

            string entityType = String.Empty;
            StringBuilder result = new StringBuilder();
            PropertyInfo[] properties = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            result.AppendFormat("<item type=\"{0}\">", objType.FullName);
            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo property = properties[i];
                if (property.Name == "IsDirty")
                {
                    continue;
                }
                if (property.IsDefined(typeof(NonHistoryAttributeAttribute), true))
                {
                    continue;
                }

                entityType = String.Empty;
                if (property.IsDefined(typeof(HistoryAttributeAttribute), true))
                {
                    HistoryAttributeAttribute[] hattrs = property.GetCustomAttributes(typeof(HistoryAttributeAttribute), true) as HistoryAttributeAttribute[];
                    if (hattrs[0].EntityType != null)
                    {
                        entityType = String.Format(" entityType=\"{0}\"", hattrs[0].EntityType.FullName);
                    }
                }

                if (property.PropertyType.IsPrimitive || property.PropertyType.IsValueType || property.PropertyType == typeof(String))
                {
                    object value = property.GetValue(obj, null);
                    result.AppendFormat(
                            "<prop name=\"{0}\" type=\"{1}\"{2}>{3}</prop>",
                            property.Name,
                            property.PropertyType,
                            entityType,
                            HistorySerializer.ConvertToString(property.PropertyType, value)
                        );
                }
            }
            result.Append("</item>");

            return result.ToString();
        }
        public static string SerializeForDelete(Type objType, Guid id)
        {
            return String.Format(
                    HistorySerializer.SERIALIZE_DELETE,
                    objType.FullName,
                    typeof(Guid).FullName,
                    id
                );
        }
        public static HistoryDeserializedInfo Deserialize(string xml)
        {
            HistoryDeserializedInfo result = new HistoryDeserializedInfo();
            if (String.IsNullOrEmpty(xml))
            {
                return result;
            }

            using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
            {
                if (reader.Read())
                {
                    reader.MoveToAttribute("type");
                    result.Type = reader.ReadContentAsString();

                    reader.ReadStartElement();
                    while (reader.IsStartElement())
                    {
                        reader.MoveToElement();
                        string element = reader.Name;

                        reader.MoveToAttribute("name");
                        string name = reader.ReadContentAsString();

                        reader.MoveToAttribute("type");
                        string type = reader.ReadContentAsString();

                        reader.MoveToAttribute("entityType");
                        string entityType = (String.Compare(reader.Name, "entityType", StringComparison.InvariantCultureIgnoreCase) == 0) ? reader.ReadContentAsString() : String.Empty;

                        reader.MoveToContent();
                        string value = reader.ReadElementContentAsString();
                        switch (element)
                        {
                            case "prop": // property
                                result[name] = new HistoryDeserializedInfoMember(type, entityType, HistorySerializer.XmlDecode(value));
                                break;
                        }
                    }
                }
            }

            return result;
        }

        private static string XmlDecode(string xml)
        {
            if (String.IsNullOrEmpty(xml))
            {
                return String.Empty;
            }
            return StringUtil.XmlDecode(xml);
        }
        private static string XmlEncode(string text)
        {
            if (String.IsNullOrEmpty(text))
            {
                return String.Empty;
            }
            return StringUtil.XmlEncode(text);
        }
        private static string ConvertToString(Type type, object obj)
        {
            if (type == typeof(String))
            {
                return HistorySerializer.XmlEncode(obj as string);
            }
            else if (type == typeof(DateTime))
            {
                return XmlConvert.ToString((DateTime)obj, XmlDateTimeSerializationMode.Local);
            }
            else
            {
                return Convert.ToString(obj, CultureInfo.InvariantCulture);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions