Document directory
- CodeSmith Model generation Template
CodeSmith Model generation Template
Template:
<% --
Name: CodeSmith Model
Author: QQ: 418715961 E_MAIL: chhuic@163.com
Description: CodeSmith Model
-- %>
<% @ CodeTemplate Language = "C #" TargetLanguage = "C #"
Description = "Generates a class including a special informational header" %>
<% @ Property Name = "NameSpace" Type = "String"
Category = "Context"
Description = "The namespace to use for this class" %>
<% @ Property Name = "ClassName" Type = "String"
Category = "Context"
Description = "The name of the class to generate" %>
<% @ Property Name = "SourceTable" Type = "SchemaExplorer. TableSchema" Category = "Context" Description = "Table that the stored procedures shocould be based on." %>
<% @ Assembly Name = "SchemaExplorer" %>
<% @ Import Namespace = "SchemaExplorer" %>
<Script runat = "template">
Public string CSharpType (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>
//////////////////////////////////////// //////////////////////////////////////// ///////
// File: <% = SourceTable. Name %> Model. cs
// Description: the <% = SourceTable. Name %> Model Created By CodeSmith.
//---------------------
// Copyright: @ <% = DateTime. Now. Year %>
//---------------------
// History:
//////////////////////////////////////// //////////////////////////////////////// ///////
Using System;
Namespace <% = NameSpace %>. <% = ClassName %>
{
/// <Summary>
/// Summary description for <% = SourceTable. Name %> Model.
/// Description: <% = SourceTable. Description %>
/// </Summary>
Public class <% = SourceTable. Name %> Model
{
# Region Private Protery
<% For (int I = 0; I <SourceTable. Columns. Count; I ++) {%>
Private <% = CSharpType (SourceTable. columns [I]) % >_<%= SourceTable. columns [I]. name. substring (0, 1 ). toLower () + SourceTable. columns [I]. name. substring (1, SourceTable. columns [I]. name. length-1) %>;
<% }%>
# Endregion
# Region Constructor
/// <Summary>
/// <% = SourceTable. Name %> Struct Constructor
/// </Summary>
Public <% = SourceTable. Name %> Model ()
{
}
/// <Summary>
/// <% = SourceTable. Name %> Struct Constructor (Have Parameters)
/// </Summary>
<% For (int I = 0; I <SourceTable. Columns. Count; I ++) {%>
/// <Param name = "<% = SourceTable. columns [I]. name. substring (0, 1 ). toLower () + SourceTable. columns [I]. name. substring (1, SourceTable. columns [I]. name. length-1) %> "> <% if (SourceTable. columns [I]. description. length> 0) {%> <% = SourceTable. columns [I]. name + "" + SourceTable. columns [I]. description %> <%} else {%> <% = SourceTable. columns [I]. name %> <% }%> </param>
<% }%>
Public <% = SourceTable. name %> Model (<% for (int I = 0; I <SourceTable. columns. count; I ++) {%> <% = CSharpType (SourceTable. columns [I]) %> <% = SourceTable. columns [I]. name. substring (0, 1 ). toLower () + SourceTable. columns [I]. name. substring (1, SourceTable. columns [I]. name. length-1) %> <% if (I <SourceTable. columns. count-1) {%>,<%}%><%}%>)
{
<% For (int I = 0; I <SourceTable. Columns. Count; I ++) {%>
This. _ <% = SourceTable. columns [I]. name. substring (0, 1 ). toLower () + SourceTable. columns [I]. name. substring (1, SourceTable. columns [I]. name. length-1) % >=<%= SourceTable. columns [I]. name. substring (0, 1 ). toLower () + SourceTable. columns [I]. name. substring (1, SourceTable. columns [I]. name. length-1) %>;
<% }%>
}
# Endregion
# Region public Property
<% For (int I = 0; I <SourceTable. Columns. Count; I ++) {%>
/// <Summary>
/// <% = SourceTable. Columns [I]. Name %> <% if (SourceTable. Columns [I]. Description. Length> 0) {%>
/// <% = SourceTable. Columns [I]. Description %> <%} %>
/// </Summary>
Public <% = CSharpType (SourceTable. Columns [I]). Trim () %> <% = SourceTable. Columns [I]. Name %>
{
Get {return _ <% = SourceTable. columns [I]. name. substring (0, 1 ). toLower () + SourceTable. columns [I]. name. substring (1, SourceTable. columns [I]. name. length-1) %> ;}
Set {_ <% = SourceTable. columns [I]. name. substring (0, 1 ). toLower () + SourceTable. columns [I]. name. substring (1, SourceTable. columns [I]. name. length-1) %> = value ;}
}
<% }%>
# Endregion
}
}
Output:
//////////////////////////////////////// //////////////////////////////////////// ///////
// File: CompanyModel. cs
// Description: the CompanyModel Created By CodeSmith.
//---------------------
// Copyright: @ 2009-9-3 12:48:02
//---------------------
// History:
//////////////////////////////////////// //////////////////////////////////////// ///////
Using System;
Namespace LineChat. Model
{
/// <Summary>
/// Summary description for CompanyModel.
/// Description:
/// </Summary>
Public class CompanyModel
{
# Region Private Protery
Private int _ companyId;
Private string _ companyAccount;
Private string _ companyName;
Private string _ companyAddress;
Private string _ shortName;
Private string _ phone;
Private string _ logo;
Private string _ email;
Private int _ postCode;
Private string _ uRL;
Private DateTime _ createTime;
Private string _ belong;
Private int _ adminId;
Private DateTime _ changeTime;
Private int _ adminId2;
Private int _ state;
Private string _ remark;
# Endregion
# Region Constructor
/// <Summary>
/// CompanyStruct Constructor
/// </Summary>
Public CompanyModel ()
{
}
/// <Summary>
/// CompanyStruct Constructor (Have Parameters)
/// </Summary>
/// <Param name = "companyId"> CompanyId </param>
/// <Param name = "companyAccount"> unique CompanyAccount key </param>
/// <Param name = "companyName"> CompanyName </param>
/// <Param name = "companyAddress"> CompanyAddress </param>
/// <Param name = "shortName"> ShortName </param>
/// <Param name = "phone"> Phone </param>
/// <Param name = "logo"> Logo </param>
/// <Param name = "email"> Email </param>
/// <Param name = "postCode"> PostCode </param>
/// <Param name = "uRL"> URL </param>
/// <Param name = "createTime"> CreateTime </param>
/// <Param name = "belong"> Belong </param>
/// <Param name = "adminId"> AdminId </param>
/// <Param name = "changeTime"> ChangeTime </param>
/// <Param name = "adminId2"> AdminId2 </param>
/// <Param name = "state"> State 1 is valid, and 0 is invalid </param>
/// <Param name = "remark"> Remark </param>
Public CompanyModel (int companyId, string companyAccount, string companyName, string companyAddress, string shortName, string phone, string logo, string email, int postCode, string uRL, DateTime createTime, string belong, int adminId, DateTime changeTime, int adminId2, int state, string remark)
{
This. _ companyId = companyId;
This. _ companyAccount = companyAccount;
This. _ companyName = companyName;
This. _ companyAddress = companyAddress;
This. _ shortName = shortName;
This. _ phone = phone;
This. _ logo = logo;
This. _ email = email;
This. _ postCode = postCode;
This. _ uRL = uRL;
This. _ createTime = createTime;
This. _ belong = belong;
This. _ adminId = adminId;
This. _ changeTime = changeTime;
This. _ adminId2 = adminId2;
This. _ state = state;
This. _ remark = remark;
}
# Endregion
# Region public Property
/// <Summary>
/// CompanyId
/// </Summary>
Public int CompanyId
{
Get {return _ companyId ;}
Set {_ companyId = value ;}
}
/// <Summary>
/// CompanyAccount
/// Unique key
/// </Summary>
Public string CompanyAccount
{
Get {return _ companyAccount ;}
Set {_ companyAccount = value ;}
}
/// <Summary>
/// CompanyName
/// </Summary>
Public string CompanyName
{
Get {return _ companyName ;}
Set {_ companyName = value ;}
}
/// <Summary>
/// CompanyAddress
/// </Summary>
Public string CompanyAddress
{
Get {return _ companyAddress ;}
Set {_ companyAddress = value ;}
}
/// <Summary>
/// ShortName
/// </Summary>
Public string ShortName
{
Get {return _ shortName ;}
Set {_ shortName = value ;}
}
/// <Summary>
/// Phone
/// </Summary>
Public string Phone
{
Get {return _ phone ;}
Set {_ phone = value ;}
}
/// <Summary>
/// Logo
/// </Summary>
Public string Logo
{
Get {return _ logo ;}
Set {_ logo = value ;}
}
/// <Summary>
/// Email
/// </Summary>
Public string Email
{
Get {return _ email ;}
Set {_ email = value ;}
}
/// <Summary>
/// PostCode
/// </Summary>
Public int PostCode
{
Get {return _ postCode ;}
Set {_ postCode = value ;}
}
/// <Summary>
/// URL
/// </Summary>
Public string URL
{
Get {return _ uRL ;}
Set {_ uRL = value ;}
}
/// <Summary>
/// CreateTime
/// </Summary>
Public DateTime CreateTime
{
Get {return _ createTime ;}
Set {_ createTime = value ;}
}
/// <Summary>
/// Belong
/// </Summary>
Public string Belong
{
Get {return _ belong ;}
Set {_ belong = value ;}
}
/// <Summary>
/// AdminId
/// </Summary>
Public int AdminId
{
Get {return _ adminId ;}
Set {_ adminId = value ;}
}
/// <Summary>
/// ChangeTime
/// </Summary>
Public DateTime ChangeTime
{
Get {return _ changeTime ;}
Set {_ changeTime = value ;}
}
/// <Summary>
/// AdminId2
/// </Summary>
Public int AdminId2
{
Get {return _ adminId2 ;}
Set {_ adminId2 = value ;}
}
/// <Summary>
/// State
/// 1 valid, 0 invalid
/// </Summary>
Public int State
{
Get {return _ state ;}
Set {_ state = value ;}
}
/// <Summary>
/// Remark
/// </Summary>
Public string Remark
{
Get {return _ remark ;}
Set {_ remark = value ;}
}
# Endregion
}
}