简单C#2.0模型层CodeSmith模板

简单C#2.0模型层CodeSmith模板

 

<%@ CodeTemplate Language="C#" TargetLanguage="Text" ResponseEncoding="UTF-8"%> <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="内容" Description="数据源表" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> using System; using System.Collections.Generic; using System.Text; namespace <%="cn.tzx"%> { /// <summary> /// <%=SourceTable%>的模型层 /// </summary> public class <%=GetClassName(SourceTable)%> { /// <summary> /// 默认构造 /// </summary> public <%= GetClassName(SourceTable)%>() {} /// <summary> /// 带主键参数的构造 /// <param name="<%=GetPrimaryKeyFieldName(SourceTable)+"_c"%>">主键</param> /// </summary> public <%= GetClassName(SourceTable)%>(<%=GetPrimaryKeyType(SourceTable)%> <%=GetPrimaryKeyFieldName(SourceTable)+"_c"%>) { this.<%=GetPrimaryKeyFieldName(SourceTable)%>=<%=GetPrimaryKeyFieldName(SourceTable)+"_c"%>; } #region 私有字段 private <%=GetPrimaryKeyType(SourceTable)%> <%=GetPrimaryKeyFieldName(SourceTable)%>; <% // 循环输出非主键列的定义 foreach(ColumnSchema colum in SourceTable.NonPrimaryKeyColumns) { %> private <%=GetDataTypeByColumn(colum)%> <%=GetFieldNameByColumn(colum)%>; <% } %> #endregion #region 公共属性 public <%=GetPrimaryKeyType(SourceTable)%> <%=GetPrimaryKeyPropertyName(SourceTable)%> { get{ return this.<%=GetPrimaryKeyFieldName(SourceTable)%>; } set{ this.<%=GetPrimaryKeyFieldName(SourceTable)%> = value; } } <% // 循环输出非主键列的属性 foreach(ColumnSchema colum in SourceTable.NonPrimaryKeyColumns) { %> public <%=GetDataTypeByColumn(colum)%> <%=GetPropertyNameByColumn(colum)%> { get{ return this.<%=GetFieldNameByColumn(colum)%>; } set{ this.<%=GetFieldNameByColumn(colum)%> = value; } } <% } %> #endregion #region 方法 /// <summary> /// 遍历属性成员名称及值 /// </summary> public string ToStringEachMember() { System.Text.StringBuilder sb=new System.Text.StringBuilder(); sb.AppendFormat("{0}:{1}/n","<%=GetPrimaryKeyPropertyName(SourceTable)%>",<%=GetPrimaryKeyPropertyName(SourceTable)%>); <% // 循环输出非主键列的属性 foreach(ColumnSchema colum in SourceTable.NonPrimaryKeyColumns) { %> sb.AppendFormat("{0}:{1}/n","<%=GetPropertyNameByColumn(colum)%>",<%=GetPropertyNameByColumn(colum)%>); <% } %> return sb.ToString(); } #endregion } } <mce:script runat="template"><!-- // 根据表对象获得类名称 public string GetClassName(TableSchema table) { string tempTable; if (table.Name.EndsWith("s")) { tempTable = table.Name.Substring(0,table.Name.Length-1); } else { tempTable = table.Name; } return ConvertToPascal(tempTable); } // 根据表对象获得主键的类型 public string GetPrimaryKeyType(TableSchema table) { if (table.PrimaryKey != null) { if (table.PrimaryKey.MemberColumns.Count == 1) { return GetCSharpDataTypeByDBColumn(table.PrimaryKey.MemberColumns[0]); } else { throw new ApplicationException("此模板只支持单个列的主键"); } } else { throw new ApplicationException("此模板需要有主键的表"); } } // 根据表对象获得主键的名称(原始) public string GetPrimaryKeyName(TableSchema table) { if (table.PrimaryKey != null) { if (table.PrimaryKey.MemberColumns.Count == 1) { return ConvertToCamel(table.PrimaryKey.MemberColumns[0].Name); } else { throw new ApplicationException("此模板只支持单个列的主键"); } } else { throw new ApplicationException("此模板需要有主键的表"); } } // 根据表对象获得主键的字段名(骆驼命名) public string GetPrimaryKeyFieldName(TableSchema table) { return "_" + ConvertToCamel(GetPrimaryKeyName(table)); } // 根据表对象获得主键的属性名(帕斯卡命名) public string GetPrimaryKeyPropertyName(TableSchema table) { return ConvertToPascal(GetPrimaryKeyName(table)); } // 根据列对象获得列的类型 public string GetDataTypeByColumn(ColumnSchema column) { return GetCSharpDataTypeByDBColumn(column); } // 根据列对象获得列的字段名(骆驼命名) public string GetFieldNameByColumn(ColumnSchema column) { return "_" + ConvertToCamel(column.Name); } // 根据列对象获得列的属性名(帕斯卡命名) public string GetPropertyNameByColumn(ColumnSchema column) { return ConvertToPascal(column.Name); } // 根据列对象获得CSharp中类型 public string GetCSharpDataTypeByDBColumnOld(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; } } } //根据列对象获得CSharp中类型 public string GetCSharpDataTypeByDBColumn(ColumnSchema column) { switch (column.DataType) { case DbType.AnsiString: return "string"; case DbType.AnsiStringFixedLength: if(column.Size==1) return "char"+(column.AllowDBNull?"?":""); else return "string"; case DbType.String: return "string"; case DbType.StringFixedLength: if(column.Size==1) return "char"+(column.AllowDBNull?"?":""); else return "string"; case DbType.Binary: return "byte[]"; case DbType.Boolean: return "bool"+(column.AllowDBNull?"?":""); case DbType.Byte: return "byte"+(column.AllowDBNull?"?":""); case DbType.Currency: return "decimal"+(column.AllowDBNull?"?":""); case DbType.Date: return "DateTime"+(column.AllowDBNull?"?":""); case DbType.DateTime: return "DateTime"+(column.AllowDBNull?"?":""); case DbType.Decimal: return "decimal"+(column.AllowDBNull?"?":""); case DbType.Double: return "double"+(column.AllowDBNull?"?":""); case DbType.Guid: return "Guid"+(column.AllowDBNull?"?":""); case DbType.Int16: return "short"+(column.AllowDBNull?"?":""); case DbType.Int32: return "int"+(column.AllowDBNull?"?":""); case DbType.Int64: return "long"+(column.AllowDBNull?"?":""); case DbType.Object: return "object"; case DbType.SByte: return "sbyte"+(column.AllowDBNull?"?":""); case DbType.Single: return "float"+(column.AllowDBNull?"?":""); case DbType.Time: return "TimeSpan"; case DbType.UInt16: return "ushort"+(column.AllowDBNull?"?":""); case DbType.UInt32: return "uint"+(column.AllowDBNull?"?":""); case DbType.UInt64: return "ulong"+(column.AllowDBNull?"?":""); case DbType.VarNumeric: return "decimal"+(column.AllowDBNull?"?":""); default: { return "__UNKNOWN__" + column.NativeType; } } } // 帕斯卡命名转换 public string ConvertToPascal(string str) { return str.Substring(0,1).ToUpper() + str.Substring(1); } // 骆驼命名转换 public string ConvertToCamel(string str) { return str.Substring(0,1).ToLower() + str.Substring(1); } // 重写获得文件名的方法 public override string GetFileName() { return GetClassName(SourceTable) + ".cs"; } // --></mce:script>

 

声明:原来的代码来自网上搜集,再自己稍微修改.

转载于:https://www.cnblogs.com/TangZhongxin/archive/2009/06/15/3942582.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值