CodeSmith实现model代码生成-创新互联

下面是实现model的代码生成,类名就是表名,至于属性的类型可以在GetCSharpVariableType方法中修改为你想要的类型CodeSmith实现model
代码生成

<%@ 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

在右江等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都网站建设 网站设计制作按需网站设计,公司网站建设,企业网站建设,品牌网站建设,成都全网营销推广,外贸网站制作,右江网站建设费用合理。

//创建时间<%= 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 获取当前表所有列累加的字符串
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 根据当前列获取成员变量公告声明
public string GetMemberVariableDeclarationStatement(ColumnSchema column)
{
return GetMemberVariableDeclarationStatement("private", column);
}
#endregion
#region 方法GetMemberVariableDeclarationStatement 根据属性访问修饰符与列获取它的声明规则
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 根据当前列与索引设置它的读取方式
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 根据字符串获取它的Camel法字符串
public string GetCamelCaseName(string value)
{
return value.Substring(0, 1).ToLower() + value.Substring(1);
}
#endregion
#region 方法GetMemberVariableName 根据当前列获取前缀为_的Camel列名
public string GetMemberVariableName(ColumnSchema column)
{
string propertyName = GetPropertyName(column);
string memberVariableName = GetCamelCaseName(propertyName);

return memberVariableName;
}
#endregion
#region 方法GetPascalMemberVariableName 根据当前列它的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 根据当前列获取它的列名
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类时根据当前列设置它的初始值
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 根据单列信息获取它的Camel型数据类型
public string GetCSharpVariableType(ColumnSchema column)
{
//EndsWith("str") 判断是否以str结束
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;
}
}
}
#endregion
#region 方法GetReaderMethod 根据单列获取它的读取该列值的方法
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 根据当前表名获取处理后的表名
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 根据表名获取第一个主键列名其写法为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 根据表名获取它的文件名
public override string GetFileName()
{
return this.GetClassName(this.SourceTable) + ".cs";
}
#endregion
</script>

文章标题:CodeSmith实现model代码生成-创新互联
地址分享:https://www.cdcxhl.com/article6/dhoiig.html

成都网站建设公司_创新互联,为您提供静态网站自适应网站网站设计网页设计公司服务器托管搜索引擎优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化