/// <summary> /// 得到工厂层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetFactoryClass(Model.CodeCreate param) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); StringBuilder factory = new StringBuilder(import.GetImport_Factory()); factory.Append("namespace " + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Factory + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "\r\n"); factory.Append("{\r\n"); factory.Append("\tpublic class " + param.ClassName + "\r\n"); factory.Append("\t{\r\n"); factory.Append("\t\t/// <summary>\r\n"); factory.Append("\t\t/// 创建实例对象\r\n"); factory.Append("\t\t/// </summary>\r\n"); factory.Append("\t\tpublic static " + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Interface + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + ".I" + param.ClassName + " CreateInstance()\r\n");; factory.Append("\t\t{\r\n"); factory.Append("\t\t\treturn Factory.CreateInstance(\"" + param.ClassName + "\") as " + param.CNSC.Interface + ".I" + param.ClassName + ";\r\n"); factory.Append("\t\t}\r\n"); factory.Append("\t}\r\n"); factory.Append("}\r\n"); return(factory.ToString()); }
/// <summary> /// 得到实体层 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetModelClass(Model.CodeCreate param) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); StringBuilder model = new StringBuilder(import.GetImport_Model()); model.Append("namespace " + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "\r\n"); model.Append("{\r\n"); model.Append("\t[Serializable]\r\n"); model.Append("\tpublic class " + param.ClassName + "\r\n"); model.Append("\t{\r\n"); foreach (var field in fields) { model.Append("\t\t/// <summary>\r\n"); model.Append("\t\t/// " + (field.Note.IsNullOrEmpty() ? field.Name : field.Note) + "\r\n"); model.Append("\t\t/// </summary>\r\n"); model.Append("\t\t[DisplayName(\"" + (field.Note.IsNullOrEmpty() ? field.Name : field.Note) + "\")]\r\n"); model.Append("\t\tpublic " + field.DotNetType + " " + field.Name + " { get; set; }\r\n\r\n"); } model.Append("\t}\r\n"); model.Append("}\r\n"); return(model.ToString()); }
/// <summary> /// 得到服务器所有数据库 /// </summary> /// <param name="connectionString"></param> /// <returns></returns> public List <string> GetDatabaseList(string serverID) { Model.Servers server = Common.Config.GetServer(serverID); List <string> dbList = new List <string>(); if (server == null) { return(dbList); } else { dbList.Add(System.IO.Path.GetFileName(server.Server)); } return(dbList); }
/// <summary> /// 得到数据层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetBuilderClass(Model.CodeCreate param, string templateName) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); string filePath = System.IO.Directory.GetCurrentDirectory() + "\\Template\\" + templateName;//"DAL.txt"; string value = ""; FileStream fs = null; StreamReader sr = null; try { if (!File.Exists(filePath)) { return(string.Empty); } fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs, System.Text.Encoding.UTF8); value = sr.ReadToEnd(); } catch (Exception ex) { Debug.WriteLine(ex); } finally { if (sr != null) { sr.Close(); } if (fs != null) { fs.Close(); } } string result = Engine.Razor.RunCompile(value, Guid.NewGuid().ToString(), null, new { param = param, fields = fields }); result = result.Replace("[*]", "@"); return(result.ToString()); }
/// <summary> /// 得到数据层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetDataClass(Model.CodeCreate param) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); StringBuilder data = new StringBuilder(import.GetImport_Data()); data.Append("namespace " + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Data + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "\r\n"); data.Append("{\r\n"); data.Append("\tpublic class " + param.ClassName + (param.BuilderType == Model.BuilderType.Factory ? " : " + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Interface + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + ".I" + param.ClassName : "") + "\r\n"); data.Append("\t{\r\n"); data.Append("\t\tprivate DBHelper dbHelper = new DBHelper();\r\n"); data.Append("\t\t/// <summary>\r\n"); data.Append("\t\t/// 构造函数\r\n"); data.Append("\t\t/// </summary>\r\n"); data.Append("\t\tpublic " + param.ClassName + "()\r\n"); data.Append("\t\t{\r\n"); data.Append("\t\t}\r\n"); //新增记录 if (param.MethodList.Contains(Model.BuilderMethods.Add)) { data.Append(GetAddMethod(fields, param)); } //更新记录 if (param.MethodList.Contains(Model.BuilderMethods.Update) && fields.Where(p => p.IsPrimaryKey).Count() > 0) { data.Append(GetUpdateMethod(fields, param)); } //删除记录 if (param.MethodList.Contains(Model.BuilderMethods.Delete) && fields.Where(p => p.IsPrimaryKey).Count() > 0) { data.Append(GetDeleteMethod(fields, param)); } //转换List data.Append(GetConvertDataReaderToListMethod(fields, param)); //查询所有记录 if (param.MethodList.Contains(Model.BuilderMethods.SelectAll)) { data.Append(GetAllMethod(fields, param)); } //查询记录数 if (param.MethodList.Contains(Model.BuilderMethods.Count)) { data.Append(GetCountMethod(fields, param)); } //查询主键记录 if (param.MethodList.Contains(Model.BuilderMethods.SelectByKey) && fields.Where(p => p.IsPrimaryKey).Count() > 0) { data.Append(GetByKeyMethod(fields, param)); } data.Append("\t}\r\n"); data.Append("}"); return(data.ToString()); }
/// <summary> /// 得到业务层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetBusinessClass(Model.CodeCreate param) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); //自增列 var Identitys = fields.Where(p => p.IsIdentity); var NotIdeneitys = fields.Where(p => !p.IsIdentity); bool HasIdentity = Identitys.Count() > 0; //主键 var Primarykeys = fields.Where(p => p.IsPrimaryKey); var NotPrimarykeys = fields.Where(p => !p.IsPrimaryKey); bool HasPrimarykey = Primarykeys.Count() > 0; StringBuilder business = new StringBuilder(import.GetImport_Business()); business.Append("namespace " + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Business + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "\r\n"); business.Append("{\r\n"); business.Append("\tpublic class " + param.ClassName + "\r\n"); business.Append("\t{\r\n"); business.Append("\t\tprivate " + (param.BuilderType == Model.BuilderType.Factory ? param.CNSC.Interface + ".I" : param.CNSC.Data + ".") + param.ClassName + " data" + param.ClassName + ";\r\n"); business.Append("\t\tpublic " + param.ClassName + "()\r\n"); business.Append("\t\t{\r\n"); business.Append("\t\t\tthis.data" + param.ClassName + " = " + (param.BuilderType == Model.BuilderType.Factory ? param.CNSC.Factory + "." + param.ClassName + ".CreateInstance();" : "new " + param.CNSC.Data + "." + param.ClassName + "();") + "\r\n"); business.Append("\t\t}\r\n"); if (param.MethodList.Contains(Model.BuilderMethods.Add)) { business.Append("\t\t/// <summary>\r\n"); business.Append("\t\t/// 新增\r\n"); business.Append("\t\t/// </summary>\r\n"); business.Append("\t\tpublic " + (HasIdentity ? Identitys.First().DotNetType : "int") + " Add(" + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "." + param.ClassName + " model)\r\n"); business.Append("\t\t{\r\n"); business.Append("\t\t\treturn data" + param.ClassName + ".Add(model);\r\n"); business.Append("\t\t}\r\n"); } if (param.MethodList.Contains(Model.BuilderMethods.Update) && HasPrimarykey) { business.Append("\t\t/// <summary>\r\n"); business.Append("\t\t/// 更新\r\n"); business.Append("\t\t/// </summary>\r\n"); business.Append("\t\tpublic int Update(" + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "." + param.ClassName + " model)\r\n"); business.Append("\t\t{\r\n"); business.Append("\t\t\treturn data" + param.ClassName + ".Update(model);\r\n"); business.Append("\t\t}\r\n"); } if (param.MethodList.Contains(Model.BuilderMethods.SelectAll)) { business.Append("\t\t/// <summary>\r\n"); business.Append("\t\t/// 查询所有记录\r\n"); business.Append("\t\t/// </summary>\r\n"); business.Append("\t\tpublic List<" + param.NameSpace + (param.NameSpace.IsNullOrEmpty() ? "" : ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "." + param.ClassName + "> GetAll()\r\n"); business.Append("\t\t{\r\n"); business.Append("\t\t\treturn data" + param.ClassName + ".GetAll();\r\n"); business.Append("\t\t}\r\n"); } if (param.MethodList.Contains(Model.BuilderMethods.SelectByKey) && HasPrimarykey) { #region 查询单条记录 business.Append("\t\t/// <summary>\r\n"); business.Append("\t\t/// 查询单条记录\r\n"); business.Append("\t\t/// </summary>\r\n"); business.Append("\t\tpublic " + param.NameSpace + (param.NameSpace.IsNullOrEmpty() ? "" : ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "." + param.ClassName + " Get("); foreach (var field in Primarykeys) { business.Append(field.DotNetType + " " + field.Name.ToLower()); business.Append(field.Name != Primarykeys.Last().Name ? ", " : ""); } business.Append(")\r\n"); business.Append("\t\t{\r\n"); business.Append("\t\t\treturn data" + param.ClassName + ".Get("); foreach (var field in Primarykeys) { business.Append(field.Name.ToLower()); business.Append(field.Name != Primarykeys.Last().Name ? ", " : ""); } business.Append(");\r\n"); business.Append("\t\t}\r\n"); #endregion } if (param.MethodList.Contains(Model.BuilderMethods.Delete) && HasPrimarykey) { #region 除 business.Append("\t\t/// <summary>\r\n"); business.Append("\t\t/// 删除\r\n"); business.Append("\t\t/// </summary>\r\n"); business.Append("\t\tpublic int Delete("); foreach (var field in Primarykeys) { business.Append(field.DotNetType + " " + field.Name.ToLower()); business.Append(field.Name != Primarykeys.Last().Name ? ", " : ""); } business.Append(")\r\n"); business.Append("\t\t{\r\n"); business.Append("\t\t\treturn data" + param.ClassName + ".Delete("); foreach (var field in Primarykeys) { business.Append(field.Name.ToLower()); business.Append(field.Name != Primarykeys.Last().Name ? ", " : ""); } business.Append(");\r\n"); business.Append("\t\t}\r\n"); #endregion } if (param.MethodList.Contains(Model.BuilderMethods.Count)) { business.Append("\t\t/// <summary>\r\n"); business.Append("\t\t/// 查询记录条数\r\n"); business.Append("\t\t/// </summary>\r\n"); business.Append("\t\tpublic long GetCount()\r\n"); business.Append("\t\t{\r\n"); business.Append("\t\t\treturn data" + param.ClassName + ".GetCount();\r\n"); business.Append("\t\t}\r\n"); } business.Append("\t}\r\n"); business.Append("}\r\n"); return(business.ToString()); }
/// <summary> /// 得到一个表中所有字段 /// </summary> /// <param name="serverID"></param> /// <param name="dbName"></param> /// <param name="tableName"></param> /// <returns></returns> public List <Model.Fields> GetFields(string serverID, string dbName, string tableName) { List <Model.Fields> fieldsList = new List <Model.Fields>(); Model.Servers server = Common.Config.GetServer(serverID); if (server == null) { return(fieldsList); } using (SqlConnection conn = new SqlConnection(Common.Config.GetConnectionString(serverID, dbName))) { conn.Open(); string sql = server.Type == Model.DatabaseType.SqlServer2000 ? string.Format(@"SELECT name=a.name,isidentity=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then 1 else 0 end,isprimarykey=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (SELECT name FROM sysindexes WHERE indid in(SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then 1 else 0 end,type=b.name,bbyte=a.length,length=COLUMNPROPERTY(a.id,a.name,'PRECISION'), dec=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),isnull=case when a.isnullable=1 then 1 else 0 end, isdefault=isnull(e.text,''),note=isnull(g.[value],'')FROM syscolumns a left join systypes b on a.xusertype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sysproperties g on a.id=g.id and a.colid=g.smallid left join sysproperties f on d.id=f.id and f.smallid=0 where d.name='{0}' order by a.id,a.colorder", tableName) : string.Format(@"select a.name as f_name,b.name as t_name,[length],a.isnullable as is_null from sys.syscolumns a inner join sys.types b on b.user_type_id=a.xtype where object_id('{0}')=id order by a.colid", tableName); using (SqlCommand cmd = new SqlCommand(sql, conn)) { SqlDataReader dr = cmd.ExecuteReader(); if (server.Type != Model.DatabaseType.SqlServer2000) { while (dr.Read()) { fieldsList.Add(new Model.Fields() { Name = dr.GetString(0), Type = dr.GetString(1), Length = GetFieldLength(dr.GetString(1), dr.GetInt16(2)), IsNull = 1 == dr.GetInt32(3), IsPrimaryKey = IsPrimaryKey(serverID, dbName, tableName, dr.GetString(0)), IsIdentity = IsIdentity(serverID, dbName, tableName, dr.GetString(0)), DotNetType = GetFieldType(dr.GetString(1), 1 == dr.GetInt32(3)), DotNetSqlType = GetFieldSqlType(dr.GetString(1)), Note = GetFieldNote(serverID, dbName, tableName, dr.GetString(0)) }); } } else { while (dr.Read()) { fieldsList.Add(new Model.Fields() { Name = dr["name"].ToString(), Type = dr["type"].ToString(), Length = GetFieldLength(dr["type"].ToString(), dr["length"].ToString().ToInt()), IsNull = "1" == dr["isnull"].ToString(), IsPrimaryKey = "1" == dr["isprimarykey"].ToString(), IsIdentity = "1" == dr["isidentity"].ToString(), DotNetType = GetFieldType(dr["type"].ToString(), "1" == dr["isnull"].ToString()), DotNetSqlType = GetFieldSqlType(dr["type"].ToString()), Note = dr["note"].ToString() }); } } dr.Close(); dr.Dispose(); } } return(fieldsList); }
/// <summary> /// 得到接口层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetInterfaceClass(Model.CodeCreate param) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); //自增列 var Identitys = fields.Where(p => p.IsIdentity); var NotIdeneitys = fields.Where(p => !p.IsIdentity); bool HasIdentity = Identitys.Count() > 0; //主键 var Primarykeys = fields.Where(p => p.IsPrimaryKey); var NotPrimarykeys = fields.Where(p => !p.IsPrimaryKey); bool HasPrimarykey = Primarykeys.Count() > 0; /*StringBuilder inter = new StringBuilder(import.GetImport_Interface()); * inter.Append("namespace " + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Interface + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "\r\n"); * inter.Append("{\r\n"); * * inter.Append("\tpublic interface I" + param.ClassName + "\r\n"); * inter.Append("\t{\r\n"); * * if (param.MethodList.Contains(Model.BuilderMethods.Add)) * { * inter.Append("\t\t/// <summary>\r\n"); * inter.Append("\t\t/// 新增\r\n"); * inter.Append("\t\t/// </summary>\r\n"); * inter.Append("\t\t" + (HasIdentity ? Identitys.First().DotNetType : "int") + " Add(" + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "." + param.ClassName + " model);\r\n\r\n"); * } * * if (param.MethodList.Contains(Model.BuilderMethods.Update) && HasPrimarykey) * { * inter.Append("\t\t/// <summary>\r\n"); * inter.Append("\t\t/// 更新\r\n"); * inter.Append("\t\t/// </summary>\r\n"); * inter.Append("\t\tint Update(" + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "." + param.ClassName + " model);\r\n\r\n"); * } * * if (param.MethodList.Contains(Model.BuilderMethods.SelectAll)) * { * inter.Append("\t\t/// <summary>\r\n"); * inter.Append("\t\t/// 查询所有记录\r\n"); * inter.Append("\t\t/// </summary>\r\n"); * inter.Append("\t\tList<" + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "." + param.ClassName + "> GetAll();\r\n\r\n"); * } * * if (param.MethodList.Contains(Model.BuilderMethods.SelectByKey) && HasPrimarykey) * { #region 查询单条记录 * inter.Append("\t\t/// <summary>\r\n"); * inter.Append("\t\t/// 查询单条记录\r\n"); * inter.Append("\t\t/// </summary>\r\n"); * inter.Append("\t\tModel" + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "." + param.ClassName + " Get("); * * foreach (var field in Primarykeys) * { * inter.Append(field.DotNetType + " " + field.Name.ToLower()); * inter.Append(field.Name != Primarykeys.Last().Name ? ", " : ""); * } * inter.Append(");\r\n\r\n"); * #endregion * } * if (param.MethodList.Contains(Model.BuilderMethods.Delete) && HasPrimarykey) * { #region 删除 * inter.Append("\t\t/// <summary>\r\n"); * inter.Append("\t\t/// 删除\r\n"); * inter.Append("\t\t/// </summary>\r\n"); * inter.Append("\t\tint Delete("); * foreach (var field in Primarykeys) * { * inter.Append(field.DotNetType + " " + field.Name.ToLower()); * inter.Append(field.Name != Primarykeys.Last().Name ? ", " : ""); * } * inter.Append(");\r\n\r\n"); * #endregion * } * * if (param.MethodList.Contains(Model.BuilderMethods.Count)) * { * inter.Append("\t\t/// <summary>\r\n"); * inter.Append("\t\t/// 查询记录条数\r\n"); * inter.Append("\t\t/// </summary>\r\n"); * inter.Append("\t\tlong GetCount();\r\n"); * * } * * inter.Append("\t}\r\n"); * inter.Append("}\r\n"); * return inter.ToString();*/ string filePath = System.IO.Directory.GetCurrentDirectory() + "\\Template\\IDAL.txt"; string value = ""; FileStream fs = null; StreamReader sr = null; try { if (!File.Exists(filePath)) { return(string.Empty); } fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs, System.Text.Encoding.UTF8); value = sr.ReadToEnd(); } catch (Exception ex) { Debug.WriteLine(ex); } finally { if (sr != null) { sr.Close(); } if (fs != null) { fs.Close(); } } string result = Engine.Razor.RunCompile(value, Guid.NewGuid().ToString(), null, new { param = param, fields = fields }); return(result.ToString()); }
/// <summary> /// 得到实体层 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetModelClass(Model.CodeCreate param) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); /*StringBuilder model = new StringBuilder(import.GetImport_Model()); * model.Append("namespace " + param.NameSpace + (param.NameSpace.IsNullOrEmpty()?"": ".") + param.CNSC.Model + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "\r\n"); * model.Append("{\r\n"); * model.Append("\t[Serializable]\r\n"); * model.Append("\tpublic class " + param.ClassName + "\r\n"); * model.Append("\t{\r\n"); * foreach (var field in fields) * { * model.Append("\t\t/// <summary>\r\n"); * model.Append("\t\t/// " + (field.Note.IsNullOrEmpty() ? field.Name : field.Note) + "\r\n"); * model.Append("\t\t/// </summary>\r\n"); * model.Append("\t\t[DisplayName(\"" + (field.Note.IsNullOrEmpty() ? field.Name : field.Note) + "\")]\r\n"); * model.Append("\t\tpublic " + field.DotNetType + " " + field.Name + " { get; set; }\r\n\r\n"); * } * model.Append("\t}\r\n"); * model.Append("}\r\n"); * return model.ToString();*/ string filePath = System.IO.Directory.GetCurrentDirectory() + "\\Template\\Entity.txt"; string value = ""; FileStream fs = null; StreamReader sr = null; try { if (!File.Exists(filePath)) { return(string.Empty); } fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs, System.Text.Encoding.UTF8); value = sr.ReadToEnd(); } catch (Exception ex) { Debug.WriteLine(ex); } finally { if (sr != null) { sr.Close(); } if (fs != null) { fs.Close(); } } string result = Engine.Razor.RunCompile(value, Guid.NewGuid().ToString(), null, new { param = param, fields = fields }); return(result.ToString()); }
/// <summary> /// 得到接口层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetInterfaceClass(Model.CodeCreate param) { Model.Servers server = Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); //自增列 var Identitys = fields.Where(p => p.IsIdentity); var NotIdeneitys = fields.Where(p => !p.IsIdentity); bool HasIdentity = Identitys.Count() > 0; //主键 var Primarykeys = fields.Where(p => p.IsPrimaryKey); var NotPrimarykeys = fields.Where(p => !p.IsPrimaryKey); bool HasPrimarykey = Primarykeys.Count() > 0; StringBuilder inter = new StringBuilder(import.GetImport_Interface()); inter.Append("namespace " + param.NameSpace + (param.NameSpace == null ? "" : ".") + param.CNSC.Interface + (param.NameSpace1 == null ? "" : "." + param.NameSpace1) + "\r\n"); inter.Append("{\r\n"); inter.Append("\tpublic interface I" + param.ClassName + "\r\n"); inter.Append("\t{\r\n"); if (param.MethodList.Contains(Model.BuilderMethods.Add)) { inter.Append("\t\t/// <summary>\r\n"); inter.Append("\t\t/// 新增\r\n"); inter.Append("\t\t/// </summary>\r\n"); inter.Append("\t\t" + (HasIdentity ? Identitys.First().DotNetType : "int") + " Add(" + param.NameSpace + (param.NameSpace == null ? "" : ".") + param.CNSC.Model + (param.NameSpace1 == null ? "" : "." + param.NameSpace1) + "." + param.ClassName + " model);\r\n\r\n"); } if (param.MethodList.Contains(Model.BuilderMethods.Update) && HasPrimarykey) { inter.Append("\t\t/// <summary>\r\n"); inter.Append("\t\t/// 更新\r\n"); inter.Append("\t\t/// </summary>\r\n"); inter.Append("\t\tint Update(" + param.NameSpace + (param.NameSpace == null ? "" : ".") + param.CNSC.Model + (param.NameSpace1 == null ? "" : "." + param.NameSpace1) + "." + param.ClassName + " model);\r\n\r\n"); } if (param.MethodList.Contains(Model.BuilderMethods.SelectAll)) { inter.Append("\t\t/// <summary>\r\n"); inter.Append("\t\t/// 查询所有记录\r\n"); inter.Append("\t\t/// </summary>\r\n"); inter.Append("\t\tList<" + param.NameSpace + (param.NameSpace == null ? "" : ".") + param.CNSC.Model + (param.NameSpace1 == null ? "" : "." + param.NameSpace1) + "." + param.ClassName + "> GetAll();\r\n\r\n"); } if (param.MethodList.Contains(Model.BuilderMethods.SelectByKey) && HasPrimarykey) { #region 查询单条记录 inter.Append("\t\t/// <summary>\r\n"); inter.Append("\t\t/// 查询单条记录\r\n"); inter.Append("\t\t/// </summary>\r\n"); inter.Append("\t\tModel" + (param.NameSpace1 == null ? "" : "." + param.NameSpace1) + "." + param.ClassName + " Get("); foreach (var field in Primarykeys) { inter.Append(field.DotNetType + " " + field.Name.ToLower()); inter.Append(field.Name != Primarykeys.Last().Name ? ", " : ""); } inter.Append(");\r\n\r\n"); #endregion } if (param.MethodList.Contains(Model.BuilderMethods.Delete) && HasPrimarykey) { #region 除 inter.Append("\t\t/// <summary>\r\n"); inter.Append("\t\t/// 删除\r\n"); inter.Append("\t\t/// </summary>\r\n"); inter.Append("\t\tint Delete("); foreach (var field in Primarykeys) { inter.Append(field.DotNetType + " " + field.Name.ToLower()); inter.Append(field.Name != Primarykeys.Last().Name ? ", " : ""); } inter.Append(");\r\n\r\n"); #endregion } if (param.MethodList.Contains(Model.BuilderMethods.Count)) { inter.Append("\t\t/// <summary>\r\n"); inter.Append("\t\t/// 查询记录条数\r\n"); inter.Append("\t\t/// </summary>\r\n"); inter.Append("\t\tlong GetCount();\r\n"); } inter.Append("\t}\r\n"); inter.Append("}\r\n"); return(inter.ToString()); }
/// <summary> /// 得到业务层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetBusinessClass(Model.CodeCreate param) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); //自增列 var Identitys = fields.Where(p => p.IsIdentity); var NotIdeneitys = fields.Where(p => !p.IsIdentity); bool HasIdentity = Identitys.Count() > 0; //主键 var Primarykeys = fields.Where(p => p.IsPrimaryKey); var NotPrimarykeys = fields.Where(p => !p.IsPrimaryKey); bool HasPrimarykey = Primarykeys.Count() > 0; /*StringBuilder business = new StringBuilder(import.GetImport_Business()); * * business.Append("namespace " + param.NameSpace + (param.NameSpace.IsNullOrEmpty() ? "" : ".") + param.CNSC.Business + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "\r\n"); * business.Append("{\r\n"); * business.Append("\tpublic class " + param.ClassName + "BLL : BaseBLL<"+param.ClassName+">\r\n"); * business.Append("\t{\r\n"); * business.Append("\t\tprivate " + "I" + param.ClassName + "DAL " + param.ClassName[0].ToString().ToLower() + param.ClassName.Substring(1) + "DAL" + ";\r\n"); * business.Append("\t\tpublic " + param.ClassName + "BLL():: base()\r\n"); * business.Append("\t\t{\r\n"); * business.Append("\t\t\tbase.Init(this.GetType().FullName, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);\r\n"); * business.Append("\t\t\tthis." + param.ClassName[0].ToString().ToLower() + param.ClassName.Substring(1) + "DAL = (I" + param.ClassName + "DAL)base.baseDal;\r\n"); * business.Append("\t\t}\r\n"); * * business.Append("\t}\r\n"); * business.Append("}\r\n");*/ string filePath = System.IO.Directory.GetCurrentDirectory() + "\\Template\\BLL.txt"; string value = ""; FileStream fs = null; StreamReader sr = null; try { if (!File.Exists(filePath)) { return(string.Empty); } fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs, System.Text.Encoding.UTF8); value = sr.ReadToEnd(); } catch (Exception ex) { Debug.WriteLine(ex); } finally { if (sr != null) { sr.Close(); } if (fs != null) { fs.Close(); } } string result = Engine.Razor.RunCompile(value, Guid.NewGuid().ToString(), null, new { param = param }); return(result.ToString()); }
/// <summary> /// 得到数据层代码 /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetDataClass(Model.CodeCreate param) { Model.Servers server = Common.Config.GetServer(param.ServerID); if (server == null) { return(string.Empty); } List <Model.Fields> fields = databaseInstance.GetFields(server.ID, param.DbName, param.TableName); /*StringBuilder data = new StringBuilder(import.GetImport_Data()); * * data.Append("namespace " + param.NameSpace + (param.NameSpace.IsNullOrEmpty() ? "" : ".") + param.CNSC.Data + (param.NameSpace1.IsNullOrEmpty() ? "" : "." + param.NameSpace1) + "\r\n"); * data.Append("{\r\n"); * data.Append("\tpublic class " + param.ClassName + "DAL: BaseDALMySql<" + param.ClassName + "> , I" + param.ClassName + "DAL\r\n"); * data.Append("\t{\r\n"); * data.Append("\t\tpublic static " + param.ClassName + "DAL Instance\r\n"); * data.Append("\t\t{\r\n"); * data.Append("\t\t\tget\r\n"); * data.Append("\t\t\t{\r\n"); * data.Append("\t\t\t\treturn new " + param.ClassName + "DAL();\r\n"); * data.Append("\t\t\t}\r\n"); * data.Append("\t\t}\r\n"); * data.Append("\t\t/// <summary>\r\n"); * data.Append("\t\t/// 构造函数\r\n"); * data.Append("\t\t/// </summary>\r\n"); * data.Append("\t\tpublic " + param.ClassName + "DAL()\r\n"); * data.Append("\t\t: base(\"" + param.ClassName + "\", \"ID\")\r\n"); * data.Append("\t\t{\r\n"); * data.Append("\t\t\tthis.sortField = \"ID\";\r\n"); * data.Append("\t\t\tthis.IsDescending = false;\r\n"); * data.Append("\t\t}\r\n"); * * * //新增记录 * * * data.Append("\t}\r\n"); * data.Append("}"); * return data.ToString();*/ string filePath = System.IO.Directory.GetCurrentDirectory() + "\\Template\\DAL.txt"; string value = ""; FileStream fs = null; StreamReader sr = null; try { if (!File.Exists(filePath)) { return(string.Empty); } fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs, System.Text.Encoding.UTF8); value = sr.ReadToEnd(); } catch (Exception ex) { Debug.WriteLine(ex); } finally { if (sr != null) { sr.Close(); } if (fs != null) { fs.Close(); } } string result = Engine.Razor.RunCompile(value, Guid.NewGuid().ToString(), null, new { param = param }); return(result.ToString()); }