您的位置:首页 > 运维架构 > 网站架构

codesmith 三层架构代码生成

2016-06-24 16:43 549 查看
bll 代码:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a very simple business object." %>
<%@ Property Name="TargetTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="ModelsNamespace" Default="MyOffice.MODEL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALNamespace" Default="MyOffice.DAL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="BLLNamespace" Default="MyOffice.BLL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="BLLClassNameSurfix" Default="Manager" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALClassNameSurfix" Default="Service" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="IDALNamespace" Default="MyOffice.IDAL" Type="System.String" Category="Context"  Description="TargetTable that the object is based on."  %>
<%@ Property Name="DataAccess" Default="DataAccess" Type="System.String" Category="Context"  Description="TargetTable that the object is based on."  %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<% PrintHeader(); %>
using System;
using System.Collections.Generic;
using System.Text;
using <%= DALNamespace %>;
using <%= ModelsNamespace %>;
using <%= IDALNamespace %>;
namespace <%= BLLNamespace %>
{
public static partial class <%= GetBLLClassName() %>
{
public static <%=GetIDALInterfaceName() %>  <%=GetiDALInterfaceName() %> = <%=DataAccess %>.Create<%=TargetTable.Name%>();

public static <%= GetModelClassName() %> Add<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>)
{
<%
if(TargetTable.ForeignKeyColumns.Count>0)
{
foreach(TableKeySchema key in TargetTable.ForeignKeys)
{
if(key.PrimaryKeyTable.ExtendedProperties.Contains("DefaultId"))
{
%>
if (<%= GetModelParamName() %>.<%= GetFKPropertyName(key) %> == null)
{
<%= GetModelParamName() %>.<%= GetFKPropertyName(key) %> = <%= GetFKPropertyType(key) %><%= BLLClassNameSurfix %>.GetDefault<%= GetFKPropertyType(key) %>();
}

<%
}
}
}
%>
return <%=GetiDALInterfaceName() %>.Add<%= GetModelClassName() %>(<%= GetModelParamName() %>);
}
public static void Delete<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>)
{
<%=GetiDALInterfaceName() %>.Delete<%= GetModelClassName() %>(<%= GetModelParamName() %>);
}
public static void Delete<%= GetModelClassName() %>ById(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
{
<%=GetiDALInterfaceName() %>.Delete<%= GetModelClassName() %>ById(<%= GetPKParamName() %>);
}
public static void Modify<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>)
{
<%=GetiDALInterfaceName() %>.Modify<%= GetModelClassName() %>(<%= GetModelParamName() %>);
}

public static IList<<%= GetModelClassName() %>> GetAll<%= MakePlural(GetModelClassName()) %>()
{
return <%=GetiDALInterfaceName() %>.GetAll<%= MakePlural(GetModelClassName()) %>();
}
public static <%= GetModelClassName() %> Get<%= GetModelClassName() %>By<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
{
return <%=GetiDALInterfaceName() %>.Get<%= GetModelClassName() %>By<%= GetPKPropertyName() %>(<%= GetPKParamName() %>);
}
<%
if(TargetTable.ExtendedProperties.Contains("DefaultId"))
{
int defaultId = Convert.ToInt32(TargetTable.ExtendedProperties["DefaultId"].Value);
%>
<%-- public static Book GetDefaultBook() --%>
public static <%= GetModelClassName() %> GetDefault<%= GetModelClassName() %>()
{
return Get<%= GetModelClassName() %>ById(<% =defaultId %>);
}
<%
}
%>
}
}
<script runat="template">
///////////////////////////////////////////////////////////////
// CLASS NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetIDALInterfaceName()
{
return "I"+ GetModelClassName() +  DALClassNameSurfix;
}
public string GetiDALInterfaceName()
{
return "i"+ GetModelClassName() +  DALClassNameSurfix;
}
public string GetBLLClassName()
{
return  GetModelClassName() + BLLClassNameSurfix;
}
public string GetDALClassName()
{
return  GetModelClassName() + DALClassNameSurfix;
}
public string GetModelMemberVarName()
{
return GetModelParamName();
}
public string GetModelParamName()
{
return MakeCamel(GetModelClassName());
}
public string GetModelClassName()
{
return  GetModelClassName(TargetTable);
}
public string GetModelClassName(TableSchema table)
{
string result;
if ( table.ExtendedProperties.Contains("ModelName") )
{
result = (string)table.ExtendedProperties["ModelName"].Value;
return MakePascal(result);
}
if (table.Name.EndsWith("s"))
{
//result = table.Name.Substring(0, table.Name.Length - 1);
result = MakeSingle(table.Name);
}
else
{
result = table.Name;
}
return MakePascal(result);
}
///////////////////////////////////////////////////////////////
// PRIMARY KEY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPKPropertyType()
{
return  GetPKType(TargetTable);
}
public string GetPKType()
{
return  GetPKType(TargetTable);
}
public string GetPKType(TableSchema TargetTable)
{
if (TargetTable.PrimaryKey != null)
{
if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpTypeFromDBFieldType(TargetTable.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 MyTables with a primary key.");
}
}
public string GetCSharpTypeFromDBFieldType(ColumnSchema column)
{
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;
}
}
}
///////////////////////////////////////////////////////////////
// PRIMARY KEY NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPKPropertyName()
{
return MakePascal(GetPKName());
}
public string GetPKMemberVarName()
{
return MakeCamel(GetPKName());
}
public string GetPKParamName()
{
return GetPKMemberVarName();
}
public string GetPKName()
{
return GetPKName(TargetTable);
}
public string GetPKName(TableSchema TargetTable)
{
if (TargetTable.PrimaryKey != null)
{
if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
{
return TargetTable.PrimaryKey.MemberColumns[0].Name;
}
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.");
}
}

///////////////////////////////////////////////////////////////
// FOREIGH KEY PROPERTY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKPropertyType(TableKeySchema key)
{
return MakePascal(GetFKPrimaryModelClassName(key));
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY ID NAMEs by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKForeignIdName(TableKeySchema key)
{
return key.ForeignKeyMemberColumns[0].Name;
}
public string GetFKPrimaryIdName(TableKeySchema key)
{
return key.PrimaryKeyMemberColumns[0].Name;
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY PROPERTY NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKMemberVarName(TableKeySchema key)
{
// return MakeCamel(GetFKName(key));
string result = GetFKForeignIdName(key);
if( result.ToLower().EndsWith("id") )
{
result = result.Substring(0, result.Length - 2);
}
return MakeCamel(result);
}
public string GetFKPropertyName(TableKeySchema key)
{
return MakePascal(GetFKMemberVarName(key));
}
public string GetFKPrimaryModelClassName(TableKeySchema key)
{
return GetModelClassName(key.PrimaryKeyTable);
}
//So dirty function! -- reviewed by shenbo
public string MakeCamel(string value)
{
return value.Substring(0, 1).ToLower() + value.Substring(1);
}
//I will be dirty too! -- coded by shenbo
public string MakePascal(string value)
{
return value.Substring(0, 1).ToUpper() + value.Substring(1);
}
public string MakePlural(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])y$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)$");
Regex plural3 = new Regex("(?<keep>[sxzh])$");
Regex plural4 = new Regex("(?<keep>[^sxzhy])$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}ies");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}s");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}es");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}s");
return name;
}
public string MakeSingle(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
Regex plural3 = new Regex("(?<keep>[sxzh])es$");
Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}y");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}");
return name;
}
public override string GetFileName()
{
return this.GetBLLClassName() + ".cs";
}
public void PrintHeader()
{
Response.WriteLine("//============================================================");
Response.WriteLine("// Product name:   MyProject");
Response.WriteLine("// Version:    1.0");
Response.WriteLine("// Coded by:   ocean ");
Response.WriteLine("// Auto generated at:  {0}", DateTime.Now);
Response.WriteLine("//============================================================");
Response.WriteLine();
}
</script>


dal 代码:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a very simple business object." %>
<%@ Property Name="TargetTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="ModelsNamespace" Default="MyOffice.MODEL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALNamespace" Default="MyOffice.DAL.SQL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALClassNameSurfix" Default="Service" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="IDALNamespace" Default="MyOffice.IDAL" Type="System.String" Category="Context"  Description="TargetTable that the object is based on."  %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<% PrintHeader(); %>
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using <%= ModelsNamespace %>;
using <%= IDALNamespace %>;
namespace <%= DALNamespace %>
{
public  partial class <%= GetDALClassName() %>:<%=GetIDALInterfaceName()%>
{
<%-- public static Book AddBook(Book book) --%>
public  <%= GetModelClassName() %> Add<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>)
{
string sql =
<%= GetInsertSQLLine1()%>
<%= GetInsertSQLLine2()%>

<% if(IsIdentity()){ %>
sql += " ; SELECT @@IDENTITY";
<%}%>
try
{
SqlParameter[] para = new SqlParameter[]
{
<%
foreach(TableKeySchema key in TargetTable.ForeignKeys)
{
%>
new SqlParameter("@<%= GetFKForeignIdName(key) %>", <%= GetModelParamName() %>.<%= GetFKPropertyName(key) %>.<%= GetFKPrimaryIdName(key) %>), //FK
<%
}
for(int i=0; i<TargetTable.NonKeyColumns.Count-1; i++)
{
ColumnSchema column = TargetTable.NonKeyColumns[i];
%>
new SqlParameter("@<%= column.Name %>", <%= GetModelParamName() %>.<%= column.Name %>),
<%
}

if(TargetTable.NonKeyColumns.Count > 0){
for(int i=TargetTable.NonKeyColumns.Count-1; i<TargetTable.NonKeyColumns.Count; i++)
{
ColumnSchema lastColumn = TargetTable.NonKeyColumns[i];
%>
new SqlParameter("@<%= lastColumn.Name %>", <%= GetModelParamName() %>.<%= lastColumn.Name %>)
<%
}
}

%>
};
<% if(IsIdentity()){ %>
int newId = DBHelper.GetScalar(sql, para);
return Get<%= GetModelClassName() %>By<%= GetPKPropertyName() %>(newId);
<%}else{%>
return <%= GetModelParamName() %>;
<%}%>
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}

<%-- public static bool DeleteBook(Book book) --%>
public  void Delete<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>)
{
Delete<%= GetModelClassName() %>By<%= GetPKPropertyName() %>( <%= GetModelParamName() %>.<%= GetPKPropertyName() %> );
}
public void Delete<%= GetModelClassName() %>ById(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
{
Delete<%= GetModelClassName() %>By<%= GetPKPropertyName() %>(  <%= GetPKParamName() %> );
}
<%-- public static bool DeleteBookById(int id) --%>
public  void Delete<%= GetModelClassName() %>By<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
{
string sql = "DELETE <%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";
try
{
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@<%= GetPKName() %>", <%= GetPKParamName() %>)
};

DBHelper.ExecuteCommand(sql, para);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}

<%-- public static void DeleteBookByISBN(string iSBN) --%>
<% foreach( IndexSchema index in TargetTable.Indexes )
{
if(index.IsUnique && !index.IsPrimaryKey && index.MemberColumns.Count  == 1)
{
string indexColumnName = index.MemberColumns[0].Name;
ColumnSchema indexColumn = index.MemberColumns[0];
string indexPropertyName = MakePascal(indexColumnName);
string indexParamType = GetParamType(indexColumn);
string indexParamName = MakeCamel(indexColumnName);
string indexMemberName = MakeCamel(indexColumnName);
%>
<%-- public static void DeleteBookByISBN(string iSBN) --%>
public  void Delete<%= GetModelClassName() %>By<%= indexPropertyName %>(<%= indexParamType %> <%= indexParamName %>)
{
string sql = "DELETE <%= TargetTable.Name %> WHERE <%= indexPropertyName %> = @<%= indexPropertyName %>";
try
{
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@<%= indexPropertyName %>", <%= indexParamName %>)
};

DBHelper.ExecuteCommand(sql, para);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}

<% } %>
<% } %>
<%-- public static bool ModifyBook(Book book) --%>
public  void Modify<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>)
{
string sql =
"UPDATE <%= TargetTable.Name %> " +
"SET " +
<%
foreach(TableKeySchema key in TargetTable.ForeignKeys)
{
%>
"<%= GetFKForeignIdName(key) %> = @<%= GetFKForeignIdName(key) %>, " + //FK
<%
}
for(int i=0; i<TargetTable.NonKeyColumns.Count-1; i++)
{
ColumnSchema column = TargetTable.NonKeyColumns[i];
%>
"<%= column.Name %> = @<%= column.Name %>, " +
<%
}

if(TargetTable.NonKeyColumns.Count > 0){
for(int i=TargetTable.NonKeyColumns.Count-1; i<TargetTable.NonKeyColumns.Count; i++)
{
ColumnSchema column = TargetTable.NonKeyColumns[i];
%>
"<%= column.Name %> = @<%= column.Name %> " +
<%
}
}
%>
"WHERE <%= GetPKName() %> = @<%= GetPKName() %>";

try
{
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@<%= GetPKName() %>", <%= GetModelParamName() %>.<%= GetPKName() %>),
<%
foreach(TableKeySchema key in TargetTable.ForeignKeys)
{
%>
new SqlParameter("@<%= GetFKForeignIdName(key) %>", <%= GetModelParamName() %>.<%= GetFKPropertyName(key) %>.<%= GetFKPrimaryIdName(key) %>), //FK
<%
}
for(int i=0; i<TargetTable.NonKeyColumns.Count-1; i++)
{
ColumnSchema column = TargetTable.NonKeyColumns[i];
%>
new SqlParameter("@<%= column.Name %>", <%= GetModelParamName() %>.<%= column.Name %>),
<%
}

if(TargetTable.NonKeyColumns.Count > 0){
for(int i=TargetTable.NonKeyColumns.Count-1; i<TargetTable.NonKeyColumns.Count; i++)
{
ColumnSchema lastColumn = TargetTable.NonKeyColumns[i];
%>
new SqlParameter("@<%= lastColumn.Name %>", <%= GetModelParamName() %>.<%= lastColumn.Name %>)
<%
}
}
%>
};
DBHelper.ExecuteCommand(sql, para);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}
<%-- public static IList<Book> GetAllBooks() --%>
public  IList<<%= GetModelClassName() %>> GetAll<%= MakePlural(GetModelClassName()) %>()
{
string sqlAll = "SELECT * FROM <%= TargetTable.Name %>";
return Get<%= MakePlural(GetModelClassName()) %>BySql( sqlAll );
}

<%-- public static Book GetBookById(int id) --%>
public  <%= GetModelClassName() %> Get<%= GetModelClassName() %>By<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
{
string sql = "SELECT * FROM <%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";
<% if( TargetTable.ForeignKeys.Count != 0 ) %>
<% { %>
<% foreach(TableKeySchema key in TargetTable.ForeignKeys) %>
<% { %>
<%= GetPKType(key.PrimaryKeyTable) %> <%= MakeCamel(GetFKForeignIdName(key)) %>;
<% } %>
<% } %>
try
{
SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@<%= GetPKPropertyName() %>", <%= GetPKParamName() %>));
if (reader.Read())
{
<%= GetModelClassName() %> <%= GetModelParamName() %> = new <%= GetModelClassName() %>();
<% foreach(ColumnSchema column in TargetTable.NonForeignKeyColumns) %>
<% { %>
<%= GetModelParamName() %>.<%= GetPropertyName(column) %> = (<%= GetPropertyType(column) %>)reader["<%= column.Name %>"];
<% } %>
<% foreach(TableKeySchema key in TargetTable.ForeignKeys) %>
<% { %>
<%= MakeCamel(GetFKForeignIdName(key)) %> = (<%= GetPKType(key.PrimaryKeyTable) %>)reader["<%= key.ForeignKeyMemberColumns[0].Name %>"]; //FK
<% } %>
reader.Close();
<% if( TargetTable.ForeignKeys.Count != 0 ) %>
<% { %>
<% foreach(TableKeySchema key in TargetTable.ForeignKeys) %>
<% { %>
<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>  i<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>  = new <%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>();
<%= GetModelParamName() %>.<%= GetFKPropertyName(key) %> = i<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>.Get<%= GetFKPropertyType(key) %>By<%= GetFKPrimaryIdName(key) %>(<%= MakeCamel(GetFKForeignIdName(key)) %>);
<% } %>
<% } %>

return <%= GetModelParamName() %>;
}
else
{
reader.Close();
return null;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}

<%-- public static Book GetBookByISBN(string iSBN) --%>
<% foreach( IndexSchema index in TargetTable.Indexes )
{
if(index.IsUnique && !index.IsPrimaryKey && index.MemberColumns.Count  == 1)
{
string indexColumnName = index.MemberColumns[0].Name;
ColumnSchema indexColumn = index.MemberColumns[0];
string indexPropertyName = MakePascal(indexColumnName);
string indexParamType = GetParamType(indexColumn);
string indexParamName = MakeCamel(indexColumnName);
string indexMemberName = MakeCamel(indexColumnName);
%>
<%-- public static Book GetBookByISBN(string iSBN) --%>
public  <%= GetModelClassName() %> Get<%= GetModelClassName() %>By<% =indexPropertyName %>(<%= indexParamType %> <%= indexParamName %>)
{
string sql = "SELECT * FROM <%= TargetTable.Name %> WHERE <%= indexColumnName %> = @<%= indexColumnName %>";
<% if( TargetTable.ForeignKeys.Count != 0 ) %>
<% { %>
<% foreach(TableKeySchema key in TargetTable.ForeignKeys) %>
<% { %>
<%= GetPKType(key.PrimaryKeyTable) %> <%= MakeCamel(GetFKForeignIdName(key)) %>;
<% } %>
<% } %>
try
{
SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@<%= indexColumnName %>", <%= indexParamName %>));
if (reader.Read())
{
<%= GetModelClassName() %> <%= GetModelParamName() %> = new <%= GetModelClassName() %>();
<% foreach(ColumnSchema column in TargetTable.NonForeignKeyColumns) %>
<% { %>
<%= GetModelParamName() %>.<%= GetPropertyName(column) %> = (<%= GetPropertyType(column) %>)reader["<%= column.Name %>"];
<% } %>
<% foreach(TableKeySchema key in TargetTable.ForeignKeys) %>
<% { %>
<%= MakeCamel(GetFKForeignIdName(key)) %> = (<%= GetPKType(key.PrimaryKeyTable) %>)reader["<%= key.ForeignKeyMemberColumns[0].Name %>"]; //FK
<% } %>
reader.Close();
<% if( TargetTable.ForeignKeys.Count != 0 ) %>
<% { %>

<% foreach(TableKeySchema key in TargetTable.ForeignKeys) %>
<% { %>
<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>  i<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %> = new <%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>();
<%= GetModelParamName() %>.<%= GetFKPropertyName(key) %> = i<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>.Get<%= GetFKPropertyType(key) %>By<%= GetFKPrimaryIdName(key) %>(<%= MakeCamel(GetFKForeignIdName(key)) %>);
<% } %>
<% } %>
return <%= GetModelParamName() %>;
}
else
{
reader.Close();
return null;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}

<% } %>
<% } %>

<%-- public static IList<Book> GetBooksBySql( string sql ) --%>
private  IList<<%= GetModelClassName() %>> Get<%= MakePlural(GetModelClassName()) %>BySql( string safeSql )
{
List<<%= GetModelClassName() %>> list = new List<<%= GetModelClassName() %>>();
try
{
DataTable table = DBHelper.GetDataSet( safeSql );

foreach (DataRow row in table.Rows)
{
<%= GetModelClassName() %> <%= GetModelParamName() %> = new <%= GetModelClassName() %>();

<% foreach(ColumnSchema column in TargetTable.NonForeignKeyColumns) %>
<% { %>
<%= GetModelParamName() %>.<%= GetPropertyName(column) %> = (<%= GetPropertyType(column) %>)row["<%= column.Name %>"];
<% } %>
<% foreach(TableKeySchema key in TargetTable.ForeignKeys) %>
<% { %>
<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %> i<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %> = new <%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>();
<%= GetModelParamName() %>.<%= GetFKPropertyName(key) %> = i<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>.Get<%= GetFKPropertyType(key) %>By<%= GetFKPrimaryIdName(key) %>((<%= GetPKType(key.PrimaryKeyTable) %>)row["<%= key.ForeignKeyMemberColumns[0].Name %>"]); //FK
<% } %>

list.Add(<%= GetModelParamName() %>);
}

return list;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}

<%-- public static IList<Book> GetBooksBySql( string sql, params SqlParameter[] values ) --%>
private  IList<<%= GetModelClassName() %>> Get<%= MakePlural(GetModelClassName()) %>BySql( string sql, params SqlParameter[] values )
{
List<<%= GetModelClassName() %>> list = new List<<%= GetModelClassName() %>>();
try
{
DataTable table = DBHelper.GetDataSet( sql, values );

foreach (DataRow row in table.Rows)
{
<%= GetModelClassName() %> <%= GetModelParamName() %> = new <%= GetModelClassName() %>();

<% foreach(ColumnSchema column in TargetTable.NonForeignKeyColumns) %>
<% { %>
<%= GetModelParamName() %>.<%= GetPropertyName(column) %> = (<%= GetPropertyType(column) %>)row["<%= column.Name %>"];
<% } %>
<% foreach(TableKeySchema key in TargetTable.ForeignKeys) %>
<% { %>
<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>  i<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %> = new <%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>();
<%= GetModelParamName() %>.<%= GetFKPropertyName(key) %> = i<%= GetFKPropertyType(key) %><%= DALClassNameSurfix %>.Get<%= GetFKPropertyType(key) %>By<%= GetFKPrimaryIdName(key) %>((<%= GetPKType(key.PrimaryKeyTable) %>)row["<%= key.ForeignKeyMemberColumns[0].Name %>"]); //FK
<% } %>

list.Add(<%= GetModelParamName() %>);
}

return list;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}

}

}
}
<script runat="template">
///////////////////////////////////////////////////////////////
// CLASS NAMES by Shen Bo
///////////////////////////////////////////////////////////////
public string GetIDALInterfaceName()
{
return "I"+ GetModelClassName() +  DALClassNameSurfix;
}
// UserService
public string GetDALClassName()
{
return  GetModelClassName() + DALClassNameSurfix;
}
// User
public string GetModelClassName()
{
return  GetModelClassName(TargetTable);
}
// user
public string GetModelMemberVarName()
{
return GetModelParamName();
}
// user
public string GetModelParamName()
{
return MakeCamel(GetModelClassName());
}
// User
public string GetModelClassName(TableSchema table)
{
string result;
if ( table.ExtendedProperties.Contains("ModelName") )
{
result = (string)table.ExtendedProperties["ModelName"].Value;
return MakePascal(result);
}
if (table.Name.EndsWith("s"))
{
//result = table.Name.Substring(0, table.Name.Length - 1);
result = MakeSingle(table.Name);
}
else
{
result = table.Name;
}
return MakePascal(result);
}

///////////////////////////////////////////////////////////////
// INSERT SQL LINES by Shen Bo
///////////////////////////////////////////////////////////////
// "INSERT users (loginid, loginpwd, username, address, phone, mail, roleId, userstateid)" +
public string GetInsertSQLLine1()
{
string result;
result = "\"INSERT " + TargetTable.Name + " (";
foreach(ColumnSchema column in TargetTable.NonPrimaryKeyColumns)
{
result += column.Name + ", ";
}
result = result.Substring(0, result.Length-2);
result += ")\" +";
return result;
}
// "VALUES (@LoginId,@LoginPwd,@UserName,@Address,@Phone,@Mail,@RoleId,@UserStateId)";
public string GetInsertSQLLine2()
{
string result;
result = "\"VALUES (";
foreach(ColumnSchema column in TargetTable.NonPrimaryKeyColumns)
{
result += "@" + column.Name + ", ";
}
result = result.Substring(0, result.Length-2);
result += ")\";";
return result;
}

///////////////////////////////////////////////////////////////
// PRIMARY KEY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
// int
public string GetPKPropertyType()
{
return  GetPKType(TargetTable);
}
// int
public string GetPKType()
{
return  GetPKType(TargetTable);
}
public bool IsIdentity(){
bool result = false;
string type = GetPKType();
switch(type){
case "short":
case "int":
case "long":
case "ushort":
case "uint":
case "ulong":
result = true;
break;
default:
result = false;
break;
}
return result;
}
// int
public string GetPKType(TableSchema TargetTable)
{
if (TargetTable.PrimaryKey != null)
{
if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpTypeFromDBFieldType(TargetTable.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 MyTables with a primary key.");
}
}

///////////////////////////////////////////////////////////////
// PRIMARY KEY NAME by Shen Bo
///////////////////////////////////////////////////////////////
// Id
public string GetPKPropertyName()
{
return MakePascal(GetPKName());
}
// id
public string GetPKParamName()
{
return GetPKMemberVarName();
}
// id
public string GetPKMemberVarName()
{
return MakeCamel(GetPKName());
}
// Id
public string GetPKName()
{
return GetPKName(TargetTable);
}
// Id
public string GetPKName(TableSchema TargetTable)
{
if (TargetTable.PrimaryKey != null)
{
if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
{
return TargetTable.PrimaryKey.MemberColumns[0].Name;
}
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.");
}
}

///////////////////////////////////////////////////////////////
// FOREIGH KEY PROPERTY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
// UserState
public string GetFKPropertyType(TableKeySchema key)
{
return MakePascal(GetFKPrimaryModelClassName(key));
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY PROPERTY NAME by Shen Bo
///////////////////////////////////////////////////////////////
// userState
public string GetFKMemberVarName(TableKeySchema key)
{
string result = GetFKForeignIdName(key);
if( result.ToLower().EndsWith("id") )
{
result = result.Substring(0, result.Length - 2);
}
return MakeCamel(result);
}
// UserState
public string GetFKPropertyName(TableKeySchema key)
{
return MakePascal(GetFKMemberVarName(key));
}
// UserState
public string GetFKPrimaryModelClassName(TableKeySchema key)
{
return GetModelClassName(key.PrimaryKeyTable);
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY ID NAMEs by Shen Bo
///////////////////////////////////////////////////////////////
//In User table => UserStateId
public string GetFKForeignIdName(TableKeySchema key)
{
return key.ForeignKeyMemberColumns[0].Name;
}
//In UserState table => Id
public string GetFKPrimaryIdName(TableKeySchema key)
{
return key.PrimaryKeyMemberColumns[0].Name;
}

///////////////////////////////////////////////////////////////
// PROPERTY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPropertyType(ColumnSchema column)
{
return GetCSharpTypeFromDBFieldType(column);
}
public string GetMemberVarType(ColumnSchema column)
{
return GetCSharpTypeFromDBFieldType(column);
}
public string GetParamType(ColumnSchema column)
{
return GetCSharpTypeFromDBFieldType(column);
}
public string GetCSharpTypeFromDBFieldType(ColumnSchema column)
{
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;
}
}
}
///////////////////////////////////////////////////////////////
// PROPERTY NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetMemberVarName(ColumnSchema column)
{
return MakeCamel(GetNameFromDBFieldName(column));
}
public string GetPropertyName(ColumnSchema column)
{
return MakePascal(GetNameFromDBFieldName(column));
}
public string GetNameFromDBFieldName(ColumnSchema column)
{
string name = column.Name;
if(name.StartsWith(GetDALClassName()))
{
name = name.Substring(GetDALClassName().Length);
}
return name;
}

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 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;
}
}
}
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;
}
}
//So dirty function! -- reviewed by shenbo
public string MakeCamel(string value)
{
return value.Substring(0, 1).ToLower() + value.Substring(1);
}
//I will be dirty too! -- coded by shenbo
public string MakePascal(string value)
{
return value.Substring(0, 1).ToUpper() + value.Substring(1);
}
public string MakePlural(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])y$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)$");
Regex plural3 = new Regex("(?<keep>[sxzh])$");
Regex plural4 = new Regex("(?<keep>[^sxzhy])$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}ies");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}s");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}es");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}s");
return name;
}
public string MakeSingle(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
Regex plural3 = new Regex("(?<keep>[sxzh])es$");
Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}y");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}");
return name;
}
public override string GetFileName()
{
return this.GetDALClassName() + ".cs";
}
public void PrintHeader()
{
Response.WriteLine("//============================================================");
Response.WriteLine("// Producnt name:   MyProject");
Response.WriteLine("// Version:    1.0");
Response.WriteLine("// Coded by:   ocean");
Response.WriteLine("// Auto generated at:  {0}", DateTime.Now);
Response.WriteLine("//============================================================");
Response.WriteLine();
}
</script>


dataaccess 代码:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a very simple business object." %>
<%@ Property Name="TargetDataBase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="ModelsNamespace" Default="MyOffice.MODEL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALNamespace" Default="MyOffice.DAL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="BLLNamespace" Default="MyOffice.BLL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="BLLClassNameSurfix" Default="Manager" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALClassNameSurfix" Default="Service" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="IDALNamespace" Default="MyOffice.IDAL" Type="System.String" Category="Context"  Description="TargetTable that the object is based on."  %>
<%@ Property Name="DataAccess" Default="DataAccess" Type="System.String" Category="Context"  Description="TargetTable that the object is based on."  %>
<%@ Property Name="DataBaseName" Default="sqlserver" Type="System.String" Category="Context"  Description="TargetTable that the object is based on."  %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<% PrintHeader(); %>
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;
using <%= IDALNamespace %>;
namespace <%= DALNamespace %>
{
public  class <%= DataAccess %>
{
static string path = ConfigurationManager.AppSettings["<%= DataBaseName %>"];

<%
foreach(TableSchema table in TargetDataBase.Tables)
{
%>
public static  <%=GetInterfaceIDALName(table)%>  <%=GetCreateInterfaceName(table.Name)%>()
{
string className = path+"<%=GetDALClassName(table) %>";
return (<%=GetInterfaceIDALName(table)%> )Assembly.Load("MyOffice.DAL").CreateInstance(className);
}
<%
}
%>

}
}
<script runat="template">
///////////////////////////////////////////////////////////////
// CLASS NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetInterfaceIDALName(TableSchema table)
{
return "I" + GetModelClassName(table) + DALClassNameSurfix;
}
public string GetDALClassName(TableSchema table)
{
return  GetModelClassName(table) + DALClassNameSurfix;
}
public string GetCreateInterfaceName(string tableName)
{
return "Create"+tableName;
}

public string GetModelClassName(TableSchema table)
{
string result;
if ( table.ExtendedProperties.Contains("ModelName") )
{
result = (string)table.ExtendedProperties["ModelName"].Value;
return MakePascal(result);
}
if (table.Name.EndsWith("s"))
{
//result = table.Name.Substring(0, table.Name.Length - 1);
result = MakeSingle(table.Name);
}
else
{
result = table.Name;
}
return MakePascal(result);
}

//I will be dirty too! -- coded by shenbo
public string MakePascal(string value)
{
return value.Substring(0, 1).ToUpper() + value.Substring(1);
}

public string MakeSingle(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
Regex plural3 = new Regex("(?<keep>[sxzh])es$");
Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}y");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}");
return name;
}
public override string GetFileName()
{
return DataAccess+".cs";
}
public void PrintHeader()
{
Response.WriteLine("//============================================================");
Response.WriteLine("// Product name:   MyProject");
Response.WriteLine("// Version:    1.0");
Response.WriteLine("// Coded by:   ocean ");
Response.WriteLine("// Auto generated at:  {0}", DateTime.Now);
Response.WriteLine("//============================================================");
Response.WriteLine();
}
</script>


idal 代码:

<%--
Name:MyOffice IDAL
Author: ocean
Description: MyOffice IDAL templete
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a very simple DAL interface object." %>
<%@ Property Name="TargetTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="ModelsNamespace" Default="MyOffice.MODEL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALNamespace" Default="MyOffice.DAL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="BLLNamespace" Default="MyOffice.BLL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="IDALNamespace" Default="MyOffice.IDAL" Type="System.String" Category="Context"  Description="TargetTable that the object is based on."  %>
<%@ Property Name="BLLClassNameSurfix" Default="Manager" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALClassNameSurfix" Default="Service" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<% PrintHeader(); %>
using System;
using System.Collections.Generic;
using System.Text;
using <%= ModelsNamespace %>;
namespace <%= IDALNamespace %>
{
public  partial interface <%= GetIDALInterfaceName() %>
{
<%= GetModelClassName() %> Add<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>);

void Delete<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>);

void Delete<%= GetModelClassName() %>ById(<%= GetPKPropertyType() %> <%= GetPKParamName() %>);

void Modify<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>);

IList<<%= GetModelClassName() %>> GetAll<%= MakePlural(GetModelClassName()) %>();

<%= GetModelClassName() %> Get<%= GetModelClassName() %>By<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>);

<%
if(TargetTable.ExtendedProperties.Contains("DefaultId"))
{
int defaultId = Convert.ToInt32(TargetTable.ExtendedProperties["DefaultId"].Value);
%>
<%-- public static Book GetDefaultBook() --%>
<%= GetModelClassName() %> GetDefault<%= GetModelClassName() %>();

<%
}
%>
}
}
<script runat="template">
///////////////////////////////////////////////////////////////
// CLASS NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetIDALInterfaceName()
{
return "I"+ GetModelClassName() +  DALClassNameSurfix;
}
public string GetDALClassName()
{
return  GetModelClassName() + DALClassNameSurfix;
}
public string GetModelMemberVarName()
{
return GetModelParamName();
}
public string GetModelParamName()
{
return MakeCamel(GetModelClassName());
}
public string GetModelClassName()
{
return  GetModelClassName(TargetTable);
}
public string GetModelClassName(TableSchema table)
{
string result;
if ( table.ExtendedProperties.Contains("ModelName") )
{
result = (string)table.ExtendedProperties["ModelName"].Value;
return MakePascal(result);
}
if (table.Name.EndsWith("s"))
{
//result = table.Name.Substring(0, table.Name.Length - 1);
result = MakeSingle(table.Name);
}
else
{
result = table.Name;
}
return MakePascal(result);
}
///////////////////////////////////////////////////////////////
// PRIMARY KEY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPKPropertyType()
{
return  GetPKType(TargetTable);
}
public string GetPKType()
{
return  GetPKType(TargetTable);
}
public string GetPKType(TableSchema TargetTable)
{
if (TargetTable.PrimaryKey != null)
{
if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpTypeFromDBFieldType(TargetTable.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 MyTables with a primary key.");
}
}
public string GetCSharpTypeFromDBFieldType(ColumnSchema column)
{
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;
}
}
}
///////////////////////////////////////////////////////////////
// PRIMARY KEY NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPKPropertyName()
{
return MakePascal(GetPKName());
}
public string GetPKMemberVarName()
{
return MakeCamel(GetPKName());
}
public string GetPKParamName()
{
return GetPKMemberVarName();
}
public string GetPKName()
{
return GetPKName(TargetTable);
}
public string GetPKName(TableSchema TargetTable)
{
if (TargetTable.PrimaryKey != null)
{
if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
{
return TargetTable.PrimaryKey.MemberColumns[0].Name;
}
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.");
}
}

///////////////////////////////////////////////////////////////
// FOREIGH KEY PROPERTY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKPropertyType(TableKeySchema key)
{
return MakePascal(GetFKPrimaryModelClassName(key));
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY ID NAMEs by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKForeignIdName(TableKeySchema key)
{
return key.ForeignKeyMemberColumns[0].Name;
}
public string GetFKPrimaryIdName(TableKeySchema key)
{
return key.PrimaryKeyMemberColumns[0].Name;
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY PROPERTY NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKMemberVarName(TableKeySchema key)
{
// return MakeCamel(GetFKName(key));
string result = GetFKForeignIdName(key);
if( result.ToLower().EndsWith("id") )
{
result = result.Substring(0, result.Length - 2);
}
return MakeCamel(result);
}
public string GetFKPropertyName(TableKeySchema key)
{
return MakePascal(GetFKMemberVarName(key));
}
public string GetFKPrimaryModelClassName(TableKeySchema key)
{
return GetModelClassName(key.PrimaryKeyTable);
}
//So dirty function! -- reviewed by shenbo
public string MakeCamel(string value)
{
return value.Substring(0, 1).ToLower() + value.Substring(1);
}
//I will be dirty too! -- coded by shenbo
public string MakePascal(string value)
{
return value.Substring(0, 1).ToUpper() + value.Substring(1);
}
public string MakePlural(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])y$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)$");
Regex plural3 = new Regex("(?<keep>[sxzh])$");
Regex plural4 = new Regex("(?<keep>[^sxzhy])$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}ies");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}s");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}es");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}s");
return name;
}
public string MakeSingle(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
Regex plural3 = new Regex("(?<keep>[sxzh])es$");
Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}y");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}");
return name;
}
public override string GetFileName()
{
return GetIDALInterfaceName() + ".cs";
}
public void PrintHeader()
{
Response.WriteLine("//============================================================");
Response.WriteLine("// Product name:   MyProject");
Response.WriteLine("// Version:    1.0");
Response.WriteLine("// Coded by:   ocean ");
Response.WriteLine("// Auto generated at:  {0}", DateTime.Now);
Response.WriteLine("//============================================================");
Response.WriteLine();
}
</script>


model 代码:

<%--
Name:MyOffice.Models
Author: ocean
Description: MyOffice IDAL templete
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a very simple business object." %>
<%@ Property Name="TargetTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="ModelsNamespace" Default="MyOffice.MODEL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<% PrintHeader(); %>
using System;
using System.Collections.Generic;
using System.Text;
namespace <%= ModelsNamespace %>
{

[Serializable()]
public class <%= GetModelClassName() %>
{

private <%= GetPKPropertyType() %> <%= GetPKMemberVarName() %>;
<%

if(TargetTable.ForeignKeys.Count > 0)
{
foreach (TableKeySchema key in TargetTable.ForeignKeys)
{
%>
private <%= GetFKPropertyType(key) %> <%= GetFKMemberVarName(key) %>;
<%
}
}

foreach (ColumnSchema column in TargetTable.NonKeyColumns)
{
if(GetMemberVariableDefaultValue(column) != "")
{
%>
private <%= GetPropertyType(column) %> <%= GetMemberVarName(column) %> = <%= GetMemberVariableDefaultValue(column) %>;
<%
}
else
{
%>
private <%= GetPropertyType(column) %> <%= GetMemberVarName(column) %>;
<%
}
}
%>

public <%= GetModelClassName() %>() { }

public <%= GetPKPropertyType() %> <%= GetPKPropertyName() %>
{
get { return this.<%= GetPKMemberVarName() %>; }
set { this.<%= GetPKMemberVarName() %> = value; }
}

<% if(TargetTable.ForeignKeys.Count > 0) %>
<% { %>

<% foreach (TableKeySchema key in TargetTable.ForeignKeys)%>
<% { %>

public <%= GetFKPropertyType(key) %> <%= GetFKPropertyName(key) %>
{
get { return this.<%= GetFKMemberVarName(key) %>; }
set { this.<%= GetFKMemberVarName(key) %> = value; }
}

<% } %>
<% } %>

<% foreach (ColumnSchema column in TargetTable.NonKeyColumns)  %>
<% { %>
public <%= GetPropertyType(column) %> <%= GetPropertyName(column) %>
{
get { return this.<%= GetMemberVarName(column) %>; }
set { this.<%= GetMemberVarName(column) %> = value; }
}

<% } %>
}
}
<script runat="template">
public string GetTableMapping()
{
string result;
result = string.Format("[DBTable({0})]", TargetTable.Name);
return  result;
}
public string GetModelClassName()
{
return  GetModelClassName(TargetTable);
}
public string GetModelClassName(TableSchema table)
{
string result;
if ( table.ExtendedProperties.Contains("ModelName") )
{
result = (string)table.ExtendedProperties["ModelName"].Value;
return MakePascal(result);
}
if (table.Name.EndsWith("s"))
{
//result = table.Name.Substring(0, table.Name.Length - 1);
result = MakeSingle(table.Name);
}
else
{
result = table.Name;
}
return MakePascal(result);
}
///////////////////////////////////////////////////////////////
// PRIMARY KEY MAPPING by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPKMapping()
{
string result;
result = string.Format("[DBField({0})]", TargetTable.PrimaryKey.MemberColumns[0].Name);
return  result;
}
///////////////////////////////////////////////////////////////
// PRIMARY KEY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPKPropertyType()
{
return  GetPKType(TargetTable);
}
public string GetPKType()
{
return  GetPKType(TargetTable);
}
public string GetPKType(TableSchema TargetTable)
{
if (TargetTable.PrimaryKey != null)
{
if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
{
return GetCSharpTypeFromDBFieldType(TargetTable.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 MyTables with a primary key.");
}
}
///////////////////////////////////////////////////////////////
// PRIMARY KEY NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPKPropertyName()
{
return MakePascal(GetPKName());
}
public string GetPKMemberVarName()
{
return MakeCamel(GetPKName());
}
public string GetPKName()
{
return GetPKName(TargetTable);
}
public string GetPKName(TableSchema TargetTable)
{
if (TargetTable.PrimaryKey != null)
{
if (TargetTable.PrimaryKey.MemberColumns.Count == 1)
{
return TargetTable.PrimaryKey.MemberColumns[0].Name;
}
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.");
}
}

///////////////////////////////////////////////////////////////
// PRIMARY KEY MAPPING by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKMapping(TableKeySchema key)
{
string result;
result = string.Format("[DBField({0})]", key.ForeignKeyMemberColumns[0].Name);
return  result;
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKPropertyType(TableKeySchema key)
{
return MakePascal(GetFKPrimaryModelClassName(key));
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY ID NAMEs by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKForeignIdName(TableKeySchema key)
{
return key.ForeignKeyMemberColumns[0].Name;
}
public string GetFKPrimaryIdName(TableKeySchema key)
{
return key.PrimaryKeyMemberColumns[0].Name;
}
///////////////////////////////////////////////////////////////
// FOREIGH KEY NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFKMemberVarName(TableKeySchema key)
{
// return MakeCamel(GetFKName(key));
string result = GetFKForeignIdName(key);
if( result.ToLower().EndsWith("id") )
{
result = result.Substring(0, result.Length - 2);
}
return MakeCamel(result);
}
public string GetFKPropertyName(TableKeySchema key)
{
return MakePascal(GetFKMemberVarName(key));
}
public string GetFKPrimaryModelClassName(TableKeySchema key)
{
return GetModelClassName(key.PrimaryKeyTable);
}

///////////////////////////////////////////////////////////////
// PROPERTY TYPE by Shen Bo
///////////////////////////////////////////////////////////////
public string GetPropertyType(ColumnSchema column)
{
return GetCSharpTypeFromDBFieldType(column);
}
public string GetMemberVarType(ColumnSchema column)
{
return GetPropertyType(column);
}
public string GetCSharpTypeFromDBFieldType(ColumnSchema column)
{
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;
}
}
}
///////////////////////////////////////////////////////////////
// PROPERTY NAME by Shen Bo
///////////////////////////////////////////////////////////////
public string GetMemberVarName(ColumnSchema column)
{
return MakeCamel(GetNameFromDBFieldName(column));
}
public string GetPropertyName(ColumnSchema column)
{
return MakePascal(GetNameFromDBFieldName(column));
}
public string GetNameFromDBFieldName(ColumnSchema column)
{
return column.Name;
}
///////////////////////////////////////////////////////////////
// COLUMN MAPPING by Shen Bo
///////////////////////////////////////////////////////////////
public string GetFieldMapping(ColumnSchema column)
{
string result;
result = string.Format("[DBField({0})]", column.Name);
return  result;
}

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 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;
}
}
}
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;
}
}
//So dirty function! -- reviewed by shenbo
public string MakeCamel(string value)
{
return value.Substring(0, 1).ToLower() + value.Substring(1);
}
//I will be dirty too! -- coded by shenbo
public string MakePascal(string value)
{
return value.Substring(0, 1).ToUpper() + value.Substring(1);
}
public string MakePlural(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])y$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)$");
Regex plural3 = new Regex("(?<keep>[sxzh])$");
Regex plural4 = new Regex("(?<keep>[^sxzhy])$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}ies");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}s");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}es");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}s");
return name;
}
public string MakeSingle(string name)
{
Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
Regex plural3 = new Regex("(?<keep>[sxzh])es$");
Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");
if(plural1.IsMatch(name))
return plural1.Replace(name, "${keep}y");
else if(plural2.IsMatch(name))
return plural2.Replace(name, "${keep}");
else if(plural3.IsMatch(name))
return plural3.Replace(name, "${keep}");
else if(plural4.IsMatch(name))
return plural4.Replace(name, "${keep}");
return name;
}
public override string GetFileName()
{
return this.GetModelClassName(this.TargetTable) + ".cs";
}
public void PrintHeader()
{
Response.WriteLine("//============================================================");
Response.WriteLine("// Producnt name:  MyProject");
Response.WriteLine("// Version:    1.0");
Response.WriteLine("// Coded by:   ocean ");
Response.WriteLine("// Auto generated at:  {0}", DateTime.Now);
Response.WriteLine("//============================================================");
Response.WriteLine();
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codesmith