Codesmi自己的
2009-05-10 13:22 宝宝合凤凰 阅读(265) 评论(0) 收藏 举报自己的CodeSmith集合.cst
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Inherits="OutputFileCodeTemplate" ResponseEncoding="UTF-8" Src="" Debug="False" Description="Template description here." %>
<%@ Property Name="DataSource" Type="SchemaExplorer.DatabaseSchema" Category="DataContext" Description="Table that the stored procedures should be based on." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Default="" Optional="True" Category="DataContext" Description="数据表" OnChanged="" Editor="" EditorBase="" Serializer="" %>
<%@ Property Name="NameSpace" Type="System.String" Default="com.xaccp.Entity" Optional="False" Category="Context" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import NameSpace="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import NameSpace="System.Data" %>
<%--%注释%--%>
<%@ Property Name="Author" Type="System.String" Default="Author" Optional="False" Category="" Description="??" %>
<%@ Property Name="Description" Type="System.String" Default="" Optional="False" Category="" Description="?????" %>
<%@ Property Name="布尔选择" Type="System.Boolean" Default="True" Optional="False" Category="" Description="" OnChanged="" Editor="" EditorBase="" Serializer="" %>
<%@ Property Name="下拉选择" Type="CollectionTypeEnum" Category="" Description="Type of collection" %>
<%--%生成文件%--%>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<% if(!System.IO.Directory.Exists(OutputDirectory)) System.IO.Directory.CreateDirectory(OutputDirectory); %>
<% this.OutputFile = OutputDirectory + GetClassName(this.SourceTable)+".cs"; %>
<% System.Diagnostics.Process.Start(OutputDirectory); %>
using System;
namespace <%= NameSpace %>
{
/// <summary>
/// 描述:<%= Description %>
/// 作者:<%= Author %>
/// 时间:<%= DateTime.Now.Year %>
///</summary>
public class <%=GetClassName(this.SourceTable)%>
{
<%foreach(ColumnSchema i in this.SourceTable.Columns){%>
private <%=GetCSDataType(i)%> <%=ToCamel(i.Name)%>
<%}%>
<%foreach(ColumnSchema i in this.SourceTable.Columns){%>
public <%=GetCSDataType(i)%> <%=ToPascal(i.Name)%>
{
get { return <%=ToCamel(i.Name)%> ; }
set { <%=ToCamel(i.Name)%>=value ; }
}
<% } %>
}
}
<script runat="template">
<%--%字段与属性的名称转换%--%>
public string ToCamel(string e)
{
return e.Substring(0,1).ToLower()+e.Substring(1);
}
public string ToPascal(string e)
{
return e.Substring(0,1).ToUpper()+e.Substring(1);
}
<%--%设计数据库中的表名与生成的类名的关系%--%>
public string GetClassName(TableSchema e)
{
string n = e.Name;
if(n.Substring(n.Length-1).ToLower()=="s")
{
return ToPascal(n.Substring(0,n.Length-1));
}
return ToPascal(n);
}
<%--%SQL类型与.net类型的转换%--%>
public string GetCSDataType(ColumnSchema column)
{
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;
}
}
}
<%--选择输出文件夹--%>
private string _outputDirectory = String.Empty;
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[Optional]
[Category("Output")]
[Description("要输出文件到...")]
public string OutputDirectory
{
get
{
// default to the directory that the template is located in
if (_outputDirectory.Length == 0) return this.CodeTemplateInfo.DirectoryName + @"output\";
return _outputDirectory;
}
set
{
if (!value.EndsWith(@"\")) value += @"\";
_outputDirectory = value;
}
}
public enum CollectionTypeEnum
{
Vector,
HashTable,
SortedList
}
</script>
浙公网安备 33010602011771号