дизайн инструмента orm - PullRequest
       19

дизайн инструмента orm

1 голос
/ 06 апреля 2010

Я хочу разработать инструмент orm для своей повседневной работы, но я всегда беспокоюсь о сопоставлении внешнего ключа.

Вот часть моего кода:

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;}
    }

}

Я хочу использовать Атрибут, чтобы получить метаданные сущности, затем сопоставить их, но я думаю, что это действительно трудно сделать;

   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 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;
        }
    }

Я не знаю, как сделать следующий шаг, если у каждой сущности есть собственный внешний ключ, как мне получить возвращаемый результат? Если сущность такова:

[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;}
}

Если я читаю все книги ArtBook из базы данных и когда я получаю атрибут ReferencesAttribute, как мне установить значение BookISBN? в методе EntityListGenerator я пытаюсь сделать рекурсию, получающую сопоставление, но я не знаю, как делать дальше, для каждого внешнего ключа я буду считывать данные из базы данных? или получить все чужое потом сопоставление? это все очень плохо.

...