自己動手寫輕量級ORM(C#)


最近在看反射,突然想寫一個ORM工具,要輕量級的,不要配置文檔,先不管效率,就是一個小工具,在項目初期方便掛數據庫。

我的目標就是在數據庫中建個表,在項目中寫個模型,然后用上這個ORM工具,就能實現數據庫的基本增刪改查。

有想法就動手做,翠花上代碼:

public bool Insert(object entity)
        {
            Type t = entity.GetType();
            PropertyInfo[] properties = t.GetProperties();

            if (properties.Count<PropertyInfo>() > 1)                //至少兩個字段,一個主鍵,一個數據字段。
            {
                string sql = "Insert into " + t.Name;       //約定:類名即為數據表名。
                string fieldList = "";
                string fieldVals = "";

                foreach (PropertyInfo field in properties)
                {
                    if (field.Name.CompareTo("Id") == 0)        //約定:字段名若為Id,必為主鍵,且自動增長。
                        continue;
                    switch (field.PropertyType.Name)            //約定:屬性名即為數據字段名。
                    {
                        case "String":
                        case "DateTime":
                            fieldList += field.Name + ",";
                            fieldVals += "'" + field.GetValue(entity, null) + "',";
                            break;
                        case "Int32":
                        case "Double":
                            fieldList += field.Name + ",";
                            fieldVals += field.GetValue(entity, null) + ",";
                            break;
                        case "Boolean":
                            fieldList += field.Name + ",";
                            fieldVals += ((bool)field.GetValue(entity, null) ? 1 : 0) + ",";
                            break;
                        default:
                            break;
                    }
                }
                fieldList = fieldList.Remove(fieldList.Length - 1);
                fieldVals = fieldVals.Remove(fieldVals.Length - 1);
                sql += "(" + fieldList + ") values (" + fieldVals + ")";
                return db.ExecuteSql(sql);
            }
            else        //"出錯,沒有字段被組合!"
                return false;
        }
public bool Modify(object entity)
        {
            Type t = entity.GetType();
            PropertyInfo[] properties = t.GetProperties();
            if (properties.Count<PropertyInfo>() > 1)                //至少兩個字段,一個主鍵,一個數據字段。
            {

                string sql = "Update " + t.Name + " set ";       //約定:類名即為數據表名。
                string fieldSet = "";
                string fieldCondition = "";
                bool firstEntry = true;
                foreach (PropertyInfo field in properties)
                {
                    if (firstEntry)        //約定:第一個字段為主鍵。
                    {
                        if (field.PropertyType.Name.StartsWith("Int"))
                            fieldCondition += field.Name + "=" + field.GetValue(entity, null);
                        else
                            fieldCondition += field.Name + "='" + field.GetValue(entity, null) + "'";
                        firstEntry = false;
                    }
                    else
                        switch (field.PropertyType.Name)            //約定:屬性名即為數據字段名。
                        {
                            case "String":
                            case "DateTime":
                                fieldSet += field.Name + "='" + field.GetValue(entity, null) + "',";
                                break;
                            case "Int32":
                            case "Double":
                                fieldSet += field.Name + "=" + field.GetValue(entity, null) + ",";
                                break;
                            case "Boolean":
                                fieldSet += field.Name + "=" + ((bool)field.GetValue(entity, null) ? 1 : 0) + ",";
                                break;
                            default:
                                break;
                        }
                }
                fieldSet = fieldSet.Remove(fieldSet.Length - 1);
                sql += fieldSet + " where " + fieldCondition;
                return db.ExecuteSql(sql);
            }
            else        //"出錯,沒有字段被組合!"
                return false;
        }
public bool Delete(object entity)
        {
            Type t = entity.GetType();
            PropertyInfo[] properties = t.GetProperties();
            if (properties.Count<PropertyInfo>() > 0)
            {
                string sql = "Delete From " + t.Name + " where ";
                string fieldCondition = "";

                if (properties[0].PropertyType.Name.StartsWith("Int"))
                    fieldCondition += properties[0].Name + "=" + properties[0].GetValue(entity, null);
                else
                    fieldCondition += properties[0].Name + "='" + properties[0].GetValue(entity, null) + "'";

                sql += fieldCondition;
                return db.ExecuteSql(sql);
            }
            else
                return false;
        }
public bool Select(object entity)
        {
            Type t = entity.GetType();
            PropertyInfo[] properties = t.GetProperties();
            if (properties.Count<PropertyInfo>() > 1)                //至少兩個字段,一個主鍵,一個數據字段。
            {

                string sql = "Select ";       //約定:類名即為數據表名。
                string fieldList = "";
                string fieldCondition = "";
                bool firstEntry = true;
                foreach (PropertyInfo field in properties)
                {
                    if (firstEntry)        //約定:第一個字段為主鍵。
                    {
                        if (field.PropertyType.Name.StartsWith("Int"))
                            fieldCondition += field.Name + "=" + field.GetValue(entity, null);
                        else
                            fieldCondition += field.Name + "='" + field.GetValue(entity, null) + "'";
                        firstEntry = false;
                    }
                    else
                        switch (field.PropertyType.Name)            //約定:屬性名即為數據字段名。
                        {
                            case "String":
                            case "DateTime":
                                fieldList += field.Name + ",";
                                break;
                            case "Int32":
                            case "Double":
                                fieldList += field.Name + ",";
                                break;
                            case "Boolean":
                                fieldList += field.Name + ",";
                                break;
                            default:
                                break;
                        }
                }
                fieldList = fieldList.Remove(fieldList.Length - 1);
                sql += fieldList + " from " + t.Name + " where " + fieldCondition;
                var rs = db.getResult(sql);
                if (rs.Count > 0)
                {
                    int index = 1;
                    foreach (var al in rs)
                    {
                        switch (properties[index].PropertyType.Name)            //約定:屬性名即為數據字段名。
                        {
                            case "String":
                                properties[index].SetValue(entity, al, null);
                                break;
                            case "DateTime":
                                properties[index].SetValue(entity, DateTime.Parse(al.ToString()), null);
                                break;
                            case "Int32":
                                properties[index].SetValue(entity, Int32.Parse(al.ToString()), null);
                                break;
                            case "Double":
                                properties[index].SetValue(entity, Double.Parse(al.ToString()), null);
                                break;
                            case "Boolean":
                                properties[index].SetValue(entity, Boolean.Parse(al.ToString()), null);
                                break;
                            default:
                                break;
                        }
                        index++;
                    }
                    return true;
                }
                else
                    return false;
            }
            else        //"出錯,沒有字段被組合!"
                return false;
        }

上面的代碼實現了最基本的單條紀錄的增刪改查,其中最麻煩的是查,最簡單的是刪。
其中的db變量是數據庫操作對象,這個大家都熟悉,我就不貼代碼了,上面的代碼提供思路供大家參考,若哪位兄弟需要代碼,請到下面鏈接去下載,保證能正常運行,例子中給的是MYSQL的操作方法。
下載鏈接:http://download.csdn.net/detail/ztk12/4975759
MySql.Data.dll 這個動態鏈接庫被我從項目中移除了,大家如果機子上沒有的話,自己去下一個吧。


免責聲明!

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



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