orm工具的设计

orm工具的设计,orm,mapping,foreign-keys,Orm,Mapping,Foreign Keys,我想为我的日常工作设计一个orm工具,但我总是担心外键的映射 以下是我的部分代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace OrmTool { [AttributeUsage(AttributeTargets.Property)] public class ColumnAttribute:A

我想为我的日常工作设计一个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;}
    }

}
我想使用Attribute来获取实体的元数据,然后映射它们,但我认为这真的很难做到

   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;
        }
    }
如果我从数据库中读取了所有的艺术书籍,当我获得ReferencesAttribute时,我如何设置BookISBN的值?
在方法EntityListGenerator中,我尝试进行递归以获得映射,但我不知道接下来如何做,对于每个外键,我将从数据库中读取数据?还是把所有的外国地图都画出来?这些都太糟糕了。

你到底为什么要再次发明轮子??你不能只使用现有的一个ORMs吗??只是好奇。。。。我不想把时间浪费在这样的事情上……我只想了解更多关于它的知识,并进行更多的练习,然后编写代码。现在如果你想从“其他方法”(希望避免“其他错误”)中学习,那么看看现有的框架以及它们是如何好的(或者不是那么好)。@pst谢谢你的建议,我只想学习更多并改进自己,谢谢
[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;}
}