design of orm tool

Posted by ifree on Stack Overflow See other posts from Stack Overflow or by ifree
Published on 2010-04-06T13:34:57Z Indexed on 2010/04/06 13:43 UTC
Read the original article Hit count: 477

Filed under:
|
|

Hello , I want to design a orm tool for my daily work, but I'm always worry about the mapping of foreign key.

Here's part of my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace OrmTool
{
    [AttributeUsage(AttributeTargets.Property)]
    public class ColumnAttribute:Attribute
    {
        public string Name { get; set; }
        public SqlDbType DataType { get; set; }
        public bool IsPk { get; set; }
    }
     [AttributeUsage(AttributeTargets.Class,AllowMultiple=false,Inherited=false)]
    public class TableAttribute:Attribute
    {
       public string TableName { get; set; }
       public string Description { get; set; }

    }
    [AttributeUsage(AttributeTargets.Property)]
    public class ReferencesAttribute : ColumnAttribut
    {
      public Type Host { get; set; }
      public string HostPkName{get;set;}
    }

}

I want to use Attribute to get the metadata of Entity ,then mapping them,but i think it's really hard to get it done;

   public class DbUtility
    {

        private static readonly string CONNSTR = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        private static readonly Type TableType = typeof(TableAttribute);
        private static readonly Type ColumnType = typeof(ColumnAttribute);
        private static readonly Type ReferenceType = typeof(ReferencesAttribute);

        private static IList<TEntity> EntityListGenerator<TEntity>(string tableName,PropertyInfo[] props,params SqlParameter[] paras) {


            return null;
        }

        private static IList<TEntity> ResultList()
        {

            return null;
        }


        private static SqlCommand PrepareCommand(string sql,SqlConnection conn,params SqlParameter[] paras) {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
            cmd.Connection = conn;
            if (paras != null)
                cmd.Parameters.AddRange(paras);
            conn.Open();
            return cmd;
        }
    }

I don't know how to do the next step, if every Entity has it's own foreign key,how do I get the return result ? If the Entity like this:

[Table(Name="ArtBook")]
public class ArtBook{
   [column(Name="id",IsPk=true,DataType=SqlDbType.Int)]
   public int Id{get;set;}
   [References(Name="ISBNId",DataType=SqlDataType.Int,Host=typeof(ISBN),HostPkName="Id")]
   public ISBN BookISBN{get;set;}
   public .....more properties.
}

public class ISBN{
   public int Id{get;set;}
   public bool IsNative{get;set;}
}

If I read all ArtBooks from database and when I get a ReferencesAttribute how do I set the value of BookISBN?

© Stack Overflow or respective owner

Related posts about orm

Related posts about foreign-key