1

I have a employee object as shown below

class emp
    {
        public int EmpID { get; set; }
        public string EmpName { get; set; }
        public int deptID { get; set; }

    }

I need to create a mapping either in this class or a different class to map the properties with column name of my SQL

for eg. EmpdID="employeeID"
        EmpName="EmployeeName"
        deptID="DepartmentID"

When from my asp.net page when I create the employee class and pass it to a function:

for eg: emp e=new emp();
        e.EmpID=1;
        e.EmpName="tommy";    
        e.deptID=10;

When the emp object is populated and passed to the buildValues function it should return array of ComumnName(e.g.employeeID):Value(e.g.1),EmployeeName:tommy,DepartmentID:10)

string[] values=buildValues(emp);


public string[] buildValues(emp e)
{
  string[] values=null;


  return values;
}

I have 2 questions: 1. Where do I specify the mappings 2. How do I use the mappings in my buildValues function shown above and build the values string array.

I would really appreciate if you can help me with this

3 Answers3

0

You need to use Reflection.

Specifically, you need to loop over typeof(Employee).GetProperties().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

This is a solved problem. Do some research on ORM's and have a look at this SO question: .Net ORM that works well with MySQL

Community
  • 1
  • 1
Josh
  • 44,706
  • 7
  • 102
  • 124
0

First of all, (as it was already said) it's best to leave this kind of mappings to an ORM tool, and just forget about them. However, ORM tools tend to be too "maternal" in protecting you from the gory details of data access and such, so it can be complicated to extend them, or change their behaviour.

That said, you could create a special class (Mappings) that would hold all mapping code. The mappings themselves are best kept in a dictionary, something like this:

static class Mappings
{
    private static Dictionary<Type, Dictionary<string, string>> TypeMappings;

    private static Dictionary<string, string> EmployeeMapping;
    //... other mapped classes

    static Mappings()
    {
        TypeMappings = new Dictionary<Type, Dictionary<string, string>>();

        EmployeeMapping = new Dictionary<string, string>();
        EmployeeMapping.Add("EmpID", "EmployeeID");
        EmployeeMapping.Add("EmpName", "EmployeeName");
        EmployeeMapping.Add("DeptID", "DepartmentID");
        TypeMappings.Add(typeof(Employee),EmployeeMapping);


        //... other mapped classes
    }

    public static string[] BuildValues<T>(T item)
    {
        if (!TypeMappings.ContainsKey(typeof(T)))
            throw new Exception("wrong call");

        Dictionary<string, string> mapping = TypeMappings[typeof(T)];
        List<string> results = new List<string>();
        foreach (var keyValuePair in mapping)
        {
            string propName = keyValuePair.Key;
            string dbName = keyValuePair.Value;
            PropertyInfo pi = typeof(T).GetProperty(propName);
            object propValue = pi.GetValue(item, null);
            results.Add(string.Format("{0}:{1}", dbName, propValue));
        }
        return results.ToArray();
    }

}

Here, the TypeMappings is a dictionary of all mapped classes, whose mappings in turn are in propertyName - databaseName dictionaries.

The BuildValues method, takes those names, reflects the values, and build a results string.

SWeko
  • 30,434
  • 10
  • 71
  • 106
  • First things first. Thanks a ton for taking your time. One last question, so from my asp.net code behind page can I call the BuildValues method and pass the emp object like this string[] values=BuildValues(emp); – KungfuPanda Jun 13 '10 at 18:10
  • Sweko, One more issue is if there are 10 properties in my emp object. If i populate only 3 for eg. last 3, the values are not showing up in the string[] only the first 3 are shown with null values. – KungfuPanda Jun 13 '10 at 19:42
  • Have you modified the mappings accordingly? – SWeko Jun 14 '10 at 13:16