动软有点局限。
CodeSmith 写的我有点苦逼,但是扩展性强。
Entity Class
<%@ Template Language="C#" TargetLanguage="Text" %> <%@ Property Name="SourceTable" Default="" Type="SchemaExplorer.TableSchema" %> <%@ Property Name="ProjectName" Type="System.String" Default="eyblog" Optional="False" Category="Strings" Description="this is my blog" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer"%> <%@ Map Name="SqlCSharp" Src="Sql-CSharp" Description="System to C# Type Map" %> <%@ Map Name="DbDataReader" Src="DbType-DataReaderMethod" Description="DbType to DataReader Method Map" %> <%@ Map Name="SqlNativeSqlDb" Src="SqlNativeType-SqlDbType" Description="SqlNativeType to SqlDbType Map" %> <%@ Map Name="DbTypeCSharp" Src="DbType-CSharp" Description="DbType to CSharp Map" %> //该类由codesmith模版编写 using System; namespace <%=ProjectName%>.Model { <%--类--%> public class <%=GetClassName(SourceTable)%>Entity { <% foreach(ColumnSchema column in SourceTable.Columns) { %> <%=GetMemberVariableDeclarationStatement(column)%> <% } %> public <%=GetClassName(SourceTable)%>Entity() { } <%--有参构造函数--%> public <%=GetClassName(SourceTable)%>Entity(<%=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)%>;} get{ <%=GetMemberVariableName(column)%>=value;} } <% } %> } } <script runat="template"> //自定义方法 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); } //自定义属性方法 public string GetPascalMemberVariableName(ColumnSchema column) { string propertyName = GetPropertyName(column); string memberVariableName = "_" + GetCamelCaseName(propertyName); string temp= propertyName.Substring(0,1).ToUpper()+propertyName.Substring(1); return temp; } public string GetMemberVariableDeclarationStatement(ColumnSchema column)//获取成员变量声明 { return GetMemberVariableDeclarationStatement("private", column); } public string GetMemberVariableDeclarationStatement(string protectionLevel, ColumnSchema column) { string statement = protectionLevel + " ";//protectionLevel statement += GetCSharpVariableType(column) + " " + GetMemberVariableName(column); //类型 空格 列名-->字段 string defaultValue = GetMemberVariableDefaultValue(column); if (defaultValue != "") { statement += " = " + defaultValue; } statement += ";";//加分号 return statement; } 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; } public string GetCamelCaseName(string value) { return value.Substring(0, 1).ToLower() + value.Substring(1); } public string GetMemberVariableName(ColumnSchema column) { string propertyName = GetPropertyName(column); string memberVariableName = "_" + GetCamelCaseName(propertyName); return memberVariableName; } 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; } 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 ""; } } } public string GetCSharpVariableType(ColumnSchema column) { if (column.Name.EndsWith("TypeCode")) return column.Name; return DbTypeCSharp[column.DataType.ToString()]; } public string GetReaderMethod(ColumnSchema column) { return DbDataReader[column.DataType.ToString()]; } public string GetClassName(TableSchema table)//根据字段名获取类名的方法 { if (table.Name.EndsWith("s")) { return table.Name.Substring(0, table.Name.Length - 1); } else { return table.Name; } } public string GetSqlDbType(ColumnSchema column) { return SqlNativeSqlDb[column.NativeType.ToString()]; } 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."); } } public override string GetFileName() { return this.GetClassName(this.SourceTable) + ".cs"; } </script>
Data access layer
<%@ Template Language="C#" TargetLanguage="Text" Encoding="utf-8"%> <%@ Property Name="SourceTable" Default="" Type="SchemaExplorer.TableSchema" %> <%@ Property Name="ProjectName" Type="System.String" Default="eyblog" Optional="False" Category="Strings" Description="this is my blog" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer"%> <%@ Map Name="SqlCSharp" Src="Sql-CSharp" Description="System to C# Type Map" %> <%@ Map Name="DbDataReader" Src="DbType-DataReaderMethod" Description="DbType to DataReader Method Map" %> <%@ Map Name="SqlNativeSqlDb" Src="SqlNativeType-SqlDbType" Description="SqlNativeType to SqlDbType Map" %> <%@ Map Name="DbTypeCSharp" Src="DbType-CSharp" Description="DbType to CSharp Map" %> using System.Collections.Generic; using System.Data; using System.SqlClient; using System; using <%=ProjectName%>.Model; using <%=ProjectName%>.DBUtility; namespace <%=ProjectName%>.DAL { public class <%=GetClassName(SourceTable)%> { //查询单个数据 public static <%=GetClassName(SourceTable)%>Entity Select<%=GetClassName(SourceTable)%>Byid(<%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[0])%> t<%=GetMemberVariableName(SourceTable.PrimaryKey.MemberColumns[0])%>) { <%=GetClassName(SourceTable)%>Entity t_<%=GetClassName(SourceTable)%>=new <%=GetClassName(SourceTable)%>Entity(); SqlDataReader sdr=null; using(sdr=SqlDBHelp.GetReader("select * from <%=SourceTable.Name%> where <%=SourceTable.PrimaryKey.MemberColumns[0].Name%>="+t<%=GetMemberVariableName(SourceTable.PrimaryKey.MemberColumns[0])%>")) { if(sde.Reader()) { <% int i=0; for(i=0;i<SourceTable.Columns.Count;i++) { %> t_<%=GetClassName(SourceTable)%>.<%=GetPascalMemberVariableName(SourceTable.Columns[i])%>=(<%=GetCSharpVariableType(SourceTable.Columns[i])%>)sdr.GetValues(<%= i%>); <% } %> } } sdr.Close(); return t_<%=GetClassName(SourceTable)%>; } //查询多条数据 public Ilist<<%=GetClassName(SourceTable)%>Entity> GetAll<%=GetClassName(SourceTable)%>() { Ilist<<%=GetClassName(SourceTable)%>Entity> t_<%=GetClassName(SourceTable)%>s=new List<<%=GetClassName(SourceTable)%>Entity>(); SqlDataReader sdr=null; using(sdr=SqlDBHelp.GetReader("select * from <%=SourceTable.Name%>")) { while(sdr.Reader()) { <%=GetClassName(SourceTable)%>Entity t_<%=GetClassName(SourceTable)%>=new <%=GetClassName(SourceTable)%>Entity(); <% for(i=0;i<SourceTable.Columns.Count;i++) { %> t_<%=GetClassName(SourceTable)%>.<%=GetPascalMemberVariableName(SourceTable.Columns[i])%>=(<%=GetCSharpVariableType(SourceTable.Columns[i])%>)sdr.GetValues(<%=i%>); <% } %> t_<%=GetClassName(SourceTable)%>s.Add(t_<%=GetClassName(SourceTable)%>); } sdr.Close(); } return t_<%=GetClassName(SourceTable)%>s; } //外键查询,几个外键生成几个方法 <% foreach(ColumnSchema column in SourceTable.ForeignKeyColumns) {%> public Ilist<<%=GetClassName(SourceTable)%>Entity> GetAll<%=GetClassName(SourceTable)%>By<%=column.Name%>(<%=GetCSharpVariableType(column)%> t<%=GetMemberVariableName(column)%>) { Ilist<<%=GetClassName(SourceTable)%>Entity> t_<%=GetClassName(SourceTable)%>s=new List<<%=GetClassName(SourceTable)%>Entity>(); SqlDataReader sdr=null; using(sdr=SqlDBHelp.GetReader("select * from <%=SourceTable.Name%> where <%=column.Name%>="+t<%=GetMemberVariableName(column)%>)) { while(sdr.Reader()) { <%=GetClassName(SourceTable)%>Entity t_<%=GetClassName(SourceTable)%>=new <%=GetClassName(SourceTable)%>Entity(); <% for(i=0;i<SourceTable.Columns.Count;i++) { %> t_<%=GetClassName(SourceTable)%>.<%=GetPascalMemberVariableName(SourceTable.Columns[i])%>=(<%=GetCSharpVariableType(SourceTable.Columns[i])%>)sdr.GetValues(<%=i%>); <% } %> t_<%=GetClassName(SourceTable)%>s.Add(t_<%=GetClassName(SourceTable)%>); } sdr.Close(); } return t_<%=GetClassName(SourceTable)%>s; } <%} %> //添加数据 public static int Insert<%=GetClassName(SourceTable)%>(<%=GetClassName(SourceTable)%>Entity T_<%=GetClassName(SourceTable)%>) { SqlParameter [] p=new SqlParameter[] { <% i=0; foreach(ColumnSchema column in SourceTable.Columns) { i++; if(i!=SourceTable.Columns.Count) { %> new SqlParameter("@<%=GetPascalMemberVariableName(column)%>",T_<%=GetClassName(SourceTable)%>.<%= GetPascalMemberVariableName(column)%>), <% }else { %> new SqlParameter("@<%=GetPascalMemberVariableName(column)%>",T_<%=GetClassName(SourceTable)%>.<%= GetPascalMemberVariableName(column)%>) <% } } %> }; int i=SqlDBHelp.GetExecute("insert into <%=SourceTable.Name%> values(<%=GetColumns(SourceTable)%>)", p); return i; } //修改数据 public static int Update<%=GetClassName(SourceTable)%>(<%=GetClassName(SourceTable)%>Entity T_<%=GetClassName(SourceTable)%>) { SqlParameter [] p=new SqlParameter[] { <% i=0; foreach(ColumnSchema column in SourceTable.Columns) { i++; if(i!=SourceTable.Columns.Count) { %> new SqlParameter("@<%=GetPascalMemberVariableName(column)%>",T_<%=GetClassName(SourceTable)%>.<%= GetPascalMemberVariableName(column)%>), <% }else { %> new SqlParameter("@<%=GetPascalMemberVariableName(column)%>",T_<%=GetClassName(SourceTable)%>.<%= GetPascalMemberVariableName(column)%>) <% } } %> }; int i=SqlDBHelp.GetExecute("update <%=SourceTable.Name%> set <%=GetUpdateColumns(SourceTable)%>", p); return i; } //删除数据,主键,不考虑联合主键 public static int Delete<%=GetClassName(SourceTable)%>(<%=GetCSharpVariableType(SourceTable.PrimaryKey.MemberColumns[0])%> t<%=GetMemberVariableName(SourceTable.PrimaryKey.MemberColumns[0])%>) { int i=SqlDBHelp.GetExecute("delete from <%=SourceTable.Name%> where <%=SourceTable.PrimaryKey.MemberColumns[0].Name%>="+t<%=GetMemberVariableName(SourceTable.PrimaryKey.MemberColumns[0])%>); return i; } } } <script runat="template"> //自定义方法 public string GetPascalMemberVariableName(ColumnSchema column) { string propertyName = GetPropertyName(column); string memberVariableName = "_" + GetCamelCaseName(propertyName); string temp= propertyName.Substring(0,1).ToUpper()+propertyName.Substring(1); return temp; } public string GetColumns(TableSchema table) { string temp=""; foreach(ColumnSchema column in table.Columns) { temp+="@"+GetPascalMemberVariableName(column)+","; } return temp.Substring(0,temp.Length-1); } public string GetUpdateColumns(TableSchema table) { string temp=""; foreach(ColumnSchema column in table.NonPrimaryKeyColumns) { temp+=column.Name+"=@"+GetPascalMemberVariableName(column)+","; } temp=temp.Substring(0,temp.Length-1); //空格是Sql语句控制符 temp+=" "+"where "+table.PrimaryKey.MemberColumns[0].Name+"="+"@"+GetPascalMemberVariableName(table.PrimaryKey.MemberColumns[0]); return temp; } //------end public string GetMemberVariableDeclarationStatement(ColumnSchema column) { return GetMemberVariableDeclarationStatement("private", column); } 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; } 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; } public string GetCamelCaseName(string value) { return value.Substring(0, 1).ToLower() + value.Substring(1); } public string GetMemberVariableName(ColumnSchema column) { string propertyName = GetPropertyName(column); string memberVariableName = "_" + GetCamelCaseName(propertyName); return memberVariableName; } 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; } 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 ""; } } } public string GetCSharpVariableType(ColumnSchema column) { if (column.Name.EndsWith("TypeCode")) return column.Name; return DbTypeCSharp[column.DataType.ToString()]; } public string GetReaderMethod(ColumnSchema column) { return DbDataReader[column.DataType.ToString()]; } public string GetClassName(TableSchema table) { if (table.Name.EndsWith("s")) { return table.Name.Substring(0, table.Name.Length - 1); } else { return table.Name; } } public string GetSqlDbType(ColumnSchema column) { return SqlNativeSqlDb[column.NativeType.ToString()]; } 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."); } } public override string GetFileName() { return this.GetClassName(this.SourceTable) + ".cs"; } </script>