簡單理解ORM,實體類生成查詢SQL語句


       目前有很多開源的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}) );
        }
    }

     運行截圖:

     

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM