SQL Table 自动生成Net底层-生成业务层Service

2018-06-22 06:17:43来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

业务层接口

        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

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET没有魔法——ASP.NET Identity 的“多重”身份验证代码篇

下一篇:自我总结和学习表单提交的几种方式 (二)