目前有很多开源的orm项目,大多情况下也不需要我们重复去造轮子,我们只需要了解轮子怎么造的,怎么用就可以,下面简单说一下怎么通过实体生成一个sql语句;

        先建立2个attribute类,tableattribute、columnattribute   , 且希望tableattribute只想标记在实体类上,所以限制 [attributeusage(attributetargets.class)],而希望columnattribute只标记在属性上 [attributeusage(attributetargets.property)]

    [attributeusage(attributetargets.class)]
    public class tableattribute : attribute
    {
        private string _tablename = "";
        public tableattribute(string tablename)
        {
            this._tablename = tablename;
        }
        public string gettablename()
        {
            return this._tablename;
        }
    }
   [attributeusage(attributetargets.property)]
    public class columnattribute:attribute
    {
        private string _columnname = "";  
        public columnattribute(string columnname)
        {
            this._columnname = columnname;
        }
        public string getcolumnname()
        {
            return this._columnname;
        }
    }

     再做一个静态扩展类,增加2个扩展方法 一个针对类型的、一个针对属性的扩展方法

public static class attributeextend
    {
        public static string getmappingname<t>(this t t) where t : basemodel
        {
            if (t.gettype().isdefined(typeof(tableattribute), true))
            {
                tableattribute attribute = (tableattribute)t.gettype().getcustomattributes(typeof(tableattribute), true)[0];
                return attribute.gettablename();
            }
            else
            {
                return t.gettype().name;
            }
        }

        public static string getmappingname(this propertyinfo prop)
        {
            if (prop.isdefined(typeof(columnattribute), true))
            {
                columnattribute attribute = (columnattribute)prop.getcustomattribute(typeof(columnattribute), true);
                return attribute.getcolumnname();
            }
            else
            {
                return prop.name;
            }
        } 

        public static string getmappingname(this type type)
        {
            if (type.isdefined(typeof(tableattribute), true))
            {
                tableattribute attribute = (tableattribute)type.getcustomattribute(typeof(tableattribute), true);
                return attribute.gettablename();
            }
            else
            {
                return type.name;
            }
        }
    }

         获取sql语句方法,目前只简单写了查询所有的,及根据id查询,如果想丰富查询操作需要用到表达式目录树

  public class ormsql
    {    
        public string getallselectsql<t>() where t :basemodel
        {
           type type = typeof(t);
           var props = type.getproperties();   
           string columnstring = string.join(",", props.select(m => $"[{m.getmappingname()}]"));
           string selectsql = $"select  {columnstring} from {type.getmappingname()}"; 
           return selectsql;
        }
        public string getselectsqlbyid<t>(t t)  where t :basemodel
        {
            type type = typeof(t);
            var props = type.getproperties();
            string columnstring = string.join(",", props.select(m => $"[{m.getmappingname()}]"));
            string selectsql = $"select  {columnstring}  from {type.getmappingname()} where id= '{t.id}'";
            return selectsql;
        }

    }

         调用方法

 public class program
    {
        public static void main(string[] args)
        {
            ormsql orm = new ormsql();
            console.writeline(orm.getallselectsql<donator>() );
            console.writeline(orm.getselectsqlbyid<donator>(new donator() {  id=1}) );
        }
    }

     运行截图: