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

业务层接口

        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

 

posted @ 2017-12-16 16:03  plming  阅读(284)  评论(0编辑  收藏  举报