码迷,mamicode.com
首页 > 其他好文 > 详细

动软模板系列一

时间:2015-03-29 12:11:21      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

先看下动软官方的教程:

http://www.maticsoft.com/help/Template.htm#_Toc292191122

六.    模板编写教程

模板的大体分为5块内容:模板指令声明,代码语句块,表达式块,类功能控制块,文本块输出。下面我们分别来介绍一下这5块内容的语法和使用说明。

    1. 模板指令声明块 <#@  #>

和ASP.NET页面的指令一样,它们出现在文件头,通过<#@…#>表示。其中<#@ template …#>指令是必须的,用于定义模板的基本属性。

(1)    模板指令

<#@ template [language="VB"] [hostspecific="true"] [debug="true"] [inherits="templateBaseClass"] [culture="code"] [compilerOptions="options"] #>

例如

<#@ template language="C#v3.5" hostSpecific="true" debug="true" #>

Language  这里可以指定模板使用的语言。

hostSpecific="true" 表示是否使用特定的host,host里面包含了模板使用的各种对象。

注意

所有属性值必须用双引号都括起来。如果值本身包含引号,则必须使用 \ 字符对这些引号进行转义。 指令通常是模板文件或包含的文件中的第一个元素。

 

(2)    参数指令

<#@ parameter type="Full.TypeName" name="ParameterName" #>

 

(3)    输出指令

<#@ output extension=".fileNameExtension"  encoding="encoding"  #>

    • output extension=".cs"   指定生成文件的扩展名。

encoding="encoding"    指定生成文件的编码。

 

(4)    程序集指令

<#@ assembly name="System.Data" #>

用于添加程序集引用,如果要使用第三方程序集,那么最好在项目中添加引用。

注:您应使用绝对路径的名称,或者路径名称中使用标准的宏的名称。

例如: <#@ assembly name="$(SolutionDir)library\MyAssembly.dll" #>

 

(5)    导入指令

<#@ import namespace="System.Data" #>

导入要使用的命名空间,注意:这里的命名空间必须要在前面指定的程序集里面找得到的,比如我指定命名空间"System.Data","System.Data.Common",这些在程序集System.Data中都有的

 

(6)    包含指令

<#@ include file="test.tt" #> 导入模板,类似Html的include用法

include 指令插入其他模板文件的文本。

例如,下面的指令插入 test.txt 的内容。 <#@ include file="c:\test.txt" #>

在处理时,被包含内容就像是包含文本模板的组成部分一样。 不过,即使 include 指令后跟普通文本块和标准控制块,也可以包含编写有类功能块 <#+...#> 的文件

 

    1. 代码语句块: <#   #>

在模板文件中,可以混合使用任意数量的文本块和标准控制块。中间是一段通过相应编程语言编写的程序调用,我们可以通过代码语句快控制文本转化的流程。

注意:不能在控制块中嵌套控制块。

<#@ output extension=".txt" #>

<#

    for(int i = 0; i < 4; i++)

    {

#>

 Hello!

<#

    }

#>

 

 

    1. 表达式块:<#=  #>

表达式控制块计算表达式并将其转换为字符串。 该字符串将插入到输出文件中。

例如: <#= 2 + 3 #>

表达式可以包含作用域中的任何变量。 例如,下面的块输出数字行:

<#@ output extension=".txt" #>

<#

    for(int i = 0; i < 4; i++)

    {

#>

This is hello number <#= i+1 #>: Hello!

<#

    }

#>

 

 

    1. 类功能控制块:<#+  #>

如果文本转化需要一些比较复杂的逻辑,我们需要写在一个单独的辅助方法中,甚至是定义一些单独的类,我们就是将它们定义在类特性块中。类功能控制块定义属性、方法或不应包含在主转换中的所有其他代码。 类功能块常用于编写帮助器函数。 通常,类功能块位于单独的文件中,这样它们可以包含在多个文本模板中。 类功能控制块以 <#+ ... #> 符号分隔。

例如,下面的模板文件声明并使用一个方法:

<#@ output extension=".txt" #>

Squares:

<#

    for(int i = 0; i < 4; i++)

    {

#>

    The square of <#= i #> is <#= Square(i+1) #>.

<#

    }

#>

That is the end of the list.

<#+  // Start of class feature block

private int Square(int i)

{

    return i*i;

}

#>

 

类功能必须编写在文件末尾。不过,即使 include指令后跟标准块和文本,也可以 <#@include #> 包含类功能的文件。例如下面代码则会报错:

List of Squares:

<#

   for(int i = 0; i < 4; i++)

   {  WriteSquareLine(i); }

#>

End of list.

<#+  // Class feature block

private void WriteSquareLine(int i)

{

#>

   The square of <#= i #> is <#= i*i #>.

<#  

}

#>

 

 

    1. 文本块输出

可以使用 Write() 和 WriteLine() 方法在标准代码块内追加文本,而不必使用表达式代码块。 它们可帮助缩进输出和报告错误。

下面两个代码块在功能上是等效的。

包含表达式块的代码块

 <#

int i = 10;

while (i-- > 0)

    { #>

        <#= i #>

    <# }

#>

使用 WriteLine() 的代码块

<#

    int i = 10;

    while (i-- > 0)

    {

        WriteLine((i.ToString()));

    }

#>

 

Write() 和 WriteLine() 方法有两个重载,其中一个重载接受单个字符串参数,另一个重载接受一个复合格式字符串以及将包含在字符串中的对象数组(与 Console.WriteLine() 方法类似)。

下面两种 WriteLine() 用法在功能上是等效的:

 <#

    string msg = "Say: {0}, {1}, {2}";

    string s1 = "hello";

    string s2 = "goodbye";

    string s3 = "farewell";

   

    WriteLine(msg, s1, s2, s3);

    WriteLine("Say: hello, goodbye, farewell");

#>

 

设置文本模板输出缩进的格式。

 CurrentIndent 字符串属性显示文本模板中的当前缩进,该类还具有一个 indentLengths 字段,该字段是已添加的缩进的列表。 

PushIndent() 方法增加缩进,

PopIndent() 方法减少缩进。

ClearIndent() 方法,删除所有缩进。

 

下面的代码块演示这些方法的用法:

 <#

    WriteLine(CurrentIndent + "Hello");

    PushIndent("    ");

    WriteLine(CurrentIndent + "Hello");

    PushIndent("    ");

    WriteLine(CurrentIndent + "Hello");

    ClearIndent();

    WriteLine(CurrentIndent + "Hello");

    PushIndent("    ");

    WriteLine(CurrentIndent + "Hello");

#>

此代码块产生以下输出:

 

 Hello

        Hello

                Hello

Hello

        Hello

 

    1. 显示错误和警告

可以使用错误和警告实用工具方法向 Visual Studio 错误列表添加消息。 例如,下面的代码向错误列表添加一条错误消息。

 <#

  try

  {

    string str = null;

    Write(str.Length.ToString());

  }

  catch (Exception e)

  {

    Error(e.Message);

  }

#> 

 

 

    1. 模板示例讲解

 

 

 

    1. Host对象属性列表

Host提供了一下属性和方法,方便编写模板时调用。

TableHost属性

类型

说明

DbName

string

当前数据库名。

DbType

string

当前数据库类型:SQL2000,SQL2005,SQL2008,Oracle,OleDb,MySQL,SQLite

TableList

List<TableInfo>

当前数据库所有的表集合,返回List<TableInfo>

DbHelperName

string

数据库访问类名,例如:DbHelperSQL

ProjectName

string

项目名称

ProcPrefix

string

存储过程前缀,例如sp_

ModelPrefix

string

Model类名前缀

ModelSuffix

string

Model类名后缀

BLLPrefix

string

BLL类名前缀

BLLSuffix

string

BLL类名后缀

DALPrefix

string

DAL类名前缀

DALSuffix

string

DAL类名后缀

TabNameRule

string

类命名规则: same(保持原样)  lower(全部小写)  upper(全部大写)

工具-选项-代码生成设置中进行设置。

DbParaDbType

string

得到数据库字段DbType的类名。例如:SqlDbType

preParameter

string

当前数据库类型对应的存储过程参数符号,例如:@

Folder

string

所属文件夹

TableName

string

表名

TableDescription

string

表的描述信息

Fieldlist

List<ColumnInfo>

字段集合

Keys

List<ColumnInfo>

主键字段集合

IdentityKey

ColumnInfo

自动增长标识列;如果没有则返回null。

 

    1. Host对象方法列表

 

TableHost方法

说明

string GetModelClass(string TabName)

组合得到Model类名: 前缀+类名+后缀

string GetBLLClass(string TabName)

组合得到BLL类名: 前缀+类名+后缀

string GetDALClass(string TabName)

组合得到DAL类名: 前缀+类名+后缀

 

 

 

    1. TableInfo  表对象属性

 

属性

类型

说明

TabName

string

表名称

TabUser

string

用户对象

TabType

string

表类型

TabDate

string

创建日期

 

    1. ColumnInfo  字段信息对象

 

属性

类型

说明

ColumnOrder

string

序号

ColumnName

string

字段名

TypeName

string

字段类型

Length

string

长度

Precision

string

精度

Scale

string

小数位数

IsIdentity

bool

是否是标识列

IsPrimaryKey

bool

是否是主键

Nullable

bool

是否允许空

DefaultVal

string

默认值

Description

string

备注

 

    1. CodeCommon工具类常用方法

CodeCommon方法

说明

string DbTypeToCS(string dbtype)

得到“数据库字段类型”对应的“c#类型”

bool isValueType(string cstype)

是否C#中的值(可空)类型

string DbTypeLength(string dbtype,string datatype,string Length)

得到数据库字段DbType的类型和长度

ColumnInfo GetIdentityKey(List<ColumnInfo> keys)

得到自动增长标识列字段

string DbParaDbType(string DbType)

得到不同数据库DbType类名,例如SqlDbType

string preParameter(string DbType)

得到不同数据库的存储过程参数符号,例如:@

string GetWhereParameterExpression(List<ColumnInfo> keys, bool IdentityisPrior, string DbType)

得到Where条件语句 - Parameter方式 (例如:用于Exists Delete  GetModel 的where)

例如:where  NewsId=@NewsId

string GetPreParameter(List<ColumnInfo> keys, boolIdentityisPrior, string DbType)

生成sql语句中的参数列表(例如:用于 Exists  Delete GetModel 的where参数赋值)

string GetInParameter(List<ColumnInfo> keys, boolIdentityisPrior)

得到方法输入参数定义的列表 (例如:用于Exists  Delete GetModel 的参数传入)

string GetFieldstrlist(List<ColumnInfo> keys, boolIdentityisPrior)

字段的 select 列表,和方法传递的参数值

string GetWhereExpression(List<ColumnInfo> keys,bool IdentityisPrior)

得到Where条件语句 - SQL方式 (例如:用于Exists Delete  GetModel 的where)

string GetModelWhereExpression(List<ColumnInfo> keys, bool IdentityisPrior)

得到Where条件语句 - SQL方式 (例如:用于Exists Delete  GetModel 的where)

string CutDescText( string descText,intcutLen,string ReplaceText)

字符串描述截取:要截取的字符串,长度,替代字符串。

 

 

 

输出效果模板代码:(复制到模板代码生成器中执行即可看到效果)

<#@ template language="c#" HostSpecific="True" #>

<#@ output extension= ".cs" #>

<#

TableHost host = (TableHost)(Host);

ColumnInfo identityKey=host.IdentityKey;

#>

数据库名:<#= host.DbName #>

数据库类型:<#= host.DbType #>

表名:<#= host.TableName #>

表描述:<#= host.TableDescription #>

数据库访问类名:<#= host.DbHelperName #>

项目名称:<#= host.ProjectName #>

存储过程前缀:<#= host.ProcPrefix #>

类命名规则:<#= host.TabNameRule #>

数据库DbType类名:<#= host.DbParaDbType #>

存储过程参数符号:<#= host.preParameter #>

表集合:

<# foreach (TableInfo tab in host.TableList)

{

WriteLine(tab.TabName);

} #>

字段集合:

<# foreach (ColumnInfo c in host.Fieldlist)

{

WriteLine("public {0} {1} {{ get; set; }}",CodeCommon.DbTypeToCS(c.TypeName),c.ColumnName );

} #>

字段集合增加连接符号:

<# for(int i=0;i< host.Keys.Count;i++)

{   ColumnInfo key = host.Keys[i]; #>

    <# if (key.IsPrimaryKey || !key.IsIdentity)

    {#>

         strSql.Append(" <#= key.ColumnName#> = <#=preParameter#><#=key.ColumnName#> <# if (i< host.Keys.Count-1 ) {#>and <#}#> ");

    <#}#>

<# }#>

主键字段集合集合:

<# foreach (ColumnInfo c in host.Keys)

{

WriteLine("public {0} {1} {{ get; set; }}",CodeCommon.DbTypeToCS(c.TypeName),c.ColumnName );

} #>

Model类名:<#= host.GetModelClass(host.TableName) #>

BLL类名:<#= host.GetBLLClass(host.TableName) #>

DAL类名:<#= host.GetDALClass(host.TableName) #>

<#= CodeCommon.DbParaDbType(host.DbType) #>

<#= CodeCommon.preParameter(host.DbType) #>

<#= CodeCommon.GetWhereParameterExpression(host.Keys,true,host.DbType) #>

<#= CodeCommon.GetPreParameter(host.Keys,true,host.DbType) #>

<#= CodeCommon.GetInParameter(host.Keys,true) #>

<#= CodeCommon.GetFieldstrlist(host.Keys,true) #>

<#= CodeCommon.GetWhereExpression(host.Keys,true) #>

-----------------------------------------------------

 

动软模板系列一

标签:

原文地址:http://www.cnblogs.com/fuqiang88/p/4375395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!