codesmith自定义模板之实体层

一说到codesmith相信大家有喜有恨的,喜之功能很牛叉,自定义代码模板,老外的技术就是牛叉,比其他的代码工具功能更强大,但是英文版就是我的语言障碍,相信多数codeMan不喜欢英文版得软件,在官方下载正式版,有使用期限,不想的怎么破解它,郁闷,不管了,能用几天是几天吧

先我们看看截图打开netiers类这是代码工厂必备的类,只有了解它才能生产自己的代码

接下里我要生产实体层代码。思路是:先找到数据源,找到表结构,然后查找列,以及相关属性,然后根据模板循环生产,输入到相关目录下

先定义一个主模板,功能是选择数据源和,选择保存路径,并且在里面注册生产实体层的子模板

先贴主模板代码:

<%@ CodeTemplate  Inherits="CodeTemplate" Language="C#" TargetLanguage="Text"
 Description
="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Design" %>
<%@ Assembly Name="System.DirectoryServices" %>
<%@ Assembly Name="System.Web" %>
<%@ Assembly Name="System.Xml" %>

<%@ Import Namespace="SchemaExplorer" %>
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System.Text" %>
<%@ Import NameSpace="System.Text.RegularExpressions" %>
<%@ Import NameSpace="System.Diagnostics" %>
<%@ Import NameSpace="System.Xml" %>
<%@ Import NameSpace="System.Xml.Xsl" %>
<%@ Import NameSpace="System.Xml.XPath" %> 
<%-- 注册实体层模板 --%>
<%@ Register Name="EntityClassTemplate" Template="BeingNet.Entity.cst" MergeProperties="False" ExcludeProperties="" %>

<%-- 1. Datasource --%>
<%@ Property Name="ChooseSourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated." %>

<% this.SaveEntityClasses();
%>


<script runat="template">

    
private string templateOutputDirectory=@"E:\";
    
    [Editor(
typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] 
    [Optional, NotChecked]
    [Category(
"01. Getting Started - Required")]
    [Description(
"The directory to output the results to.")]
    [DefaultValue(
"")]
    
public string OutputDirectory 
    { 
        
get
        {
             
return templateOutputDirectory;
        }
        
set
        {
            
if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);
            templateOutputDirectory 
= value;
        } 
    }

    
private void SaveEntityClasses()
    {
          CodeTemplate entitytemplate 
= new EntityClassTemplate();
            
foreach(TableSchema table in this.ChooseSourceDatabase.Tables)
            {
                  entitytemplate.SetProperty(
"CurrentTable",table);
                entitytemplate.RenderToFile(
this.templateOutputDirectory + "\\"  + table.Name+".cs",true);
              
                Debug.WriteLine(table.Name);
            }
           
    }

</script>

下面是实体层模版代码:

<%@ CodeTemplate  Inherits="CodeTemplate" Language="C#" TargetLanguage="Text"
 Description
="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Design" %>
<%@ Assembly Name="System.DirectoryServices" %>
<%@ Assembly Name="System.Web" %>
<%@ Assembly Name="System.Xml" %>

<%@ Import Namespace="SchemaExplorer" %>
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System.Text" %>
<%@ Import NameSpace="System.Text.RegularExpressions" %>
<%@ Import NameSpace="System.Diagnostics" %>
<%@ Import NameSpace="System.Xml" %>
<%@ Import NameSpace="System.Xml.Xsl" %>
<%@ Import NameSpace="System.Xml.XPath" %> 

<%-- 1. Datasource --%>
<%@ Property Name="CurrentTable" Type="SchemaExplorer.TableSchema" DeepLoad="True" Optional="False"  %>
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OA.Entity
{
    
public class <% = CurrentTable.Name%>
    {
        
<% foreach(ColumnSchema col in CurrentTable.Columns)
        {
%>
        
public <% = col.DataType %> <%= col.Name %> { getset; }
            
            
<%}%> 
    }
}

                        
        
        
原文地址:https://www.cnblogs.com/Jaylong/p/entity.html