小结

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

前言公司项目是基于soa面向服务的架构思想开发的,项目分解众多子项目是必然的。然而子项目的架子结构种类也过多的话,就会对后期的开发维护产生一锅粥的感觉。为了尽可能的在结构层避免出现这种混乱的现象,我们就做了一个决定,使用一个统一的架子结构,让项目管理变的简单起来。

这样一来,结构中各层就会有很多重复的代码或者重复的逻辑出现,为啦提高开发效率,节约开发时间,我们采用了codesmith根据自定义模板,生成代码功能。让单表的增删改查功能从数据访问层到ui展示层一键批量生成。下面就开始我的codeSmith模板编写历程回顾。

CodeSmith安装下载官网地址:http://www.codesmithtools.com

下载地址:http://www.codesmithtools.com/downloads

我使用的,带破解注册工具的codesmith链接:http://pan.baidu.com/s/1dDdndsd。

傻瓜式安装,不做介绍。只不过你安装完需要很多码。那么烦啦,就用我百度云里面的。带注册软件,安装完之后,不要急于打开codesmith,先去用注册软件注册下。

安装完成,破解成功。

[flash=500,400]http://s1.knowsky.com/20160211/b0itex0sleh56.jpg[/flash]

打开codesmith主界面如下。

[flash=500,400]http://s1.knowsky.com/20160211/gvitjl4zx3156.jpg[/flash]

Note:打开新建Csharp template,然后后缀名为cst的就是模板文件,自己写的模板代码,就在这种后缀格式的文件中。然后光标放在模板文件中,F5即可生成你要代码的文件。

写自己的codesmith模板代码。1、自定义参数模板

[flash=500,400]http://s1.knowsky.com/20160211/zrhggoedqcy56.jpg[/flash]

Note:从这里我们能看到参数的声明,与基本语法的使用规则,需带<%%>。熟悉之后,在右下角给参数赋值,然后光标放入模板中,点击f5生成代码,看下,推敲下。

2、遍历数据库中表的模板

[flash=500,400]http://s1.knowsky.com/20160211/0kb4dlb1rc357.jpg[/flash]

Note:图片展示的是怎么设置数据库配置

模板代码如下

<%--引入c#模板--%>

<%@ CodeTemplate Language="C#"TargetLanguage="C#"Debug="False"Description="Create an enum of tables."%>

<%--声明数据库的参数,在左下角的Database属性中,选择要操作的数据库名称--%>

<%@PRoperty Category="Database"Name="SourceDatabase"Type="SchemaExplorer.DatabaseSchema"Optional="False"Description="Database the table enums will come from."%>

<%--引入下面的类库,操作数据库必备的。不要纠结加入就行啦。--%>

<%@ Assembly Name="SchemaExplorer"%>

<%@ Import Namespace="SchemaExplorer"%>

<%--SourceDatabase, 是你选择数据库的属性类,涵盖数据库的名称,创建时间,字符串链接,描述等等,自己可以点点看 --%>publicenum<%=SourceDatabase.Name %>Tables

{<%-- 遍历数据库中的表集合 --%>

<%for(intx =0; x < SourceDatabase.Tables.Count; x++)

{

TableSchema table=SourceDatabase.Tables[x];if(x < SourceDatabase.Tables.Count -1)//输出表名,这里是c#的注释,不会被写进生成的代码中。\t为换行符。Response.WriteLine("\t{0},", table.Name);elseResponse.WriteLine("\t{0}", table.Name);

}%>}

3、遍历数据库表中的字段,声明并使用自定义函数

<%@ CodeTemplate Language="C#"TargetLanguage="C#"Debug="False"Description="Create a list of properties from database table."%>

<%--声明数据库表的参数,在左下角的表属性中,选择要操作的数据库表--%>

<%@ Property Name="SourceTable"Type="SchemaExplorer.TableSchema"Category="Context"Description="Table that the object is based on."%>

<%--引入system类型转为c#的数据类型的映射字典 --%>

<%@ Map Name="CSharpAlias"[flash=500,400]http://www.knowsky.com/[/flash]Src="System-CSharpAlias"Description="System to C# Type Map"%>

<%--引入下面的类库,操作数据库必备的。--%>

<%@ Assembly Name="SchemaExplorer"%>

<%@ Import Namespace="SchemaExplorer"%>

<%--遍历数据库表的字段属性--%>

<%foreach(ColumnSchema columninthis.SourceTable.Columns) { %>

<%--拼接字符串,输出c#中实体的属性--%>public<%= ControlType(CSharpAlias[column.SystemType.FullName]) %> <%= StringUtil.ToPascalCase(column.Name) %>{get;set; }<% } %>

<script runat="template">//如果类型为int,或datetime类型输出可空类型publicstringControlType(objectval)

{varty=val.ToString();if(ty=="int")

{return"int?";

}if(ty=="System.DateTime")

{return"System.DateTime?";

}returnty;

}</script>

4、批量生成文件,并指定生成文件位置

[flash=500,400]http://s1.knowsky.com/20160211/2p4nthigckh57.jpg[/flash]

代码如下

<%@ Template Language="C#"TargetLanguage="Text"%>

<%-- 注册要生成的模板 --%>

<%@ Register Name="TableEnumTemplate"Template="TableEnum.cst"MergeProperties="Flase"ExcludeProperties=""%>

<%@ Register Name="TableClumTemplate"Template="TableProperties.cst"MergeProperties="Flase"ExcludeProperties=""%>

<%--声明数据库的参数,在左下角的Database属性中,选择要操作的数据库名称--%>

<%@ Property Category="Database"Name="SourceDatabase"Type="SchemaExplorer.DatabaseSchema"Optional="False"Description="Database the table enums will come from."%>

<%--Type数据类型为TableSchema,表明参数Table是一个表对象。--%>

<%@ Property Name="SourceTable"Type="TableSchema"DeepLoad="True"Optional="False"Category="Table"Description="Table Name"%>

<%-- 执行输出文件的函数 --%>

<%this.OutPutFile(); %>

<script runat="template">//输出文件privatevoidOutPutFile()

{//生成列举表名的模板CodeTemplate table =newTableEnumTemplate();//指定输出路径stringtableFilePath = OutputDirectory +"\\"+this.SourceDatabase.Name +".cs";//给子模板参数赋值table.SetProperty("SourceDatabase",this.SourceDatabase);

table.RenderToFile(tableFilePath,true);//生成列表表字段的模板CodeTemplate cloumn =newTableClumTemplate();//指定输出路径stringcloumnFilePath = OutputDirectory +"\\"+this.SourceTable.Name +".cs";//给子模板参数赋值cloumn.SetProperty("SourceTable",this.SourceTable);

cloumn.RenderToFile(cloumnFilePath,true);

}//解决方案输出路径privatestringDirectory =String.Empty;

[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor),typeof(System.Drawing.Design.UITypeEditor))]

[Optional, NotChecked]

[DefaultValue("")]publicstringOutputDirectory

{get{returnDirectory;

}set{if(value.EndsWith("\\")) value = value.Substring(0, value.Length -1);

Directory=value;

}

}</script>

好啦,就这么多啦,能满足我的需求啦。

小结如果你在看到本文后有什么疑问,请加入博客左上角群,一起交流学习。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航