第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

簡單代碼生成器原理剖析

標簽:
JavaScript

上篇《简单代码生成器原理剖析(一)》分析了代码生成器的原理,查询数据库系统视图:INFORMATION_SCHEMA.TABLES、 INFORMATION_SCHEMA.COLUMNS  可以获得数据库中表、列的相关信息,再运用StringBuilder类的其AppendLine方法追加字符串,最后早运用File.WriteAllText方法将字符串写入文件。

第二版代码生成器在第一版的基础上扩展了以下功能:

  • 使用了部分类(partial):当使用大项目或自动生成的代码(如由 Windows 窗体设计器提供的代码)时,将一个类、结构或接口类型拆分到多个文件中的做法就很有用。 分部类型可能包含分部方法。

  •  使用可空类型:由于数据库中表中数据很有可能是NULL,可空类型使得数据从表中读取出来赋值给值类型更加兼容。

  • 增加了ToModel方法:将数据库表中一行数据封装成Model类的对象返回。

 Model的实现:复制代码

        /// <summary>
        /// Model
        /// </summary>
        public static void CreateModel(CodeBuilderArgs args,string tableName)
        {
            DataTable dt = ExecuteDataTable(args.ConnectionString, "select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@TABLE_NAME"
                ,new SqlParameter("TABLE_NAME",tableName));
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Linq;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine("namespace " + args .Namespace+ ".Model");
            sb.AppendLine("{");
            sb.AppendLine("partial class " + tableName);
            sb.AppendLine("{");
            foreach (DataRow row in dt.Rows)
            {
                string colName = Convert.ToString(row["COLUMN_NAME"]);
                string colType = Convert.ToString(row["DATA_TYPE"]);

                Type netType = GetTypeByDBType(colType);
                string netTypeName;
                if (netType.IsValueType)
                {
                    netTypeName = netType + "?";
                }
                else
                {
                    netTypeName = netType.ToString();
                }
                sb.AppendLine("public " +netTypeName +" "+ colName + " { get; set; }");
            }
            sb.AppendLine(" }");
            sb.AppendLine(" }");
            string modelDir = Path.Combine(args.OutputDir, "Model");
            string modelFile = Path.Combine(modelDir, tableName+".cs");
            Directory.CreateDirectory(modelDir);
            File.WriteAllText(modelFile, sb.ToString());
        }

复制代码DAL(数据访问层)的实现: 复制代码

        /// <summary>
        ///DAL
        /// </summary>
        /// <param name="args"></param>
        /// <param name="tableName"></param>
        public static void CreateDAL(CodeBuilderArgs args, string tableName)
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = ExecuteDataTable(args.ConnectionString, "select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@TABLE_NAME"
    , new SqlParameter("TABLE_NAME", tableName));
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Linq;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine("using "+args.Namespace+".Model;");
            sb.AppendLine("using System.Data.SqlClient;");
            sb.AppendLine("using System.Data;");
            sb.AppendLine("namespace "+args.Namespace+".DAL");
            sb.AppendLine("{");
            sb.AppendLine("partial class "+tableName+"DAL");
            sb.AppendLine("{");
            sb.AppendLine("public int AddNew("+tableName+" model)");
            sb.AppendLine("{");
     
            string[] cols = GetColsWithoutId(GetCols(dt, new List<string>())).ToArray();
            var colsParameters=from e in cols select"@"+ e;
            sb.AppendLine("string sql = \"insert into " + tableName + "(" + string.Join(",", cols) + ") output inserted.id values(" + string.Join(",", colsParameters.ToArray()) + ")\";");
            sb.AppendLine("int id = (int)SqlHelper.ExecuteScalar(sql");
            
            foreach (DataRow row in dt.Rows)
            {
                string colName = Convert.ToString(row["COLUMN_NAME"]);
                if(!(colName.ToLower()=="id"))
                {
                  sb.AppendLine(", new SqlParameter(\""+colName+"\", model."+colName+")");   
                }
               
            }
            sb.AppendLine(");");
            sb.AppendLine("return id;");
            sb.AppendLine("}");

            var par=from e in cols select e+"="+"@"+e;
            sb.AppendLine("public bool Update("+tableName+" model)");
            sb.AppendLine("{");
            sb.AppendLine("string sql = \"update "+tableName+" set "+string.Join(",",par.ToArray())+" where id=@id\";");
            sb.AppendLine("int rows = SqlHelper.ExecuteNonQuery(sql");
            foreach (DataRow row in dt.Rows)
            {
                string colName = Convert.ToString(row["COLUMN_NAME"]);
                sb.AppendLine(", new SqlParameter(\"" + colName + "\", model." + colName + ")");   
            }
            sb.AppendLine(");");
            sb.AppendLine("return rows>0;");
            sb.AppendLine("}");


            sb.AppendLine("public bool Delete(int id)");
            sb.AppendLine("{");
            sb.AppendLine("int rows = SqlHelper.ExecuteNonQuery(\"delete from "+tableName+" where id=@id\",");
            sb.AppendLine("new SqlParameter(\"id\", id));");
            sb.AppendLine("return rows > 0;");
            sb.AppendLine("}");

            sb.AppendLine("private static "+tableName+" ToModel(DataRow row)");
            sb.AppendLine("{");
            sb.AppendLine(""+tableName+" model = new "+tableName+"();");
            foreach (DataRow row in dt.Rows)
            {
                string colName = Convert.ToString(row["COLUMN_NAME"]);
                string colType = Convert.ToString(row["DATA_TYPE"]);

                Type netType = GetTypeByDBType(colType);
                string netTypeName;
                if (netType.IsValueType)
                {
                    netTypeName = netType + "?";
                }
                else
                {
                    netTypeName = netType.ToString();
                }
                sb.AppendLine("model."+colName+" = row.IsNull(\""+colName+"\") ? null : "+"("+netTypeName+")"+"row[\""+colName+"\"];");
            }
            sb.AppendLine("return model;");
            sb.AppendLine("}");



            //
            sb.AppendLine("public "+tableName+" Get(int id)");
            sb.AppendLine("{");
            sb.AppendLine("DataTable dt = SqlHelper.ExecuteDataTable(\"select * from "+tableName+"  where id=@id\",");
            sb.AppendLine("new SqlParameter(\"id\", id));");
            sb.AppendLine("if(dt.Rows.Count > 1)");
            sb.AppendLine("{");
            sb.AppendLine("throw new Exception(\"more than 1 row was found\");");
            sb.AppendLine("}");
            sb.AppendLine("if(dt.Rows.Count <= 0)");
            sb.AppendLine("{");
            sb.AppendLine("return null; ");
            sb.AppendLine("}");
            sb.AppendLine("DataRow row = dt.Rows[0];");
            sb.AppendLine(""+tableName+" model = ToModel(row);");
            sb.AppendLine("return model;");
            sb.AppendLine("}");

            sb.AppendLine("public IEnumerable<"+tableName+">ListAll()");
            sb.AppendLine("{");
            sb.AppendLine("List<"+tableName+"> list = new List<"+tableName+">();");
            sb.AppendLine("DataTable dt = SqlHelper.ExecuteDataTable(\"select * from "+tableName+"\");");
            sb.AppendLine("foreach (DataRow row in dt.Rows)");
            sb.AppendLine("{");
            sb.AppendLine("list.Add(ToModel(row));");
            sb.AppendLine("}");
            sb.AppendLine("return list;");
            sb.AppendLine("}");
            sb.AppendLine("}");
            sb.AppendLine("}");

            string DALlDir = Path.Combine(args.OutputDir,"DAL");
            string DALFile = Path.Combine(DALlDir,tableName+"DAL.cs");
            Directory.CreateDirectory(DALlDir);
            File.WriteAllText(DALFile,sb.ToString());
        }
        private static IEnumerable<string> GetColsWithoutId(List<string> cols)
        {
            var colArray = from col in cols where col.ToLower() != "id" select col;
            return colArray;
        }
        private static List<string> GetCols(DataTable dt, List<string> cols)
        {
            foreach (DataRow row in dt.Rows)
            {
                string colName = Convert.ToString(row["COLUMN_NAME"]);
                cols.Add(colName);
            }
            return cols;
        }

复制代码BLL的实现:复制代码

        /// <summary>
        /// BLL
        /// </summary>
        /// <param name="args"></param>
        /// <param name="tableName"></param>
        public static void CreateBLL(CodeBuilderArgs args,string tableName)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Linq;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine("using "+args.Namespace+".Model;");
            sb.AppendLine("using "+args.Namespace+".DAL;");
            sb.AppendLine("namespace "+args.Namespace+".BLL");
            sb.AppendLine("{");
            sb.AppendLine("partial class "+tableName+"BLL");
            sb.AppendLine("{");
            sb.AppendLine("public int AddNew("+tableName+" model)");
            sb.AppendLine("{");
            sb.AppendLine("return new "+tableName+"DAL().AddNew(model);");
            sb.AppendLine("}");
            sb.AppendLine("public bool Delete(int id)");
            sb.AppendLine("{");
            sb.AppendLine("return new "+tableName+"DAL().Delete(id);");
            sb.AppendLine("}");
            sb.AppendLine("public bool Update("+tableName+" model)");
            sb.AppendLine("{");
            sb.AppendLine("return new "+tableName+"DAL().Update(model);");
            sb.AppendLine("}");
            sb.AppendLine("public "+tableName+" Get(int id)");
            sb.AppendLine("{");
            sb.AppendLine("return new "+tableName+"DAL().Get(id);");
            sb.AppendLine("}");
            sb.AppendLine("public IEnumerable<"+tableName+"> ListAll()");
            sb.AppendLine("{");
            sb.AppendLine("return new "+tableName+"DAL().ListAll();");
            sb.AppendLine("}");
            sb.AppendLine("}");
            sb.AppendLine("}");
            string BLLDir = Path.Combine(args.OutputDir,"BLL");
            string BLLFile = Path.Combine(BLLDir,tableName+"BLL.cs");
            Directory.CreateDirectory(BLLDir);
            File.WriteAllText(BLLFile,sb.ToString());
        }

复制代码详细代码可参考:http://www.oksvn.com/Project/Detail-11919.shtml

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消