private void btnGenDALDataDict_Click(object sender, EventArgs e) { if (false == Msg.AskQuestion("确认要生成吗?")) { return; } //创建参数对象 Params4DataDictDAL param = new Params4DataDictDAL(); param.ConcretelyName = txtConcretelyName_DAL.Text; param.DAL_Name = txtDAL_DAL.Text; param.ORM_Name = txtORM_DAL.Text; param.DAL_Namespace = txtNamespace_DAL.Text; param.UsingNamespace = txtUsingNamespace_DAL.Lines; //生成源码 string code = new GenerateDAL().GenerateDataDictDAL(param); //显示代码 if (chkPreviewDAL.Checked) { this.ShowCode(code); } //输出.cs文件 if (chkExportToFileDAL.Checked) { string fileName = txtOutpup_DAL_DataDict.Text + txtDAL_DAL.Text + ".cs"; File.WriteAllText(fileName, code, Encoding.UTF8); MessageBox.Show("输出文件:" + fileName); } }
/// <summary> /// 生成数据字典的数据层 /// </summary> public string GenerateDataDictDAL(Params4DataDictDAL param) { StringBuilder builder = new StringBuilder(); //.Net Framework的名字空间 builder.AppendLine("using System;"); builder.AppendLine("using System.Collections.Generic;"); builder.AppendLine("using System.Text;"); builder.AppendLine("using System.Data;"); //引用的自定义名字空间 foreach (string space in param.UsingNamespace) { builder.AppendLine("using " + space + ";"); } //生成单元注释部分 CreateComment(builder, param.ConcretelyName); builder.AppendLine("namespace " + param.DAL_Namespace); //当前数据层所在的名字空间 builder.AppendLine("{"); builder.AppendLine(" /// <summary>"); builder.AppendLine(" /// " + param.DAL_Name); builder.AppendLine(" /// </summary>"); builder.AppendLine(" public class " + param.DAL_Name + " : dalBaseDataDict"); //基类 builder.AppendLine(" {"); builder.AppendLine(" /// <summary>"); builder.AppendLine(" /// 构造器"); builder.AppendLine(" /// </summary>"); builder.AppendLine(" /// <param name=\"loginer\">当前登录用户</param>"); builder.AppendLine(" public " + param.DAL_Name + "(Loginer loginer): base(loginer)"); //构造器 builder.AppendLine(" {"); builder.AppendLine(" _KeyName = " + param.ORM_Name + ".__KeyName; //主键字段"); builder.AppendLine(" _TableName = " + param.ORM_Name + ".__TableName;//表名"); builder.AppendLine(" _ModelType = typeof(" + param.ORM_Name + ");"); builder.AppendLine(" }"); builder.AppendLine(""); builder.AppendLine(" /// <summary>"); builder.AppendLine(" /// 根据表名获取该表的SQL命令生成器"); builder.AppendLine(" /// </summary>"); builder.AppendLine(" /// <param name=\"tableName\">表名</param>"); builder.AppendLine(" /// <returns></returns>"); builder.AppendLine(" protected override IGenerateSqlCommand CreateSqlGenerator(string tableName)"); builder.AppendLine(" {"); builder.AppendLine(" Type ORM = null;"); builder.AppendLine(" if (tableName == " + param.ORM_Name + ".__TableName) ORM = typeof(" + param.ORM_Name + ");"); builder.AppendLine(" if (ORM == null) throw new Exception(tableName + \"表没有ORM模型!\");"); builder.AppendLine(" return new GenerateSqlCmdByTableFields(ORM);"); builder.AppendLine(" }"); builder.AppendLine(""); builder.AppendLine(" }"); builder.AppendLine("}"); return(builder.ToString()); }
/// <summary> /// 生成数据字典的数据层 /// </summary> public string GenerateDataDictDAL(Params4DataDictDAL param) { StringBuilder builder = new StringBuilder(); //.Net Framework的名字空间 builder.AppendLine("using System;"); builder.AppendLine("using System.Collections.Generic;"); builder.AppendLine("using System.Text;"); builder.AppendLine("using System.Data;"); //引用的自定义名字空间 foreach (string space in param.UsingNamespace) builder.AppendLine("using " + space + ";"); //生成单元注释部分 CreateComment(builder, param.ConcretelyName); builder.AppendLine("namespace " + param.DAL_Namespace); //当前数据层所在的名字空间 builder.AppendLine("{"); builder.AppendLine(" /// <summary>"); builder.AppendLine(" /// " + param.DAL_Name); builder.AppendLine(" /// </summary>"); builder.AppendLine(" public class " + param.DAL_Name + " : dalBaseDataDict"); //基类 builder.AppendLine(" {"); builder.AppendLine(" /// <summary>"); builder.AppendLine(" /// 构造器"); builder.AppendLine(" /// </summary>"); builder.AppendLine(" /// <param name=\"loginer\">当前登录用户</param>"); builder.AppendLine(" public " + param.DAL_Name + "(Loginer loginer): base(loginer)"); //构造器 builder.AppendLine(" {"); builder.AppendLine(" _KeyName = " + param.ORM_Name + ".__KeyName; //主键字段"); builder.AppendLine(" _TableName = " + param.ORM_Name + ".__TableName;//表名"); builder.AppendLine(" _ModelType = typeof(" + param.ORM_Name + ");"); builder.AppendLine(" }"); builder.AppendLine(""); builder.AppendLine(" /// <summary>"); builder.AppendLine(" /// 根据表名获取该表的SQL命令生成器"); builder.AppendLine(" /// </summary>"); builder.AppendLine(" /// <param name=\"tableName\">表名</param>"); builder.AppendLine(" /// <returns></returns>"); builder.AppendLine(" protected override IGenerateSqlCommand CreateSqlGenerator(string tableName)"); builder.AppendLine(" {"); builder.AppendLine(" Type ORM = null;"); builder.AppendLine(" if (tableName == " + param.ORM_Name + ".__TableName) ORM = typeof(" + param.ORM_Name + ");"); builder.AppendLine(" if (ORM == null) throw new Exception(tableName + \"表没有ORM模型!\");"); builder.AppendLine(" return new GenerateSqlCmdByTableFields(ORM);"); builder.AppendLine(" }"); builder.AppendLine(""); builder.AppendLine(" }"); builder.AppendLine("}"); return builder.ToString(); }