图解CodeSmith使用和实用教程一 - 入门和生成MIS项目实体层代码

本文介绍了如何利用CodeSmith工具快速生成MIS项目的实体层代码,包括安装步骤、使用示例及注意事项。通过自定义模板和数据库连接,实现了自动化代码生成,显著减少了开发工作量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    CodeSmith,也就是传说中的那个代码生成工具。

    下载CodeSmith 6.5,可到我网盘下,链接在底部。

一 安装

1 解压,点击安装;



2 协议;



3 选择目录;



4 一路next完成。



5 解压Crack目录下的内容;



6 执行上一步解开的东西的安装,必须不要打开CodeSmith,VS20XX; 安装后看下开始菜单;如下图;已经装好;



二 用CodeSmith生成MIS项目的实体层代码

    实体层代码,就是对数据库表的映射部分的代码,如果表多一些的话,手写是个相当大的工作量;

1  进入CodeSmith,先测试下;在右侧选择一个自带示例模板,内容如下图;



2 点击 Run;生成该CST模板的生成的代码如下图;



3 写生成项目实体层的模板代码

test.cst:

<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>


<%-- 注册实体层Entity模板 --%>
<%@ Register Name="EntityTemplate" Template="D:\WinformTier\Entity.cst" MergeProperties="Flase" ExcludeProperties=""%>


<%-- 数据库 --%>
<%@ Property Name="SourceDatabase" 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."%>


<%
//创建实体层Entity类
this.GenerateEntityClasses();

Debug.WriteLine("OK");
%>

<script runat="template">
    //生成实体Entity类
private void GenerateEntityClasses()
    {
        CodeTemplate Template =new EntityTemplate();
        foreach(TableSchema table in this.SourceDatabase.Tables)
        {
            string FileDirectory = OutputDirectory +"\\"+ table.Name +".cs";
            //生成模板
            Template.SetProperty("Table",table);
            //文件输出
            Template.RenderToFile(FileDirectory,true);
            Debug.WriteLine(FileDirectory +" 创建成功.");
        }
    }
</script>


<script runat="template">
    //解决方案输出路径
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;
        } 
    }
</script>

Entity.cst

<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>

<%@ Assembly Name="SchemaExplorer"%>
<%@ Import Namespace="SchemaExplorer"%>

<%@ Property Name="Table" Type="TableSchema" 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."%>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Entity
{
publicpartialclass<%= Table.Name%>
{
<%foreach(ColumnSchema col in Table.Columns){ %>
public <%= col.DataType %> <%= col.Name %> { get;set; }
<% } %>
}
}

上述代码中的“D:\WinformTier\Entity.cst”,可改为自己保存cst的路径;

另外还要添加数据库连接;添加了之后CodeSmith会自动读出数据库中的表的名字和字段名,然后生成代码;

添加数据源如下图,跟VS中类似,此处选择的是SqlSever架构提供者,如果使用别的数据库略有不同;


在右侧属性面板选中添加的数据源和选择输出目录;



然后点击Run,Build成功;看下输出目录;一堆的实体层代码文件已经生成;真的是好多好强大啊;打开一个看一下,没什么问题,如下图所示。看来资本主义确实有值得我们学习的地方,


codesmith 6.5; 上述模板代码;Sql Server示例数据库AdventureWorks_Data.mdf;可到俺网盘下载;

codesmith下载:

http://pan.baidu.com/s/1o63dEHW


示例数据库:

http://pan.baidu.com/s/1sjynMGX


模板代码:

http://pan.baidu.com/s/1o6n4Byy



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值