CodeSmith 创建Ado.Net自定义模版(二)

CodeSmith 创建Ado.Net自定义模版(二)

接第一篇:  CodeSmith 创建Ado.Net自定义模版(一)

建立第二个C# Template: Step2_Model.cst(实体类模版)

代码:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Description="实体类" %>
<%@ Property Name="NameSpace" Type="System.String" Default="Model" Category="Property" Description="命名空间" %>
<%@ Property Name="Author" Type="System.String" Default="Wilson" Category="Property" Description="作者名" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="True" Category="db" Description="表映射文件" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>

//---------------------上面这些就不做解释了,详情看CodeSmith 创建Ado.Net自定义模版(一)--------------------------

<script runat="template"> //自定义方法可以写在这个标签内
///<summary>
///把数据库类型转化为C#类型
///</summary>
public string DataType2CSharpType(System.Data.DbType dbType) //这里如果有遗漏,大家可以自行补上
    {
        switch (dbType)
        {
            case DbType.AnsiString:
                return "string";
            case DbType.AnsiStringFixedLength:
                return "string";
            case DbType.Binary:
                return "byte[]";
            case DbType.Boolean:
                return "bool";
            case DbType.Byte:
                return "byte";
            case DbType.Currency:
                return "decimal";
            case DbType.Date:
                return "DateTime";
            case DbType.DateTime:
                return "DateTime";
            case DbType.DateTime2:
                return "DateTime";
            case DbType.DateTimeOffset:
                return "DateTime";
            case DbType.Decimal:
                return "decimal";
            case DbType.Double:
                return "double";
            case DbType.Guid:
                return "Guid";
            case DbType.Int16:
                return "short";
            case DbType.Int32:
                return "int";
            case DbType.Int64:
                return "long";
            case DbType.Object:
                return "object";
            case DbType.SByte:
                return "sbyte";
            case DbType.Single:
                return "float";
            case DbType.String:
                return "string";
            case DbType.StringFixedLength:
                return "string";
            case DbType.Time:
                return "DateTime";               
            case DbType.UInt16:
                return "ushort";
            case DbType.UInt32:
                return "uint";
            case DbType.UInt64:
                return "ulong";
            case DbType.VarNumeric:
                return "decimal";
            case DbType.Xml:
                return "string";
            default:
                return "object";
        }
    }
</script>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace <%=NameSpace%>
{
///<summary>
/// 功能: 实体类 (<%=SourceTable.Description%>) //这里是你数据库中Description
/// 创建人:<%=Author%> //这里是上一篇说过的自定义属性
/// 创建日期:<%=DateTime.Now.ToShortDateString() %> //这里和上面的<%=%>的用法就是C#的输出变量
///</summary>
[Serializable]
public class <%=SourceTable.Name%>
{
public <%=SourceTable.Name%>()
{

}

#region##<%=SourceTable.Name%>实体

<%for(int i = 0;i < SourceTable.Columns.Count;i++)%>
<%{%>

///<summary>
///<%=SourceTable.Columns[i].Description%>
///</summary>
public <%=DataType2CSharpType(SourceTable.Columns[i].DataType)%> <%=SourceTable.Columns[i].Name%> {get; set;}

<%}%>

#endregion
}
}

DataType2CSharpType方法是把数据库的类型转化为C#类型。。。

很简单,几个标签己经在CodeSmith 创建Ado.Net自定义模版(一)中做过介绍

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="True" Category="db" Description="表映射文件" %>

有这句话,在属性栏中可以看到db大目录下,SourceTable属性,属性框中有个按钮,点击,会弹出选择数据库的窗口

或者先在工具的Scheme Explorer工具栏中,点击ManagerDataSource,添加一个数据库进来。。

这样你就可以选择数据库表

<%=SourceTable.Name%>  :这是你选择的表名

<%=SourceTable.Description%> :这是你数据库中对表的描述

SourceTable.Columns.Count :这是数据库中的列总数

SourceTable.Columns[i].DataType :是索引为i的列类型

SourceTable.Columns[i].Name  : 是索引用i的列名

 

有必要介始一下下面的一句

<%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Description="实体类" %>

Language:这是表示你编写输出和函数使用的语言 (可以使用C#,VB等语言

TargetLanguage:这是你生成代码的语言

ResponseEncoding:这是编码输出的格式

还可以设置:Debug,Src等属性

PS:CodeSmith Studio中有智能提示,其它属性,大家可以自己试试

后面还有两篇(数据访问层、业务逻辑层),有兴趣的可以看看。

没有太多内容了,只有实现了,前两篇如果看完,

编写一个CodeSmith模版应该是没有问题了,

 

相关篇张:

CodeSmith 创建Ado.Net自定义模版(一)

CodeSmith 创建Ado.Net自定义模版(三)

CodeSmith 创建Ado.Net自定义模版(四)     PS:第四篇有CodeSmith直接生成文件夹及文件的提示,如果需要自行扩展

 

源码下载:http://download.csdn.net/source/3535328

源码下载二:https://files.cnblogs.com/zhongweiv/AdoTemp.rar





作   者:   Porschev[钟慰]
出   处:   http://www.cnblogs.com/zhongweiv/
微   博:     http://weibo.com/porschev
欢迎任何形式的转载,但请务必注明原文详细链接

原文地址:https://www.cnblogs.com/zhongweiv/p/CodeSimth_2.html