zoukankan      html  css  js  c++  java
  • CodeSmith批量生成实体类

    很久没有使用CodeSmith代码生成工具了,不知道现在还有没有人在用哦,最近准备做个个人博客系统,从使用Powerdesigner设计数据库开始,CodeSmith生成架构,然后慢慢实现博客系统搭建。

    由于以前没有做过CodeSmith笔记,用的又少,几乎不会自定义模板的生成简单的实体类  ==||,本来很多大牛写了N篇技术文章了,看了N遍总觉得是别人的东西,个人觉得还是有必要自己动手多记几笔,以备以后之用……

    废话不多说了,马上进入CodeSmith的温习中吧……

    首先启动CodeSmith,新增一个要用来生成实体的 DataBase

    再在右边打开TemplateExplorer面板,点击My CodeSmith Templates 新建自己的Template模板 (MyBlog.cst):

    在MyBlog模板中,加上如下代码:

    View Code
    <%-- 
    Name:
    Author: 
    Description: 
    --%>
    <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="CodeTemplate" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
    
    <%-- 必须引用的SchemaExplorer命名空间--%>
    <%@ Assembly Name="SchemaExplorer"%>
    <%@ Import Namespace="SchemaExplorer"%>
    
    <%-- DB(数据库) --%>
    <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Default="" Optional="True" Category="Strings" Description="This is a sample string property." %>

     <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="CodeTemplate" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
    Language : 表示使用的编程语言   TargetLanguage:目标语言 (默认的Text也可以)

    <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Default=""  ……

    声明一个链接数据库的属性,Build后,即可从右侧的Properties中选择要连接的数据库。(按提示操作)

    完成链接数据库的操作后,新建一个Entity.cst模板,和MyBlog.cst保存在同一个文件夹中。

    Entity模板比较简单,就简单地生成单个实体类的代码:

    View Code
    <%-- 
    Name:
    Author: 
    Description: 
    --%>
    <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="CodeTemplate" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
    
    <%@ Assembly Name="SchemaExplorer"%>
    <%@ Import Namespace="SchemaExplorer"%>
    
    <%@ Property Name="Table" Type="TableSchema" Default="" Optional="False" Category="Context" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyBlogEntity
    {
        public class <%=Table.Name%>
        {
            <%foreach(ColumnSchema col in Table.Columns){%>
            public <%=col.DataType%> <%=col.Name%> { get; set; }
            <%}%>
        }
    }
    
    <script runat="template">
    // My methods here.
    
    </script>

    然后在MyBlog模板中注册下Entity模板:

    <%--注册Entity实体--%>
    <%@ Register Name="EntityTemplate" Template="Entity.cst" MergeProperties="False" ExcludeProperties="" %>

    注册完后,就可以在MyBlog中选择Build,生成成功后,然后就可以选择输出路径,Run一下,就可以生成所有的表对应的实体类了。

    这只是简单地实体类的生成,如果要生成包含增删改查的业务逻辑,直接在模板方法中新增自己的方法,就可以生成自己定义的任何类型。

    附上这两个文件的代码:

    MyBlog.cst:

    View Code
    <%-- 
    Name:
    Author: 
    Description: 
    --%>
    <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="CodeTemplate" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
    
    <%-- 必须引用的SchemaExplorer命名空间--%>
    <%@ Assembly Name="SchemaExplorer"%>
    <%@ Import Namespace="SchemaExplorer"%>
    
    <%-- DB(数据库) --%>
    <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Default="" Optional="True" Category="Strings" Description="This is a sample string property." %>
    
    <%--注册Entity实体--%>
    <%@ Register Name="EntityTemplate" Template="Entity.cst" MergeProperties="False" ExcludeProperties="" %>
    
    <%
    //创建实体类
    OutPutFile("Model","MyBlog");
    Debug.Write("全部创建完毕!!");
    %>
    
    <script runat="template">
    // My methods here.
    
    public string SampleMethod()
    {
        return "Method output.";
    }
    
    //解决方案输出路径(通用方法)
    private string Directory = String.Empty;
    
    [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
    [Optional, NotChecked]
    [DefaultValue("")]
    public string OutputDirectory
    {
        get
        {
            return Directory;
        }
        set
        {
            if (value.EndsWith("\\")) value = value.Substring(0, value.Length -1);
            Directory = value;
        }
    }
    
    //循环生成并输出数据库中表生成的实体类
    public void OutPutFile(string modelName,string projectName)
    {
        //创建一个Template(模板)
        CodeTemplate codeTm=new EntityTemplate();
        foreach(TableSchema table in SourceDatabase.Tables)//循环数据库中的多个表
        {
            string fileOutPutPath=string.Format("{0}\\{1}\\{2}\\{3}.cs",this.Directory,projectName,modelName,table.Name);
            //生成模板
            codeTm.SetProperty("Table",table);
            //文件输出RenderToFile方法
            codeTm.RenderToFile(fileOutPutPath,true);
            
            Debug.WriteLine("---------------创建"+table.Name+"成功!---------------------------");
        }
    }
    </script>

    Entity.cst:

    View Code
    <%-- 
    Name:
    Author: 
    Description: 
    --%>
    <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="CodeTemplate" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
    
    <%@ Assembly Name="SchemaExplorer"%>
    <%@ Import Namespace="SchemaExplorer"%>
    
    <%@ Property Name="Table" Type="TableSchema" Default="" Optional="False" Category="Context" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyBlogEntity
    {
        public class <%=Table.Name%>
        {
            <%foreach(ColumnSchema col in Table.Columns){%>
            public <%=col.DataType%> <%=col.Name%> { get; set; }
            <%}%>
        }
    }
    
    <script runat="template">
    // My methods here.
    
    </script>

     

    选择的输出路径为OutputDirectory  F:

    模板中拼的地址是  F:\MyBlog\Model,所以生成的所有实体类,直接可以从这个地址去找到。

  • 相关阅读:
    poj3669 广搜
    检索所有课程都选修的的学生的学号与姓名
    UVA10160 Servicing Stations
    uva11205 The broken pedometer 子集生成
    poj1101 the game 广搜
    poj3009 Curling 2.0 深搜
    poj 1564 Sum It Up 搜索
    HDU 2268 How To Use The Car (数学题)
    codeforces 467C George and Job(简单dp,看了题解抄一遍)
    HDU 2267 How Many People Can Survive(广搜,简单)
  • 原文地址:https://www.cnblogs.com/yangyp/p/2990739.html
Copyright © 2011-2022 走看看