locked
Proper Class structure / please help me do things better. RRS feed

  • Question

  • User451246207 posted

    Hello all, I am in the process of starting out on a huge web site project and I just wanted to run by you guys the way I currently build and use classes in my c# asp.net website.

    For this example I will use a "Person" class.  This person has many properties which are stored in a record in a mysql database.  So I pass the unique id in the database to my person constructor which uses my database interaction code to populate the properties in the class from the database.  This can be done 2 ways, property by property assigning the values, or I can make sure all the public properties in the class exactly match the columns in the database and use reflection on the class to populate the values.  The second method requires way less code, however I am not sure if it ties me down too much.  I would like to access the persons information using xml, so I have a getXml method that returns an XElement with all of the properties of the class, this can also be built manually or using reflection to loop through all of the properties.  I can also use reflection to loop through all properties and save the data back to the database.

    For the site I am building, I will have in excess of 30 classes, all unique in most respects except for the database population, conversion to xml, and saving parts.

    What I have done in the past, is written a base class which handles the generic looping thru properties with reflection and takes care of all the dirty work, so if I want to write a new class, I just have to extend my base class, add my unique properties at the top of the class, and its ready to go, as long as the public propery names match that of the db columns. 

    Here is an example of my baseclass:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Text;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Reflection;
    using System.Collections;
    
    /// <summary>
    /// Summary description for BaseClass
    /// </summary>
    public class BaseClass
    {
        protected string tableName { get; set; }
        protected string keyName { get; set; }
        protected string xmlNodeName { get; set; }
        public string hash { get; set; }
    	public BaseClass()
    	{
    	}
        public BaseClass(string tn, string kn, string xn)
        {
            tableName = tn;
            keyName = kn;
            xmlNodeName = xn;
            GetType().GetProperty(keyName).SetValue(this, 0, null);
            hash = makeHash();
        }
        public BaseClass(object id, string tn, string kn, string xn)
        {
            tableName = tn;
            keyName = kn;
            xmlNodeName = xn;
            DataHelper getter = new DataHelper("select * from " + tableName + " where " + keyName + " = ?id");
            getter.setParam("?id", id.ToString());
            foreach (DataRow dr in getter.getData())
            {
                foreach (PropertyInfo p in this.GetType().GetProperties())
                {
                    if (p.PropertyType.ToString() == "System.DateTime")
                    {
                        DateTime dt = DateTime.Parse(dr[p.Name].ToString());
                        p.SetValue(this, dt, null);
                    }
                    else if (p.PropertyType.ToString() == "System.Int32")
                    {
                        p.SetValue(this, int.Parse(dr[p.Name].ToString()), null);
                    }
                    else if (p.PropertyType.ToString() == "System.String")
                    {
                        p.SetValue(this, dr[p.Name].ToString(), null);
                    }
                    else if (p.PropertyType.ToString() == "System.Boolean")
                    {
                        p.SetValue(this, Boolean.Parse(dr[p.Name].ToString()), null);
                    }
                    else if (p.PropertyType.ToString() == "System.Xml.Linq.XElement")
                    {
                        p.SetValue(this, XElement.Parse(dr[p.Name].ToString()), null);
                    }
                    else if (p.PropertyType.ToString() == "System.Double")
                    {
                        p.SetValue(this, Double.Parse(dr[p.Name].ToString()), null);
                    }
                    else
                    {
                        p.SetValue(this, dr[p.Name].ToString(), null);
                    }
                }
            }
            //any custom object init stuff here
        }
        public string q(object str)
        {
            string ret = "";
            try
            {
                ret = '"' + str.ToString() + '"';
            }
            catch (Exception ex)
            {
                ret = '"' + " " + '"';
            }
            return ret;
        }
        public virtual void delete()
        {
            DataHelper del = new DataHelper("delete from " + tableName + " where " + keyName + " = " + "?id");
            del.setParam("?id", GetType().GetProperty(keyName).GetValue(this, null).ToString());
            del.execute();
        }
        public void save()
        {
            Hashtable ht = new Hashtable();
            foreach (PropertyInfo p in this.GetType().GetProperties())
            { ht.Add(p.Name, p.GetValue(this, null)); }
            GetType().GetProperty(keyName).SetValue(this, DataHelper.doSave(ht, tableName, keyName), null);
        }
        public string makeHash()
        {
            return RandomPassword.Generate(32, 32);
        }
        public virtual void setXml(XElement data)
        {
    
        }
        public virtual XElement getXml()
        {
            XElement xml = new XElement(xmlNodeName);
            foreach (PropertyInfo p in this.GetType().GetProperties())
            {
                if (p.PropertyType.ToString() == "System.DateTime")
                {
                    xml.Add(new XAttribute(p.Name, ((DateTime)p.GetValue(this, null)).ToString()));
                }
                else if (p.PropertyType.ToString() == "System.Xml.Linq.XElement")
                {
                    XElement holder = new XElement(p.Name);
                    holder.Add(p.GetValue(this, null) as XElement);
                    xml.Add(holder);
                }
                else
                {
                    xml.Add(new XAttribute(p.Name, p.GetValue(this, null)));
                }
            }
            return xml;
        }
        public string HtmlEncode(string text)
        {
            string ret = " ";
            try
            {
                char[] chars = HttpUtility.HtmlEncode(text).ToCharArray();
                StringBuilder result = new StringBuilder(text.Length + (int)(text.Length * 0.1));
    
                foreach (char c in chars)
                {
                    int value = Convert.ToInt32(c);
                    if (value > 127)
                        result.AppendFormat("&#{0};", value);
                    else
                        result.Append(c);
                }
                ret = result.ToString();
            }
            catch (Exception ex) { }
            return ret;
        }
    }
     
    and here is a simple class that extends it :
     
    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    /// <summary>
    /// Summary description for Media
    /// </summary>
    public class Media : BaseClass
    {
        public int mediaId { get; set; }
        public string title { get; set; }
        public string type { get; set; }
        public int childId { get; set; }
        public Media()
            : base("medias", "mediaId", "media")
        {
    
        }
        public Media(object id)
            : base(id, "medias", "mediaId", "media")
        {
    
        }
    }
    

     

     

    Thanks in advance for any insight or pointers!

     

    Jason



     

    Monday, February 1, 2010 7:49 AM

Answers

  • User197322208 posted

    For datareader, please check http://stackoverflow.com/questions/1973351/automapper-mapping-between-a-idatareader-and-dto-object

    For some data, check ForMember

    http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/02/21/automapper-feature-projection.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 1, 2010 4:05 PM

All replies

  • User197322208 posted

    You are reinventing the AutoMapper! (just search...)



    Monday, February 1, 2010 8:56 AM
  • User451246207 posted

    How much of these processes should the AutoMapper take care of for me?  I see that it should allow me to go from My Object type to xml and back, will it help with the database interaction as well?  From a DataRow object to my custom object? and from my custom object to the database? 

     

    Also, in some cases going to xml I would like the data to display as a text node instead of in an attribute (if the data is likely to contain html for example), how would something like the automapper handle this?

    Monday, February 1, 2010 10:40 AM
  • User197322208 posted

    For datareader, please check http://stackoverflow.com/questions/1973351/automapper-mapping-between-a-idatareader-and-dto-object

    For some data, check ForMember

    http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/02/21/automapper-feature-projection.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 1, 2010 4:05 PM