/// <summary> /// 自动生成逻辑层 /// </summary> public void CreateComponent(string className) { //获取当前文件夹路径 //string currPath = Application.StartupPath; //检查是否存在文件夹 string subPath = subPathName + "/Component"; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } string targetPath = subPath + "\\ComponentBase.cs";//结果保存到桌面 // FileStream fs = new FileStream(targetPath, FileMode.Append); string sourcePath = PathName() + "\\ComponentBase.cs";; bool isrewrite = false; if (!File.Exists(targetPath)) // 返回bool类型,存在返回true,不存在返回false { System.IO.File.Copy(sourcePath, targetPath, isrewrite); //不存在则复制文件 } List <string> code_list = new List <string>(); code_list.Add("using System;"); code_list.Add("using System.Collections.Generic;"); code_list.Add("using System.Linq;"); code_list.Add("using System.Text;"); code_list.Add(" using System.Threading.Tasks;"); code_list.Add("using Manager;"); code_list.Add("using Model;"); code_list.Add(" namespace Component"); code_list.Add("{"); code_list.Add(" public class " + className + "Component :ComponentBase<" + className + "," + className + "Manager>"); code_list.Add("{"); code_list.Add(" public IList<" + className + "> GetAll()"); code_list.Add("{"); code_list.Add(" return manager.GetAll();"); code_list.Add("}"); code_list.Add("public int Update(" + className + " dl)"); code_list.Add("{"); code_list.Add("return manager.Update(dl);"); code_list.Add("}"); code_list.Add("public int Delete(" + className + " dl)"); code_list.Add("{"); code_list.Add("return manager.Delete(dl);"); code_list.Add("}"); code_list.Add("}"); code_list.Add("}"); FileOperate.FileWrite(code_list, "" + subPath + "/" + className + "Component.cs"); Controllers(className); }
/// <summary> /// 创建实体 /// </summary> private void CreateModel() { //获取当前文件夹路径 //string currPath = Application.StartupPath; //检查是否存在文件夹 string subPath = subPathName + "\\Model"; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } DataTable dt_Cloumns = this.dgvColumns.DataSource as DataTable; List <string> code_list = new List <string>(); code_list.Add("using System;"); code_list.Add("public class " + this.tvTables.SelectedNode.Text + ""); code_list.Add("{"); foreach (DataRow row in dt_Cloumns.Rows) { string c_type = FileOperate.SqlTypeTope(row["type"].ToString()); string c_name = row["name"].ToString(); code_list.Add("public " + c_type + " " + c_name.Substring(0, 1).ToUpper() + c_name.Substring(1) + "{get; set;}"); } code_list.Add("}"); FileOperate.FileWrite(code_list, "" + subPath + "\\" + this.tvTables.SelectedNode.Text + ".cs"); //CreateManger(); }
/// <summary> /// 自动生成控制器 /// </summary> public void Controllers(string className) { //获取当前文件夹路径 //string currPath = Application.StartupPath; //检查是否存在文件夹 string subPath = subPathName + "/Controllers"; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } List <string> code_list = new List <string>(); code_list.Add("using System;"); code_list.Add("using Component;"); code_list.Add("using System.Collections.Generic;"); code_list.Add(" using System.Linq;"); code_list.Add("using System.Text;"); code_list.Add("using System.Threading.Tasks;"); code_list.Add(" namespace Controllers"); code_list.Add("{"); code_list.Add("public class " + className + "Controller : Controller"); code_list.Add("{"); code_list.Add("" + className + "Component cp=new " + className + "Component()"); code_list.Add("public ActionResult Index()"); code_list.Add("{"); code_list.Add("" + className + " com = new " + className + "();"); code_list.Add(" cp.GetAll();"); code_list.Add(" cp.Delete(com);"); code_list.Add(" cp.Update(com);"); code_list.Add("return View();"); code_list.Add("}"); code_list.Add("}"); code_list.Add("}"); FileOperate.FileWrite(code_list, "" + subPath + "/" + className + "Controller.cs"); MessageBox.Show("在目录中Code文件夹查看分!!!"); }
/// <summary> /// 自动生成数据访问层 /// </summary> private void CreateManger(string ClassName) { //获取当前文件夹路径 //string currPath = Application.StartupPath; //检查是否存在文件夹 string subPath = subPathName + "/Manager"; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } string targetPath = subPath + "\\ManagerBase.cs";//结果保存到桌面 // FileStream fs = new FileStream(targetPath, FileMode.Append); string sourcePath = PathName() + "\\ManagerBase.cs"; bool isrewrite = false; if (!File.Exists(targetPath)) // 返回bool类型,存在返回true,不存在返回false { System.IO.File.Copy(sourcePath, targetPath, isrewrite); //不存在则复制文件 } List <string> code_list = new List <string>(); code_list.Add("using System;"); code_list.Add("using Model;"); code_list.Add("using System.Collections.Generic;"); code_list.Add("using System.Linq;"); code_list.Add("using System.Text;"); code_list.Add(" using System.Threading.Tasks;"); code_list.Add(" namespace Manager"); code_list.Add("{"); code_list.Add(" public class " + ClassName + "Manager :ManagerBase<" + ClassName + ">"); code_list.Add("{"); code_list.Add("}"); code_list.Add("}"); FileOperate.FileWrite(code_list, "" + subPath + "/" + ClassName + "Manager.cs"); CreateComponent(ClassName); }
public void CreateModel(string strJson) { subPath = subPathName + "\\" + ModelName.Text + ""; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary <string, Object> json = (Dictionary <string, Object>)serializer.DeserializeObject(strJson); FileOperate.FWrite("using System;\r\nusing System.Collections.Generic;\r\nnamespace " + ModelName.Text.Trim() + "\r\n{\r\npublic class " + TableName + "\r\n{\r\n", "" + subPath + "\\" + TableName + ".cs"); if (json != null) { List <string> keys = json.Keys.ToList(); foreach (var s in keys) { findNode(json[s], s, "test", true); } } DataRow[] Rowlist = IcreateType.GetColumns(TableName).Select("type <> 'jsonb' and type <>'json'"); for (int i = 0; i < Rowlist.Length; i++) { string classType = FileOperate.SqlTypeTope(Rowlist[i][1].ToString()); if (classType == "") { classType = "string"; } string className = Rowlist[i][0].ToString(); FileOperate.FWrite("public " + classType + " " + className + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } FileOperate.FWrite("}\r\n}\r\n", "" + subPath + "\\" + TableName + ".cs"); CreateManger(TableName); CreateComponent((DataTable)dgvColumns.DataSource); CreateControllers((DataTable)dgvColumns.DataSource); }
/// <summary> /// 自动生成数据访问层 /// </summary> private void CreateManger(string ClassName) { ClassName = StrToUpper(ClassName); //检查是否存在文件夹 MgPath = subPathName + "/" + ManagerName.Text + ""; if (false == System.IO.Directory.Exists(MgPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(MgPath); } string targetPath = MgPath + "\\ManagerBase.cs";//结果保存到桌面 // FileStream fs = new FileStream(targetPath, FileMode.Append); string sourcePath = PathName() + "\\ManagerBase.cs"; bool isrewrite = false; if (!File.Exists(targetPath)) // 返回bool类型,存在返回true,不存在返回false { System.IO.File.Copy(sourcePath, targetPath, isrewrite); //不存在则复制文件 } List <string> code_list = new List <string>(); code_list.Add("using System;"); code_list.Add(string.Format("using {0};", ModelName.Text.Trim())); code_list.Add("using System.Collections.Generic;"); code_list.Add("using System.Linq;"); code_list.Add("using System.Text;"); code_list.Add(" using System.Threading.Tasks;"); code_list.Add(" namespace " + ManagerName.Text.Trim() + ""); code_list.Add("{"); code_list.Add(" public class " + ClassName + "Manager :ManagerBase<" + ClassName + ">"); code_list.Add("{"); code_list.Add("}"); code_list.Add("}"); FileOperate.FileWrite(code_list, "" + MgPath + "/" + ClassName + "Manager.cs"); }
public void findNode(object obj, string fileName, string fastName, bool IsF) { if (null == obj) { if (IsF) { FileOperate.FWrite("public string " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.FWrite("public string " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } return; } var type = obj.GetType(); if (type == typeof(int)) { if (IsF) { FileOperate.FWrite("public int " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.FWrite("public int " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } return; } else if (type == typeof(string)) { if (IsF) { FileOperate.FWrite("public string " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.FWrite("public string " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } return; } else if (type == typeof(bool)) { if (IsF) { FileOperate.FWrite("public bool " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.FWrite("public bool " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } return; } else if (type == typeof(Dictionary <string, Object>))//键值对 { Dictionary <string, Object> chi = obj as Dictionary <string, Object>; List <string> keys = chi.Keys.ToList(); if (IsF) { FileOperate.FWrite("public " + fileName + "Content " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.FWrite("public " + fileName + "Content " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } FileOperate.FWrite("using System;\r\nusing System.Collections.Generic;\r\nnamespace " + ModelName.Text.Trim() + "\r\n{\r\npublic class " + fileName + "Content\r\n{\r\n", "" + subPath + "\\" + fileName + "Content.cs"); int i = 0; foreach (var s in keys) { i++; findNode(chi[s], s, fileName, false); if (i == keys.Count) { FileOperate.FWrite("}\r\n}\r\n", "" + subPath + "\\" + fileName + "Content.cs"); } } } else //集合 { Object[] objArr = obj as Object[]; Dictionary <string, Object> chi = objArr[0] as Dictionary <string, Object>; List <string> keys = chi.Keys.ToList(); if (IsF) { FileOperate.FWrite("public List<" + fileName + "Content> " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + TableName + ".cs"); } else { FileOperate.FWrite("public List<" + fileName + "Content> " + fileName + " {get;set;}\r\n", "" + subPath + "\\" + fastName + "Content.cs"); } FileOperate.FWrite("using System;\r\nusing System.Collections.Generic;\r\nnamespace " + ModelName.Text.Trim() + "\r\n{\r\npublic class " + fileName + "Content\r\n{\r\n", "" + subPath + "\\" + fileName + "Content.cs"); int i = 0; foreach (var s in keys) { i++; findNode(chi[s], s, fileName, false); if (i == keys.Count) { FileOperate.FWrite("}\r\n}\r\n", "" + subPath + "\\" + fileName + "Content.cs"); } } } }
/// <summary> /// 自动生成控制器 /// </summary> /// <param name="dataJson">服务表</param> public void CreateControllers(DataTable dataJson) { //获取当前文件夹路径 //string currPath = Application.StartupPath; //检查是否存在文件夹 string subPath = subPathName + "\\" + ControllersName.Text; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } string className = dataJson.Rows[0][0].ToString(); List <string> code_list = new List <string>(); code_list.Add("using System;"); code_list.Add("using " + ConpontentName.Text + ";"); code_list.Add("using System.Collections.Generic;"); code_list.Add("using System.Linq;"); code_list.Add("using System.Text;"); code_list.Add("using System.Web.Mvc;"); code_list.Add("using WebApiModel;"); code_list.Add("using System.Threading.Tasks;"); code_list.Add("namespace Controllers"); code_list.Add("{"); code_list.Add("public class " + className + "Controller : Controller"); code_list.Add("{"); code_list.Add("public string Get()"); code_list.Add("{"); code_list.Add("string str = BaseFunction.GetControllerInfo<" + className + "Controller>();"); code_list.Add("return str;"); code_list.Add("}"); int i = 0; foreach (DataRow dr in dataJson.Rows) { string strServerName = dr["服务名"].ToString(); if (null == strServerName || strServerName == "" || strServerName.Length == 0) { continue; } string strType = this.dgvColumns.Rows[i].Cells["类型"].FormattedValue.ToString(); string strQueryCondition = dr["查询条件"].ToString(); code_list.Add(string.Format("[{0}(\"{1}\")]", strType, strServerName)); code_list.Add("[displayname(name=\"\")]"); code_list.Add("[note(name=\"\")]"); code_list.Add("[paraoutname(name=\"\")]"); code_list.Add("[schemaVal(name=\"\")]"); if (strServerName == "GetAll") { code_list.Add(string.Format("public List<{0}> {1}({2})", className, strServerName, strQueryCondition)); } else { code_list.Add(string.Format("public int {0}({1})", strServerName, strQueryCondition)); } code_list.Add("{"); code_list.Add(className + "Component cp=new " + className + "Component();"); List <string> typeVlue = retSrtName(strQueryCondition); string para = string.Empty; foreach (string s in typeVlue) { para += s + ","; } if (para.Length > 0) { para = para.Substring(0, para.Length - 1); } code_list.Add(string.Format("return cp.{0}({1});", strServerName, para)); code_list.Add("}"); i++; } code_list.Add("}"); code_list.Add("}"); FileOperate.FileWrite(code_list, "" + subPath + "/" + className + "Controller.cs"); }
/// <summary> /// 自动生成逻辑层 /// </summary> /// <param name="datasource">服务表</param> public void CreateComponent(DataTable datasource) { //获取当前文件夹路径 //string currPath = Application.StartupPath; //检查是否存在文件夹 CpPath = subPathName + "\\" + ConpontentName.Text + ""; if (false == System.IO.Directory.Exists(CpPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(CpPath); } string targetPath = CpPath + "\\ComponentBase.cs";//结果保存到桌面 // FileStream fs = new FileStream(targetPath, FileMode.Append); string sourcePath = PathName() + "\\ComponentBase.cs";; bool isrewrite = false; if (!File.Exists(targetPath)) // 返回bool类型,存在返回true,不存在返回false { System.IO.File.Copy(sourcePath, targetPath, isrewrite); //不存在则复制文件 } List <string> code_list = new List <string>(); code_list.Add("using System;"); code_list.Add("using System.Collections.Generic;"); code_list.Add("using System.Linq;"); code_list.Add("using System.Text;"); code_list.Add(" using System.Threading.Tasks;"); code_list.Add(string.Format("using {0};", ManagerName.Text.Trim())); code_list.Add(string.Format("using {0};", ModelName.Text.Trim())); code_list.Add(" namespace " + ConpontentName.Text.Trim() + ""); code_list.Add("{"); code_list.Add(" public class " + TableName + "Component :ComponentBase<" + TableName + "," + TableName + "Manager>"); code_list.Add("{"); foreach (DataRow dr in datasource.Rows) { string strTypeName = dr["服务名"].ToString(); string strQueryCondition = dr["查询条件"].ToString(); if (strTypeName == "GetAll") { code_list.Add(string.Format(" public int {0} ({1})", strTypeName, strQueryCondition)); code_list.Add("{"); List <string> typeVlue = retSrtName(strQueryCondition); string para = string.Empty; foreach (string s in typeVlue) { para += s + ","; } if (para.Length > 0) { para = para.Substring(0, para.Length - 1); } code_list.Add(string.Format("return manager.{0}({1});", strTypeName, para)); code_list.Add("}"); } else { code_list.Add(string.Format(" public int {0} ({1})", strTypeName, strQueryCondition)); code_list.Add("{"); List <string> typeVlue = retSrtName(strQueryCondition); string para = string.Empty; foreach (string s in typeVlue) { para += s + ","; } if (para.Length > 0) { para = para.Substring(0, para.Length - 1); } code_list.Add(string.Format("return manager.{0}({1});", strTypeName, para)); code_list.Add("}"); } } code_list.Add("}"); code_list.Add("}"); FileOperate.FileWrite(code_list, "" + CpPath + "/" + TableName + "Component.cs"); }
private void button1_Click(object sender, EventArgs e) { //if (t_path.Text.Trim() == "") //{ // MessageBox.Show("请选择存放的路径!!"); // return; //} //subPathName = "" + t_path.Text + "Code"; //创建文件夹 //if (false == System.IO.Directory.Exists(subPathName)) //{ // //创建pic文件夹 // System.IO.Directory.CreateDirectory(subPathName); //} //CreateModel(); if (t_path.Text.Trim() == "") { MessageBox.Show("请选择存放的路径!!"); return; } subPathName = "" + t_path.Text + "Code"; //创建文件夹 if (false == System.IO.Directory.Exists(subPathName)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPathName); } int count = dgvColumns.Rows.Count; //获取每张表中的列是json格式的 for (int i = 0; i < count; i++) { JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类 string str_json_out = dgvColumns.Rows[i].Cells["Json格式"].Value.ToString(); JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary <string, Object> json = (Dictionary <string, Object>)serializer.DeserializeObject(str_json_out); //获取当前文件夹路径 //string currPath = Application.StartupPath; //检查是否存在文件夹 string subPath = subPathName + "\\Model"; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } Find1(json); IList <string> allKeys = json.Keys.ToList(); List <string> code_list = new List <string>(); code_list.Add("using System;"); code_list.Add("namespace Model"); code_list.Add("{"); code_list.Add("public class " + this.tvTables.SelectedNode.Text + ""); code_list.Add("{"); for (int j = 0; j < allKeys.Count; j++) { code_list.Add("public string " + allKeys[j].ToString() + " {get;set;}"); } CreateManger(this.tvTables.SelectedNode.Text); foreach (KeyValuePair <string, object> item in json) { if (item.Value.GetType() == typeof(Dictionary <string, Object>)) { code_list.Add("public " + item.Key + "Content " + item.Key + " {get;set;}"); Dictionary <string, Object> list = item.Value as Dictionary <string, Object>; List <string> code_lists = new List <string>(); code_lists.Add("using System;"); code_lists.Add("namespace Model"); code_lists.Add("{"); code_lists.Add("public " + item.Key + "Content " + item.Key + " "); code_lists.Add("{"); CreateManger(item.Key + "Content"); foreach (KeyValuePair <string, object> items in list) { code_lists.Add("public string " + items.Key + " {get;set;}"); } code_lists.Add("}"); code_lists.Add("}"); FileOperate.FileWrite(code_lists, "" + subPath + "\\ " + item.Key + "Content .cs"); } if (item.Value.GetType() == typeof(Object[])) { code_list.Add("public List<" + item.Key + "Content> " + item.Key + " {get;set;}"); object[] list = item.Value as Object[]; List <string> code_listss = new List <string>(); code_listss.Add("using System;"); code_listss.Add("namespace Model"); code_listss.Add("{"); code_listss.Add("public " + item.Key + "Content " + item.Key + " "); code_listss.Add("{"); CreateManger(item.Key + "Content"); Dictionary <string, Object> a = list[0] as Dictionary <string, Object>; foreach (KeyValuePair <string, Object> items in a) { code_listss.Add("public string " + items.Key + " {get;set;}"); } code_listss.Add("}"); code_listss.Add("}"); FileOperate.FileWrite(code_listss, "" + subPath + "\\ " + item.Key + "Content .cs"); } } code_list.Add("}"); code_list.Add("}"); FileOperate.FileWrite(code_list, "" + subPath + "\\" + this.tvTables.SelectedNode.Text + ".cs"); } }
public void CreateModel(string strJson) { lists.Clear(); subPath = subPathName + "\\" + ModelName.Text + ""; if (false == System.IO.Directory.Exists(subPath)) { //创建pic文件夹 System.IO.Directory.CreateDirectory(subPath); } JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary <string, Object> json = (Dictionary <string, Object>)serializer.DeserializeObject(strJson); if (json != null) { List <string> keys = json.Keys.ToList(); List <string> fList = new List <string>(); lists.Add(fList); fList.Add("[Serializable]"); fList.Add("public class " + TableName); fList.Add("{"); foreach (var s in keys) { //findNode(json[s], s, "test", true); findNode2(s, json[s], fList); } DataRow[] Rowlist = IcreateType.GetColumns(tableName).Select("type <> 'jsonb' and type <>'json'"); for (int i = 0; i < Rowlist.Length; i++) { string classType = FileOperate.SqlTypeTope(Rowlist[i][1].ToString()); if (classType == "") { classType = "string"; } string className = Rowlist[i][0].ToString(); fList.Add("public " + classType + " " + className + " {get;set;}"); } fList.Add("}"); List <string> header = new List <string>(); header.Add("using System;"); header.Add("using System.Collections.Generic;"); header.Add("namespace " + ModelName.Text.Trim()); header.Add("{"); FileOperate.FileWrite(header, subPath + "\\" + TableName + ".cs"); foreach (List <string> clist in lists) { FileOperate.WriteAppendList(clist, subPath + "\\" + TableName + ".cs"); } FileOperate.WriteAppendStr("\r\n}", subPath + "\\" + TableName + ".cs"); } else { List <string> header = new List <string>(); header.Add("using System;"); header.Add("using System.Collections.Generic;"); header.Add("namespace " + ModelName.Text.Trim()); header.Add("{"); header.Add("[Serializable]"); header.Add("public class " + TableName); header.Add("{"); FileOperate.FileWrite(header, subPath + "\\" + TableName + ".cs"); DataRow[] Rowlist = IcreateType.GetColumns(tableName).Select("type <> 'jsonb' and type <>'json'"); for (int i = 0; i < Rowlist.Length; i++) { string classType = FileOperate.SqlTypeTope(Rowlist[i][1].ToString()); if (classType == "") { classType = "string"; } string className = Rowlist[i][0].ToString(); header.Add("public " + classType + " " + className + " {get;set;}"); } header.Add("}"); header.Add("}"); FileOperate.FileWrite(header, subPath + "\\" + TableName + ".cs"); } CreateManger(TableName); CreateComponent((DataTable)dgvColumns.DataSource); CreateControllers((DataTable)dgvColumns.DataSource); MessageBox.Show("生成成功,请查看!!", "提示"); }