SQL Table 自动生成Net底层-生成业务层Service
2018-06-22 06:17:43来源:未知 阅读 ()
业务层接口
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(); }
实现业务层接口
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(); }
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(); }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- SqlDataReader指定转换无效的解决方法 2020-03-23
- DataSet与DataTable的区别示例介绍 2020-03-04
- 让Sqlite脱离VC++ Runtime独立运行的方法 2020-03-03
- asp.net实现导出DataTable数据到Word或者Excel的方法 2020-02-28
- 深入浅析.NET应用程序SQL注入 2020-02-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash