<%@ CodeTemplate Language="C#" TargetLanguage="Text" ResponseEncoding="UTF-8" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Default="" Optional="True" Category="Tables" Description="This is a Table." %>
<%@ Property Name="ModelClassNameSpace" Type="System.String" Default="modelClassNameSpace" Optional="False" Category="Strings" Description="This is ModelClassNameSpace"%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
///author:penty
//創(chuàng)建時(shí)間<%= DateTime.Now.ToLongDateString()%>
///
public class <%= GetClassName(SourceTable)%>
{
<%
foreach(ColumnSchema column in SourceTable.Columns)
{
%>
<%= GetMemberVariableDeclarationStatement(column)%>
<%
}
%>
public <%= GetClassName(SourceTable)%>()
{
}
public <%= GetClassName(SourceTable)%>(<%= GetConStructorParameters(SourceTable)%>)
{
<%
foreach(ColumnSchema column in SourceTable.Columns)
{
%>
<%= GetMemberVariableName(column)%> = <%= GetMemberVariableName(column).Substring(1) %> ;
<%
}
%>
}
<%
foreach(ColumnSchema column in SourceTable.Columns)
{
%>
public <%= GetCSharpVariableType(column)%> <%= GetPascalMemberVariableName(column)%>
{
get
{
return <%= GetMemberVariableName(column)%>;
}
set
{
<%= GetMemberVariableName(column)%>=value;
}
}
<%
}
%>
}
<script runat="template">
#region 方法GetConStructorParameters 獲取當(dāng)前表所有列累加的字符串
public string GetConStructorParameters(TableSchema table)
{
string returnParameters="";
foreach(ColumnSchema column in table.Columns)
{
returnParameters+= GetCSharpVariableType(column)+" "+GetMemberVariableName(column).Substring(1)+",";
}
return returnParameters.Substring(0,returnParameters.Length-1);
}
#endregion
#region 方法GetMemberVariableDeclarationStatement 根據(jù)當(dāng)前列獲取成員變量公告聲明
public string GetMemberVariableDeclarationStatement(ColumnSchema column)
{
return GetMemberVariableDeclarationStatement("private", column);
}
#endregion
#region 方法GetMemberVariableDeclarationStatement 根據(jù)屬性訪問(wèn)修飾符與列獲取它的聲明規(guī)則
public string GetMemberVariableDeclarationStatement(string protectionLevel, ColumnSchema column)
{
string statement = protectionLevel + " ";
statement += GetCSharpVariableType(column) + " " + GetMemberVariableName(column);
string defaultValue = GetMemberVariableDefaultValue(column);
if (defaultValue != "")
{
statement += " = " + defaultValue;
}
statement += ";";
return statement;
}
#endregion
#region 方法GetReaderAssignmentStatement 根據(jù)當(dāng)前列與索引設(shè)置它的讀取方式
public string GetReaderAssignmentStatement(ColumnSchema column, int index)
{
string statement = "if (!reader.IsDBNull(" + index.ToString() + ")) ";
statement += GetMemberVariableName(column) + " = ";
if (column.Name.EndsWith("TypeCode")) statement += "(" + column.Name + ")";
statement += "reader." + GetReaderMethod(column) + "(" + index.ToString() + ");";
return statement;
}
#endregion
#region 方法GetCamelCaseName 根據(jù)字符串獲取它的Camel法字符串
public string GetCamelCaseName(string value)
{
return value.Substring(0, 1).ToLower() + value.Substring(1);
}
#endregion
#region 方法GetMemberVariableName 根據(jù)當(dāng)前列獲取前綴為_(kāi)的Camel列名
public string GetMemberVariableName(ColumnSchema column)
{
string propertyName = GetPropertyName(column);
string memberVariableName = GetCamelCaseName(propertyName);
return memberVariableName;
}
#endregion
#region 方法GetPascalMemberVariableName 根據(jù)當(dāng)前列它的Pascal列名
public string GetPascalMemberVariableName(ColumnSchema column)
{
string propertyName = GetPropertyName(column);
string temp= propertyName.Substring(0,1).ToUpper()+propertyName.Substring(1);
return temp;
}
#endregion
#region 方法GetPropertyName 根據(jù)當(dāng)前列獲取它的列名
public string GetPropertyName(ColumnSchema column)
{
string propertyName = column.Name;
if (propertyName == column.Table.Name + "Name") return "Name";
if (propertyName == column.Table.Name + "Description") return "Description";
if (propertyName.EndsWith("TypeCode")) propertyName = propertyName.Substring(0, propertyName.Length - 4);
return propertyName;
}
#endregion
#region 方法GetMemberVariableDefaultValue 在生成model類時(shí)根據(jù)當(dāng)前列設(shè)置它的初始值
public string GetMemberVariableDefaultValue(ColumnSchema column)
{
switch (column.DataType)
{
case DbType.Guid:
{
return "Guid.Empty";
}
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength:
{
return "String.Empty";
}
default:
{
return "";
}
}
}
#endregion
#region 方法GetCSharpVariableType 根據(jù)單列信息獲取它的Camel型數(shù)據(jù)類型
public string GetCSharpVariableType(ColumnSchema column)
{
//EndsWith("str") 判斷是否以str結(jié)束
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:
{
//??????如此寫(xiě)法有何用意
return "__UNKNOWN__" + column.NativeType;
}
}
}
#endregion
#region 方法GetReaderMethod 根據(jù)單列獲取它的讀取該列值的方法
public string GetReaderMethod(ColumnSchema column)
{
switch (column.DataType)
{
case DbType.Byte:
{
return "GetByte";
}
case DbType.Int16:
{
return "GetInt16";
}
case DbType.Int32:
{
return "GetInt32";
}
case DbType.Int64:
{
return "GetInt64";
}
case DbType.AnsiStringFixedLength:
case DbType.AnsiString:
case DbType.String:
case DbType.StringFixedLength:
{
return "GetString";
}
case DbType.Boolean:
{
return "GetBoolean";
}
case DbType.Guid:
{
return "GetGuid";
}
case DbType.Currency:
case DbType.Decimal:
{
return "GetDecimal";
}
case DbType.DateTime:
case DbType.Date:
{
return "GetDateTime";
}
case DbType.Binary:
{
return "GetBytes";
}
default:
{
return "__SQL__" + column.DataType;
}
}
}
#endregion
#region 方法GetClassName 根據(jù)當(dāng)前表名獲取處理后的表名
public string GetClassName(TableSchema table)
{
if (table.Name.EndsWith("s"))
{
return table.Name.Substring(3, table.Name.Length - 1);
}
else
{
return table.Name.Substring(0, table.Name.Length);
}
}
#endregion
#region 方法GetSqlDbType理解比較含糊
public string GetSqlDbType(ColumnSchema column)
{
switch (column.NativeType)
{
case "bigint": return "BigInt";
case "binary": return "Binary";
case "bit": return "Bit";
case "char": return "Char";
case "datetime": return "DateTime";
case "decimal": return "Decimal";
case "float": return "Float";
case "image": return "Image";
case "int": return "Int";
case "money": return "Money";
case "nchar": return "NChar";
case "ntext": return "NText";
case "numeric": return "Decimal";
case "nvarchar": return "NVarChar";
case "real": return "Real";
case "smalldatetime": return "SmallDateTime";
case "smallint": return "SmallInt";
case "smallmoney": return "SmallMoney";
case "sql_variant": return "Variant";
case "sysname": return "NChar";
case "text": return "Text";
case "timestamp": return "Timestamp";
case "tinyint": return "TinyInt";
case "uniqueidentifier": return "UniqueIdentifier";
case "varbinary": return "VarBinary";
case "varchar": return "VarChar";
default: return "__UNKNOWN__" + column.NativeType;
}
}
#endregion
#region 方法GetPrimaryKeyType 根據(jù)表名獲取第一個(gè)主鍵列名其寫(xiě)法為Camel法
public string GetPrimaryKeyType(TableSchema table)
{
if (table.PrimaryKey != null)
{
if (table.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
}
else
{
throw new ApplicationException("This template will not work on primary keys with more than one member column.");
}
}
else
{
throw new ApplicationException("This template will only work on tables with a primary key.");
}
}
#endregion
#region 方法GetFileName 根據(jù)表名獲取它的文件名
public override string GetFileName()
{
return this.GetClassName(this.SourceTable) + ".cs";
}
#endregion
</script>
文章標(biāo)題:CodeSmith實(shí)現(xiàn)model代碼生成-創(chuàng)新互聯(lián)
地址分享:http://jinyejixie.com/article6/dhoiig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)公司、服務(wù)器托管、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容