CodeSmith 模版

时间:2022-08-15 18:14:29

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
<%@ Property Name="ns" Type="System.String" Default="Yonyou.Cn" Optional="False" Category="命名空间" Description="描写命名空间" OnChanged="" Editor="" EditorBase="" Serializer="" %>
<%@ Property Name="TableSrc" Type="SchemaExplorer.TableSchema" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace <%=ns%>
{
public class <%=TableSrc.Name%>
{
<%for(int i=0;i<TableSrc.Columns.Count;i++){%>
private <%=GetCSharpTypeFromDBFieldType(TableSrc.Columns[i])%> _<%=ConvertToFieldName(TableSrc.Columns[i].Name) %>;
public string <%=ConvertToPropertyName(TableSrc.Columns[i].Name) %>
{
get{
return _<%=ConvertToFieldName(TableSrc.Columns[i].Name) %>;
}

set{
_<%=ConvertToFieldName(TableSrc.Columns[i].Name) %>=value;
}
}
<%} %>
}
}

<script runat="template">

public string GetCSharpTypeFromDBFieldType(ColumnSchema column)
{
string type;
switch (column.DataType)
{
case DbType.AnsiString: type= "string";break;
case DbType.AnsiStringFixedLength: type= "string";break;
case DbType.Binary: type= "byte[]";break;
case DbType.Boolean: type= "bool";break;
case DbType.Byte: type= "byte";break;
case DbType.Currency: type= "decimal";break;
case DbType.Date: type= "DateTime";break;
case DbType.DateTime: type= "DateTime";break;
case DbType.Decimal: type= "decimal";break;
case DbType.Double: type= "double";break;
case DbType.Guid: type= "Guid";break;
case DbType.Int16: type= "short";break;
case DbType.Int32: type= "int";break;
case DbType.Int64: type= "long";break;
case DbType.Object: type= "object";break;
case DbType.SByte: type= "sbyte";break;
case DbType.Single: type= "float";break;
case DbType.String: type= "string";break;
case DbType.StringFixedLength: type= "string";break;
case DbType.Time: type= "TimeSpan";break;
case DbType.UInt16: type= "ushort";break;
case DbType.UInt32: type= "uint";break;
case DbType.UInt64: type= "ulong";break;
case DbType.VarNumeric: type= "decimal";break;
default:
{
type= "__UNKNOWN__" + column.NativeType;
break;
}
}
if(column.AllowDBNull&&
column.SystemType.IsValueType)
{
type=type+"?";
}
return type;
}

public string ConvertToFieldName(string field)
{
return char.ToLower(field[0]).ToString()+field.Substring(1);
}
public string ConvertToPropertyName(string field)
{
return char.ToUpper(field[0]).ToString()+field.Substring(1);
}
</script>