.net中型系统 第二章 业务逻辑类模板设计之一

.net中型系统 第二章 业务逻辑类模板设计之一

前言

在实际项目的开发中,我们通常会抽象出一些通用的类来作为逻辑处理,以便开发复用。
有时候是手动编写的,但是这些对于小项目而言还力所能及,对于大项目或者是业务逐渐丰富的项目而言,会累死,也会很容易漏掉,在统一性也可能会不一致,这就需要我们编写一些可自动生成的设计脚本,来应对这些情况。
以下就让我来介绍一下在实际项目中所使用的自动生成脚本,你们学习之后可以根据自己的理解以及遇到的项目的实际情况,自行调整目录及模板。
下面让我们开始吧!

准备

1、开发工具:CodeSmith.exe
2、语法:CodeSmith相关的模板语法

相关链接:软件及语法文档

结构

项目结构:
业务逻辑模板生成目录结构

讲解

1、TipMessage.cst文件:主要生成对应的数据库表字段的提示类

namespace <%=namespaceName%>.TipMessage
{
<%string TableCode="-"+GetTableIndex(); %>
    /// <summary>
    /// <%= GetTableTitle() %>提示(<%=TableCode%>0000~<%=TableCode%>9999)
    /// </summary>
    public class <%= GetClassName(SourceTable) %>Tip : TipMessageBase
    {
        #region 自定义00
     
        #endregion 自定义00
    
    <% if(SourceTable.HasPrimaryKey){%>
        <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
        <%AddIndex(); %>
        #region //<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>:<%=Index.ToString().PadLeft(2, '0')%>
        /// <summary>
        /// 【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】已存在
        /// </summary>
  		public static TipContent <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>Exists=new TipContent("【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】已存在", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>00);
        
        <%if(GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[i]).ToLower()=="string") %>
        <%{ %>
        /// <summary>
        /// 【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】不能空
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>Empty=new TipContent("【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】不能空", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>01);
        /// <summary>
        /// 【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】长度不得大于<%=SourceTable.PrimaryKey.MemberColumns[i].Size%>
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>Length=new TipContent("【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】长度不得大于<%=SourceTable.PrimaryKey.MemberColumns[i].Size%>", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>04);
        <%} %>
        <%else %>
        <%{ %>
        /// <summary>
        /// 请选择【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>Empty=new TipContent("请选择【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>01);
        <%} %>
        /// <summary>
        /// 【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】格式不正确
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>Format=new TipContent("【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】格式不正确", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>02);
        /// <summary>
        /// 【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】含有非法字符
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>Illegal=new TipContent("【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】含有非法字符", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>03);
        /// <summary>
        /// 【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】不存在
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]) %>UnExists=new TipContent("【<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>】不存在", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>99);
        #endregion //<%=SourceTable.PrimaryKey.MemberColumns[i].Description.Replace("\r\n","")%>:<%=Index.ToString().PadLeft(2, '0')%>
        <% } %>
        <% } %>
        
        <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
        <%if(GetPropertyName(SourceTable.NonPrimaryKeyColumns[i])!="ID") %>
        <%{ %>
        <%AddIndex(); %>
        #region //<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>:<%=Index.ToString().PadLeft(2, '0')%>
        /// <summary>
        /// 【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】已存在
        /// </summary>
  		public static TipContent <%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>Exists=new TipContent("【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】已存在", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>00);
        <%if(GetCSharpVariableType(SourceTable.NonPrimaryKeyColumns[i]).ToLower()=="string") %>
        <%{ %>
        /// <summary>
        /// 【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】不能空
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>Empty=new TipContent("【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】不能空", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>01);
        /// <summary>
        /// 【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】长度不得大于<%=SourceTable.NonPrimaryKeyColumns[i].Size%>
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>Length=new TipContent("【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】长度不得大于<%=SourceTable.NonPrimaryKeyColumns[i].Size%>", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>04);
        <%} %>
        <%else %>
        <%{ %>
        /// <summary>
        /// 请选择【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>Empty=new TipContent("请选择【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>01);
        <%} %>
        /// <summary>
        /// 【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】格式不正确
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>Format=new TipContent("【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】格式不正确", <%=TableCode%><%=i.ToString().PadLeft(2, '0')%>02);
        /// <summary>
        /// 【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】含有非法字符
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>Illegal=new TipContent("【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】含有非法字符", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>03);
        /// <summary>
        /// 【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】不存在
        /// </summary>
        public static TipContent <%= GetPropertyName(SourceTable.NonPrimaryKeyColumns[i]) %>UnExists=new TipContent("【<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>】不存在", <%=TableCode%><%=Index.ToString().PadLeft(2, '0')%>99);
        #endregion //<%=SourceTable.NonPrimaryKeyColumns[i].Description.Replace("\r\n","")%>:<%=Index.ToString().PadLeft(2, '0')%>
        
         <%} %>
		<% } %>
	}
}

2、Switch.cst文件:主要是在数据库映射类与业务逻辑类之间的切换

namespace <%=namespaceName%>.Switch
{
    /// <summary>
    /// <%=GetTableTitle() %>转换
    /// </summary>
    public class <%=GetClassName(SourceTable) %>DtoSwitch
    {
        public static List<DTO.<%=GetClassName(SourceTable) %>Info> SwitchForList(IQueryable<Data.Model.<%=GetClassName(SourceTable) %>> modelList)
        {
            List<DTO.<%=GetClassName(SourceTable) %>Info> dtoList = new List<DTO.<%=GetClassName(SourceTable) %>Info>();
            dtoList = modelList.ToList().Select(model => Switch(model)).ToList();
            return dtoList;
        }
         public static List<Data.Model.<%=GetClassName(SourceTable) %>> SwitchForList(List<DTO.<%=GetClassName(SourceTable) %>Info> modelList)
        {
            List<Data.Model.<%=GetClassName(SourceTable) %>> dtoList = new List<Data.Model.<%=GetClassName(SourceTable) %>>();
            dtoList = modelList.Select(model => Switch(model)).ToList();
            return dtoList;
        }
        /// <summary>
        /// <%=GetTableTitle() %>转换:Model转DTO
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public static DTO.<%=GetClassName(SourceTable) %>Info Switch(Data.Model.<%=GetClassName(SourceTable) %> model)
        {
            return new DTO.<%=GetClassName(SourceTable) %>Info() {
<%=GetSwitch(SourceTable,"model") %>
            };
        }
        /// <summary>
        /// <%=GetTableTitle() %>转换:DTO转Model
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public static Data.Model.<%=GetClassName(SourceTable) %> Switch(DTO.<%=GetClassName(SourceTable) %>Info dto)
        {
            return new Data.Model.<%=GetClassName(SourceTable) %>() {
<%=GetSwitch(SourceTable,"dto") %>
            };
        }
    }
}

3、DTO.cst:主要是生成对应数据库映射类的实体类,后面以便根据业务可以自由增加

namespace <%=namespaceName%>.DTO
{
    public class <%=GetClassName(SourceTable) %>Info : Base.<%=GetClassName(SourceTable) %>Dto
    {
    
    }
}

未完待续,请接着看下一篇…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

本心win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值