设为首页
优惠IDC
收藏本站
六狼博客
六狼论坛
开启辅助访问
切换到窄版
用户名
Email
自动登录
找回密码
密码
登录
立即注册
只需一步,快速开始
只需一步,快速开始
快捷导航
门户
首页
BBS
云计算
大数据
手机
移动开发android,ios,windows phone,windows mobile
编程
编程技术java,php,python,delphi,ruby,c,c++
前端
WEB前端htmlcss,javascript,jquery,html5
数据库
数据库开发Access,mysql,oracle,sql server,MongoDB
系统
操作系统windows,linux,unix,os,RedHat,tomcat
架构
项目管理
软件设计,架构设计,面向对象,设计模式,项目管理
企业
服务
运维实战
神马
搜索
搜索
热搜:
php
java
python
ruby
hadoop
sphinx
solr
ios
android
windows
centos
本版
帖子
用户
六狼论坛
»
首页
›
编程技术
›
C++
›
CodeSmith实战(一):生成实体层
返回列表
查看:
57
|
回复:
0
CodeSmith实战(一):生成实体层
[复制链接]
wangdeshui
wangdeshui
当前离线
积分
432
窥视卡
雷达卡
升级
77.33%
当前用户组为
举人
当前积分为
432
, 升到下一级还需要 68 点。
136
主题
136
主题
136
主题
举人
举人, 积分 432, 距离下一级还需 68 积分
举人, 积分 432, 距离下一级还需 68 积分
积分
432
发消息
楼主
|
发表于 2013-1-27 04:41:04
|
显示全部楼层
|
阅读模式
1.先建立一个生成单个表的实体的模板
<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="OutputFileCodeTemplate" Debug="False" Description="Template description here." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Optional="True" Description="the table name" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
namespace Jack.CodeSmith.Test
{
public class <%=this.SourceTable.Name%>Model
{
public <%=this.SourceTable.Name%>()
{
//ToDo:
}
<%for(int i=0; i<this.SourceTable.NonPrimaryKeyColumns.Count;i++){%>
private <%=GetCSharpVariableType(this.SourceTable.NonPrimaryKeyColumns
)%> m<%=this.SourceTable.NonPrimaryKeyColumns
.Name%>;
<%}%>
<%for(int i=0; i<this.SourceTable.NonPrimaryKeyColumns.Count;i++)
{
int namelength=this.SourceTable.NonPrimaryKeyColumns
.Name.Length;
string colName=this.SourceTable.NonPrimaryKeyColumns
.Name;%>
public <%=GetCSharpVariableType(this.SourceTable.NonPrimaryKeyColumns
)%> <%=colName.Substring(0,1).ToUpper()%><%=colName.Substring(1,colName.Length-1)%>
get {return m<%=this.SourceTable.NonPrimaryKeyColumns
.Name%>;}
set {m<%=this.SourceTable.NonPrimaryKeyColumns
.Name%>=value;}
<%}%>
}
}
<script runat="template">
// Override the OutputFile property and assign our specific settings to it.
[FileDialog(FileDialogType.Save, Title="Select Output File", Filter="C# Files (*.cs)|*.cs", DefaultExtension=".cs")]
public override string OutputFile
{
get {return base.OutputFile;}
set {base.OutputFile = value;}
}
</script>
<script runat="template">
public string GetCSharpVariableType(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
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.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 "TimeSpan";
case DbType.UInt16: return "ushort";
case DbType.UInt32: return "uint";
case DbType.UInt64: return "ulong";
case DbType.VarNumeric: return "decimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
</script>
下面是代码格式化后的截图
2. 再建立一个模板,调用刚才建立的子模板
<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Inherits="" Description="This is my first data temple" %>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="" Description="Database containing the tables." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace=" System.Design" %>
<%@ Register Name="SubTemplate" Template="C:\CodeSmithStudy\GenerateModel.cst" MergeProperties="True" ExcludeProperties="" %>
<%
for(int i=0; i<this.SourceDatabase.Tables.Count;i++)
{
SubTemplate mySub=new SubTemplate();
mySub.OutputFile=this.OutputDirectory+@"\"+this.SourceDatabase.Tables
.Name+"Model.cs";
mySub.SourceTable=this.SourceDatabase.Tables
;
mySub.Render(Response);
}
%>
<script runat="template">
private string _outputDirectory = @"c:\codesmithstudy";
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
Category("Custom"), Description("Output directory.")]
public string OutputDirectory
{
get {return _outputDirectory;}
set {_outputDirectory= value;}
}
</script>
格式化后的截图
3. 从属性窗口选择要保存到的文件夹和数据库
4.运行,得到如下结果
5. 模板下载
http://www.cnblogs.com/Files/cnblogsfans/template.rar
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
显身卡
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖后跳转到最后一页
Copyright © 2008-2020
六狼论坛
(https://it.6wolf.com) 版权所有 All Rights Reserved.
Powered by
Discuz!
X3.4
京ICP备14020293号-2
本网站内容均收集于互联网,如有问题请联系
QQ:389897944
予以删除
快速回复
返回顶部
返回列表