Lecture – XML-Reflection-Inheritance Part 3

XML-Reflection-Inheritance Part 3

Prerequisites

Part of Topic – Creating a Single Table Database Object Storage

Lecture – XML-Reflection-Inheritance Part 2

next lecture: Lecture – XML-Refelction-Inheritance Part 4

Summary

Topics Covered in this Video;

-ID and Owner Properties
-OwnerUserID and LastEditedDate elements
-DatabaseXmlStore
-Where to place Create Table
-Refactoring
-Convert.ChangeType
-Context.User.Identity.Name
-Inserting Objects

Video 

Reference Materials

Sample of Calling Classes

protected void btnSave_Click(object sender, EventArgs e)
        {
            SamplePropertiesInherited s = new SamplePropertiesInherited();
            s.setPropertiesFromXml(tbResults.Text);
            s.Owner = Context.User.Identity.Name;
            lblValue.Text =  DatabaseXmlStore.Insert(s);
        }

Interface Class


    public interface IXmlPropertyObject
    {
        // Required Properties
        int id { get; set; }
        string Name { get; set; }
        string Description { get; set; }
        string Owner { get; set; }
        // Required Methods
        string asXML();
        string className();
        string getPropertyFromXml(string property, string xml);
        string getPropertyValue(string name);
        void setPropertyFromXml(string property, string xml);
        void setProperty(string name, string value);
        void setPropertiesFromXml(string xml);
    }

XmlPropertyObject Class

public class XmlPropertyObject : IXmlPropertyObject 
    {
        public int id {get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Owner { get; set; }
        public string asXML()
        {
            string s = String.Empty;
            string fc = className();
            while (fc.Contains("."))
            {
                fc = fc.Substring(fc.IndexOf(".") + 1);
            }
            s += "<" + fc + ">\n";
            foreach (var property in this.GetType().GetProperties())
            {
                s += " <" + property.Name + ">";
                s += Convert.ToString(property.GetValue(this));
                s += "</" + property.Name + ">";
                s += "\n";
            }
            s += "</" + fc + ">";
            return s;
        }
        public string className()
        {
            string fc = this.GetType().ToString();
            while (fc.Contains("."))
            {
                fc = fc.Substring(fc.IndexOf(".") + 1);
            }
            return fc;
        }
        public string getPropertyFromXml(string property, string xml)
        {
            XDocument doc = XDocument.Parse(xml);
            // Gets the value fom XML
            try  {return doc.Root.Element(property).Value; }
            catch {return String.Empty; }
        }
        public void setPropertyFromXml(string property, string xml)
        {
            string value = getPropertyFromXml(property, xml);
            setProperty(property, value);
        }
        public string getPropertyValue(string name)
        {
            PropertyInfo pi = this.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
            return Convert.ToString(pi.GetValue(this));
        }
        public void setProperty(string name, string value)
        {
            PropertyInfo pi = this.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
            if (null != pi && pi.CanWrite)
            {
                pi.SetValue(this, Convert.ChangeType(value,pi.PropertyType), null);
            }
        }
        public void setPropertiesFromXml(string xml)
        {
            XDocument doc = XDocument.Parse(xml);
            foreach (XElement xe in doc.Root.Elements())
            {
                setProperty(xe.Name.ToString(), xe.Value);
            }
        }
    }

Database XmlStore Class as part of this lecture

public class DatabaseXmlStore
    {
        // Allows for the storage and retrieval of objects from the database
        // That conform to IXmlPropertyObject 
        /*
         * 
         CREATE TABLE XmlObject (
          id INT PRIMARY KEY IDENTITY(1,1),
          Class VARCHAR(255) NOT NULL,
          Name VARCHAR(255) NULL,
          Description VARCHAR(500) NULL,
          XMLData VARCHAR(MAX) NULL,
          OwnerUserID VARCHAR(255) NOT NULL,
          LastEditedDate DATETIME)
        
         * * 
         */
        // Properties
       
         public static SqlConnection Connection()
         {
             // This should be read from the Web.config
             string cs = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=COP4834ObjectStorage;Data Source=YOUR_COMPUTER";
             return new SqlConnection(cs);
         }
        #region Insert
         public static string Insert(IXmlPropertyObject o)
         {
             string objectName = o.className();
             string sql = "INSERT INTO XmlObject ";
             sql += "(Class, Name, Description, XMLData, OwnerUserID, LastEditedDate)";
             sql += "VALUES";
             sql += "(@Class, @Name, @Description, @XMLData, @OwnerUserID, GETDATE())";
             SqlConnection connection = DatabaseXmlStore.Connection();
             using (connection)
             {
                 SqlCommand command = new SqlCommand(sql, connection);
                 command.Parameters.Add("@Class", SqlDbType.VarChar);
                 command.Parameters["@Class"].Value = o.className();
                 command.Parameters.Add("@Name", SqlDbType.VarChar);
                 command.Parameters["@Name"].Value = o.Name;
                 command.Parameters.Add("@Description", SqlDbType.VarChar);
                 command.Parameters["@Description"].Value = o.Description;
                 command.Parameters.Add("@XmlData", SqlDbType.VarChar);
                 command.Parameters["@XmlData"].Value = o.asXML();
                 command.Parameters.Add("@OwnerUserID", SqlDbType.VarChar);
                 command.Parameters["@OwnerUserID"].Value = o.Owner;
                 try
                 {
                     connection.Open();
                     Int32 rowsAffected = command.ExecuteNonQuery();
                     return "Data Saved";
                 }
                 catch (Exception ex)
                 {
                     return ex.Message;
                 }
             }
         }
        #endregion
        
        #region Select
        //public static void Select(IXmlPropertyObject o, int id)
        //{
            
        //}
        //public static DataTable SelectAll(string ownerID)
        //{
        //}
        //public static DataTable SelectAll(string ownerID, string objectType)
        //{
        //}
       
        #endregion
        //public static void Delete(IXmlPropertyObject o)
        //{
        //}
        //public static void Update(IXmlPropertyObject o)
        //{
        //}
    
    } 

Additional Information

COP 4834 Lectures Page