0

Here's my situation, I have to create a common function for inserting values into a MySQL db. So far I have the approach below. This code works fine when there is no composite class within another class. If we have navigational properties in the entities then this code breaks, i'm stuck here.

( ORM - we can't use it, since its clients requirement)

public string create(object entity){
    var sql = new OdbcCommand();
        var d = new StringDictionary();
        var dd = new StringDictionary();
        try
        {
            var objectType = entity.GetType();
            var properties = objectType.GetProperties();
            var tableName = objectType.GetCustomAttributes(false).Select(x => (x as TableAttribute).Name).FirstOrDefault();
            foreach (var prop in properties)
            {
                if (prop.DeclaringType != typeof (BasicRequest)) // BasicRequest is base class
                {
                    if (
                        prop.GetCustomAttributes(false).Where(y=>y is ColumnAttribute).Select(x => (x as ColumnAttribute)).FirstOrDefault().IsPrimaryKey)
                    {
                        continue;
                    }
                    if (prop.PropertyType.IsGenericType) // here comes the problem
                    {
                        // the problem is if a class has List of its child 
                          objects then how to insert them since here parent 
                          is not yet inserted.
                    }
                    if (prop.GetCustomAttributes(false).Where(y => y is ColumnAttribute).Select(x => (x as ColumnAttribute)).FirstOrDefault().IsParent)
                    {
                         // same problem here also but this time this object is
                         parent object of the entity
                        /*parentObject = prop.GetValue(entity, null);
                        CreateNew(parentObject);
                        */

                        continue;
                    }
                    d[prop.GetCustomAttributes(false).Where(y => y is ColumnAttribute).Select(x => (x as ColumnAttribute).Name).FirstOrDefault()] = DBUtil.FixSQLString(Convert.ToString(prop.GetValue(entity, null)));
                }
                else
                {
                    continue;
                }
            }
            Connection coxn = ConnectionPool.GetInstance().GetCoxn(Constants.MyName,ConnectionPool.WriteServer,"db_name");
            sql.Connection = coxn.odbcConnection;
            sql.CommandType = CommandType.Text;
            sql.CommandText = DBUtil.CreateInsert(tableName, d);
            int affected = sql.ExecuteNonQuery();

            if (affected != 1)
            {
                ErrorMessage = "Insert Failed Unexpectedly";
                return "0";
            }
            return "1";

        }
        catch (Exception ex)
        {
            ErrorMessage = ex.Message;
            return "0";
        }
}

Lets take an entity (this kinda design is not in my hand, this classes are already present I cannot change this entities)

[Table(Name="person")]
public class Person
{
    [Column(Name="id",IsPrimaryKey=true)] // Column is my custom attribute
    public int Id{get;set;}
    [Column(Name="name",IsPrimaryKey=true)]
    public string Name{get;set;}
   // this is where i'm not able get it how do i insert
   // child class with the function i wrote above 
    [Column(IsChild=true)]
    public List<Contact> contact{get;set;} // Navigation property
    //Same problem with this property also 
    // how do i do insert for parent
    [Column(IsParent=true)]
    public User user{get;set;}

}

// i'll call that insert method as
Common c = new Common();
c.Create(Person p); // p will be passed as argument from controller
Robert H
  • 11,520
  • 18
  • 68
  • 110
Meson
  • 859
  • 1
  • 9
  • 15

2 Answers2

0

Allow me to suggest using micro-orm, instead of hand-writing all this functionality.

I would recommend Dapper or PetaPoco (which you can add as a single file to your application).

motime
  • 564
  • 2
  • 10
0

It's looks like you're trying to create your own ORM implementation. It'll be very complicated and definitely not worth the effort for a client project.

You can use code generation tools to generate a data access layer from database structures. CodeSmith http://www.codesmithtools.com/ for example.

xing
  • 447
  • 2
  • 6
  • database structures is not good at all, its been there for long time, and client is not in a mood to change that, i dont think any code generation tools will work, that's why i'm doin' this manual approach. – Meson Oct 12 '12 at 06:13
  • may be the client is not worth, but for knowledge purpose it is absolutely a worth – Meson Oct 12 '12 at 06:20