業務層介面 public static string DataTableToBaseIService(string tableName, string nameSpace, string className) { var table = SqlTableHelper.GetSQLTableInfo( ...
業務層介面
public static string DataTableToBaseIService(string tableName, string nameSpace, string className) { var table = SqlTableHelper.GetSQLTableInfo(tableName); StringBuilder reval = new StringBuilder(); reval.AppendFormat(@" using System; using System.Collections.Generic; using System.Data; using {0}.Mapping; namespace {0}.IService {{ public partial interface {1} {{ /// <summary> /// 保存 /// </summary> int Save({2} model,ref string msg); /// <summary> /// 獲取數據(分頁) /// </summary> /// <param name=""filterSql"">查詢條件</param> /// <param name=""pageIndex"">頁碼</param> /// <param name=""pageSize"">每頁顯示數量</param> /// <param name=""total"">總行數</param> /// <param name=""sort"">排序條件</param> /// <returns>返回DataTable</returns> DataTable PageData(string filterSql,int pageIndex,int pageSize,out int total,string sort=""""); /// <summary> /// 獲取數據(不分頁) /// </summary> /// <param name=""filterSql"">查詢條件</param> /// <param name=""sort"">排序條件</param> /// <returns>返回DataTable</returns> DataTable PageData(string filterSql,string sort=""""); /// <summary> /// 獲取單條數據 /// </summary> /// <param name=""filterSql"">查詢條件</param> /// <returns>返回DataTable</returns> DataTable GetSingle(string filterSql); /// <summary> /// 刪除 /// </summary> int Delete({3} {4},ref string msg); }} }}", nameSpace, className, tableName, SqlTableToMapping.ChangeType(table.Rows[0]["數據類型"].ToString()), table.Rows[0]["列名"]); return reval.ToString(); } public static string DataTableToUserIService(string nameSpace, string className) { StringBuilder reval = new StringBuilder(); reval.AppendFormat(@" using System; using System.Collections.Generic; using System.Data; using {0}.Mapping; namespace {0}.IService {{ public partial interface {1} {{ }} }}", nameSpace, className); return reval.ToString(); }View Code
實現業務層介面
public static string DataTableToBaseService(DataTable dt, string tableName, string nameSpace) { string key = dt.Rows[0]["列名"].ToString(); StringBuilder sb = new StringBuilder(); sb.AppendFormat(@" using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Data.SqlClient; using {0}.Mapping; using {0}.IRepository; using {0}.IService; namespace {0}.Service {{ public partial class {1}Service:I{1}Service {{", nameSpace, tableName); sb.AppendFormat(@" private readonly I{0}Repository _{0}Repository; public {0}Service(I{0}Repository {0}Repository) {{ this._{0}Repository = {0}Repository; }}", tableName); ///新增 sb.AppendFormat(@" /// <summary> /// 新增/修改 /// </summary> public int Save({0} model,ref string msg) {{ var iret = -1; try {{ iret = _{0}Repository.Save(model); }} catch(Exception e) {{ msg = e.Message; }} return iret; }} /// <summary> /// 獲取數據(分頁) /// </summary> /// <param name=""filterSql"">查詢條件</param> /// <param name=""pageIndex"">頁碼</param> /// <param name=""pageSize"">每頁顯示數量</param> /// <param name=""total"">總行數</param> /// <param name=""sort"">排序條件</param> /// <returns>返回DataTable</returns> public DataTable PageData(string filterSql, int pageIndex, int pageSize, out int total, string sort = """") {{ return _{0}Repository.PageData(filterSql,pageIndex,pageSize,out total,sort); }} /// <summary> /// 獲取數據(分頁) /// </summary> /// <param name=""filterSql"">查詢條件</param> /// <param name=""sort"">排序條件</param> /// <returns>返回DataTable</returns> public DataTable PageData(string filterSql, string sort = """") {{ return _{0}Repository.PageData(filterSql,sort); }} /// <summary> /// 獲取單條數據 /// </summary> /// <param name=""filterSql"">查詢條件</param> /// <returns>返回DataTable</returns> public DataTable GetSingle(string filterSql) {{ return _{0}Repository.GetSingle(filterSql); }} /// <summary> /// 刪除數據 /// </summary> /// <param name=""key"">主鍵</param> /// <returns>返回DataTable</returns> public int Delete({2} {1},ref string msg) {{ var iret = -1; try {{ iret = _{0}Repository.Delete({1}); }} catch(Exception e) {{ msg = e.Message; }} return iret; }} ", tableName, key, SqlTableToMapping.ChangeType(dt.Rows[0]["數據類型"].ToString())); sb.Append(@" } }"); return sb.ToString(); } #endregion #region 生成自定義類 public static string DataTableToUserService(string tableName, string nameSpace) { StringBuilder reval = new StringBuilder(); reval.AppendFormat(@" using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Data.SqlClient; using {0}.Mapping; namespace {0}.Service {{ public partial class {1}Service {{ }} }}", nameSpace, tableName); return reval.ToString(); }View Code
Autoface註入
public static string AutofaceClass(string nameSpace, DataTable dt) { StringBuilder sb = new StringBuilder(); sb.AppendFormat(@" using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autofac; using {0}.Mapping; using {0}.IService; namespace {0}.Service {{ public class ServiceModule : Module {{ protected override void Load(ContainerBuilder builder) {{", nameSpace); for (var i = 0; i < dt.Rows.Count; i++) { sb.AppendFormat(@" builder.RegisterType<{0}>().As<{1}>();", dt.Rows[i]["name"] + "Service", "I" + dt.Rows[i]["name"] + "Service"); } sb.Append(@" base.Load(builder); } } }"); return sb.ToString(); }View Code