CodeSmith生成Model、Dal、IDal、BLL
c# 模板都继承此类,调用c#方法。
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.ComponentModel.Design;
using CodeSmith.Engine;
using SchemaExplorer;
public class Model : CodeTemplate
{
public string GetCSharpVariableType(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 "int";
case DbType.Currency: return "decimal";
case DbType.Date: return "DataTime";
case DbType.DateTime: return "DateTime";
case DbType.Decimal: if (column.Size > 0) { return "decimal"; } else { return "int"; }
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 "string";
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";
}
return null;
}
public string GetCSharpVariableType(MemberColumnSchema 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 "int";
case DbType.Currency: return "decimal";
case DbType.Date: return "DataTime";
case DbType.DateTime: return "DateTime";
case DbType.Decimal: if (column.Size > 0) { return "decimal"; } else { return "int"; }
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 "string";
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";
}
return null;
}
public string GetDefaultValue(ColumnSchema column)
{
switch (column.DataType)
{
case DbType.AnsiString: return "String.Empty";
case DbType.AnsiStringFixedLength: return "String.Empty";
case DbType.Binary: return "new byte[]";
case DbType.Boolean: return "false";
case DbType.Byte: return "0";
case DbType.Currency: return "0";
case DbType.Date: return "DateTime.MinValue";
case DbType.DateTime: return "DateTime.MinValue";
case DbType.Decimal: return "0";
case DbType.Double: return "0";
case DbType.Guid: return "Guid.Empty";
case DbType.Int16: return "0";
case DbType.Int32: return "0";
case DbType.Int64: return "0";
case DbType.Object: return "";
case DbType.SByte: return "0";
case DbType.Single: return "0";
case DbType.String: return "String.Empty";
case DbType.StringFixedLength: return "String.Empty";
case DbType.Time: return "0";
case DbType.UInt16: return "0";
case DbType.UInt32: return "0";
case DbType.UInt64: return "0";
case DbType.VarNumeric: return "0";
default: return "";
}
}
public string GetParameters(SchemaExplorer.TableSchema SourceDataTable)
{
string pp = "";
foreach (ColumnSchema column in SourceDataTable.Columns)
{
pp += GetCSharpVariableType(column) + " " + column.Name.ToLower() + ",";
}
//鍘婚櫎鏈€鍚庣殑鈥滐紝鈥?
pp = pp.Remove(pp.Length - 1);
return pp;
}
public string InitialToUpper(string str)
{
str = str.ToLower();
str = InitialToUpper2(str);
return str;
}
public string InitialToUpper2(string str)
{
char first = char.ToUpper(str[0]);
str = str.Substring(1);
str = first + str;
return str;
}
#region DAL
public string BuildSelectSql(SchemaExplorer.TableSchema SourceDataTable)
{
string sql = "select ";
foreach (ColumnSchema column in SourceDataTable.Columns)
{
sql += column.Name + ",";
}
sql = sql.Remove(sql.Length - 1);
sql += " from " + SourceDataTable.Name;
return sql;
}
public string BuildSelectSqlByID(SchemaExplorer.TableSchema SourceDataTable)
{
string sql = BuildSelectSql(SourceDataTable);
if (SourceDataTable.PrimaryKey.MemberColumns.Count == 1)
{
sql += " where " + SourceDataTable.PrimaryKey.MemberColumns[0].Name + "=:" + SourceDataTable.PrimaryKey.MemberColumns[0].Name;
}
else
{
string where = "";
foreach (MemberColumnSchema key in SourceDataTable.PrimaryKey.MemberColumns)
{
where += key.Name + "=:" + key.Name;
}
sql += " where " + where;
}
return sql;
}
public string BuildCreateSql(SchemaExplorer.TableSchema SourceDataTable)
{
string sql = "insert into ";
sql += SourceDataTable.Name;
sql += "(";
foreach (ColumnSchema column in SourceDataTable.Columns)
{
sql += column.Name + "=:" + column.Name + ",";
}
sql = sql.Remove(sql.Length - 1);
sql += ")values (";
foreach (ColumnSchema column in SourceDataTable.Columns)
{
sql += ":" + column.Name + ",";
}
sql = sql.Remove(sql.Length - 1);
sql += ")";
return sql;
}
public string BuildUpdateSql(SchemaExplorer.TableSchema SourceDataTable)
{
string sql = "update ";
sql += SourceDataTable.Name;
sql += " set ";
foreach (ColumnSchema column in SourceDataTable.Columns)
{
if (!column.IsPrimaryKeyMember)
{
sql += column.Name + "=:" + column.Name + ",";
}
}
sql = sql.Remove(sql.Length - 1);
if (SourceDataTable.PrimaryKey.MemberColumns.Count == 1)
{
sql += " where " + SourceDataTable.PrimaryKey.MemberColumns[0].Name + "=:" + SourceDataTable.PrimaryKey.MemberColumns[0].Name;
}
else
{
string where = "";
foreach (MemberColumnSchema key in SourceDataTable.PrimaryKey.MemberColumns)
{
where += key.Name + "=:" + key.Name;
}
sql += " where " + where;
}
return sql;
}
public string BuildDeleteSql(SchemaExplorer.TableSchema SourceDataTable)
{
string sql = "delete from " + SourceDataTable.Name + " where ";
if (SourceDataTable.PrimaryKey.MemberColumns.Count == 1)
{
sql += " where " + SourceDataTable.PrimaryKey.MemberColumns[0].Name + "=:" + SourceDataTable.PrimaryKey.MemberColumns[0].Name;
}
else
{
string where = "";
foreach (MemberColumnSchema key in SourceDataTable.PrimaryKey.MemberColumns)
{
where += key.Name + "=:" + key.Name;
}
sql += " where " + where;
}
return sql;
}
#endregion
public string GetPrimaryKeyParameters(SchemaExplorer.TableSchema SourceDataTable)
{
string parsmstr = "";
foreach (MemberColumnSchema key in SourceDataTable.PrimaryKey.MemberColumns)
{
parsmstr += GetCSharpVariableType(key) + " " + key.Name.ToLower();
}
parsmstr = parsmstr.Remove(parsmstr.Length - 1);
return parsmstr;
}
public string GetInfoParameters(SchemaExplorer.TableSchema SourceDataTable)
{
string paramstr = "";
for (int i = 0; i < SourceDataTable.Columns.Count; i++)
{
string typeName = GetCSharpVariableType(SourceDataTable.Columns[i]);
paramstr += "reader." + GetTypeConvertMothed(typeName) + "(" + i + "),";
}
paramstr = paramstr.Remove(paramstr.Length - 1);
return paramstr;
}
public string GetTypeConvertMothed(string str)
{
switch (str)
{
case "int":
return "GetInt32Value";
default:
return "Get" + InitialToUpper2(str) + "Value";
}
}
}
Model
<%-- Name: Author: Description: --%> <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="Model.cs" Inherits="Model" Debug="False" CompilerVersion="v3.5" Description="Template description here." %> <%@ Property Name="ClassName" Type="System.String" Default="SomeValue" Optional="True" Category="Strings" Description="This is a sample string property." %> <%@ Property Category="1.Database" Name="SourceDataTable" Optional="False" Type="SchemaExplorer.TableSchema" Description="Database to create SQLite DDL from." %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Text" %> /* insert license info here */ using System; namespace Pub.Model { /// <summary> /// Generated by MyGeneration /// </summary> [Serializable] public class <%=ClassName%>Info { #region Private Members <%foreach (ColumnSchema column in SourceDataTable.Columns){%> private <%=GetCSharpVariableType(column) %> m_<%=column.Name.ToLower()%>; <%} %> #endregion #region Default ( Empty ) Class Constuctor /// <summary> /// default constructor /// </summary> public <%=ClassName%>Info() { <%foreach (ColumnSchema column in SourceDataTable.Columns){%> m_<%=column.Name.ToLower()%>=<%=GetDefaultValue(column)%>; <%} %> } #endregion // End of Default ( Empty ) Class Constuctor #region Full Constructor /// <summary> /// full constructor /// </summary> public <%=ClassName%>Info(<%=GetParameters(SourceDataTable)%>) { <%foreach (ColumnSchema column in SourceDataTable.Columns){%> m_<%=column.Name.ToLower()%>=<%=column.Name.ToLower() %>; <%} %> } #endregion // End Full Constructor #region Public Properties <%foreach (ColumnSchema column in SourceDataTable.Columns){%> /// <summary> /// <%=column.Description%> /// </summary> public <%=GetCSharpVariableType(column) %> <%=InitialToUpper(column.Name.ToLower())%> { set{m_<%=column.Name.ToLower()%>=value;} get{return m_<%=column.Name.ToLower()%>;} } <%} %> #endregion } } <script runat="template"> // My methods here. </script>
DAL
<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="Model.cs" Inherits="Model" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
<%@ Property Name="ClassName" Type="System.String" Default="SomeValue" Optional="True" Category="Strings" Description="This is a sample string property." %>
<%@ Property Category="1.Database" Name="SourceDataTable" Optional="False"
Type="SchemaExplorer.TableSchema"
Description="Database to create SQLite DDL from." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text" %>
using System;
using Pub.IDAL;
using Pub.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace Pub.OracleDAL
{
/// <summary>
/// Generated by MyGeneration
/// </summary>
public class <%=ClassName%>DAL : I<%=ClassName%>DAL
{
//Static constants
private const string SQL_SELECT_<%=ClassName.ToUpper()%>_BY_TID = "<%=BuildSelectSqlByID(SourceDataTable)%>";
private const string SQL_SELECT_<%=ClassName.ToUpper()%> = "<%=BuildSelectSql(SourceDataTable)%>";
private const string SQL_UPDATE_<%=ClassName.ToUpper()%> = "<%=BuildUpdateSql(SourceDataTable)%>";
private const string SQL_DELETE_<%=ClassName.ToUpper()%> = "<%=BuildDeleteSql(SourceDataTable)%>";
private const string SQL_INSERT_<%=ClassName.ToUpper()%> = "<%=BuildCreateSql(SourceDataTable)%>";
#region I<%=ClassName%>DAL
public void Insert(<%=ClassName%>Info info)
{
Database db = DBHelper.GetDataBase();
DbCommand cmd = db.GetSqlStringCommand(SQL_INSERT_<%=ClassName.ToUpper()%>);
<%foreach (ColumnSchema column in SourceDataTable.Columns){%>
db.AddInParameter(cmd, "<%=column.Name%>", <%=column.DataType%>, info.<%=InitialToUpper(column.Name) %>);
<%}%>
using (DbConnection conn = db.CreateConnection())
{
db.ExecuteNonQuery(cmd);
}
}
public void Update(<%=ClassName%>Info info)
{
Database db = DBHelper.GetDataBase();
DbCommand cmd = db.GetSqlStringCommand(SQL_UPDATE_<%=ClassName.ToUpper()%>);
<%foreach (ColumnSchema column in SourceDataTable.Columns){%>
db.AddInParameter(cmd, "<%=column.Name%>", <%=column.DataType%>, info.<%=InitialToUpper(column.Name) %>);
<%}%>
using (DbConnection conn = db.CreateConnection())
{
db.ExecuteNonQuery(cmd);
}
}
public void Delete(<%=GetPrimaryKeyParameters(SourceDataTable)%>)
{
Database db = DBHelper.GetDataBase();
DbCommand cmd = db.GetSqlStringCommand(SQL_DELETE_<%=ClassName.ToUpper()%>);
<%foreach (MemberColumnSchema key in SourceDataTable.PrimaryKey.MemberColumns){%>
db.AddInParameter(cmd, "<%=key.Name%>", <%=key.DataType%>, <%=key.Name %>);
<%}%>
using (DbConnection conn = db.CreateConnection())
{
db.ExecuteNonQuery(cmd);
}
}
public IList<<%=ClassName%>Info> Select()
{
List<TemplateInfo> infos = new List<TemplateInfo>();
Database db = DBHelper.GetDataBase();
DbCommand cmd = db.GetSqlStringCommand(SQL_SELECT_<%=ClassName.ToUpper()%>);
using (DbConnection conn = db.CreateConnection())
{
IDataReader reader = db.ExecuteReader(cmd);
while (reader.Read())
{
infos.Add(new TemplateInfo(<%=GetInfoParameters(SourceDataTable)%>));
}
}
return infos;
}
public <%=ClassName%>Info Select(<%=GetPrimaryKeyParameters(SourceDataTable)%>)
{
<%=ClassName%>Info info = null;
Database db = DBHelper.GetDataBase();
DbCommand cmd = db.GetSqlStringCommand(SQL_SELECT_<%=ClassName.ToUpper()%>_BY_TID);
db.AddInParameter(cmd, "TId", DbType.Int32, tid);
<%foreach (MemberColumnSchema key in SourceDataTable.PrimaryKey.MemberColumns){%>
db.AddInParameter(cmd, "<%=key.Name%>", <%=key.DataType%>, <%=key.Name %>);
<%}%>
using (DbConnection conn = db.CreateConnection())
{
IDataReader reader = db.ExecuteReader(cmd);
if (reader.Read())
{
info = new <%=ClassName%>Info(<%=GetInfoParameters(SourceDataTable)%>);
}
}
return info;
}
#endregion
}
}
IDAL
<%-- Name: Author: Description: --%> <%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="Model.cs" Inherits="Model" Debug="False" CompilerVersion="v3.5" Description="Template description here." %> <%@ Property Name="ClassName" Type="System.String" Default="SomeValue" Optional="True" Category="Strings" Description="This is a sample string property." %> <%@ Property Category="1.Database" Name="SourceDataTable" Optional="False" Type="SchemaExplorer.TableSchema" Description="Database to create SQLite DDL from." %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Text" %> using System; using Pub.Model; using System.Collections.Generic; namespace Pub.IDAL { /// <summary> /// Generated by MyGeneration /// </summary> public interface I<%=ClassName%>DAL { void Insert(<%=ClassName%>Info info); void Update(<%=ClassName%>Info info); void Delete(<%=GetPrimaryKeyParameters(SourceDataTable)%>); <%=ClassName%>Info Select(<%=GetPrimaryKeyParameters(SourceDataTable)%>); IList<\<%=ClassName%>Info> Select(); } }
BLL
<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="Model.cs" Inherits="Model" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
<%@ Property Name="ClassName" Type="System.String" Default="SomeValue" Optional="True" Category="Strings" Description="This is a sample string property." %>
<%@ Property Category="1.Database" Name="SourceDataTable" Optional="False"
Type="SchemaExplorer.TableSchema"
Description="Database to create SQLite DDL from." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pub.IDAL;
using Pub.DALFactory;
using Pub.Model;
namespace Pub.BLL
{
public class <%=ClassName%>BLL
{
private static readonly I<%=ClassName%>DAL dal = DataAccess.Create<%=ClassName%>DAL();
public IList<<%=ClassName%>Info> Get<%=ClassName%>s()
{
return dal.Select();
}
public <%=ClassName%>Info Get<%=ClassName%>(<%=GetPrimaryKeyParameters(SourceDataTable)%>)
{
return dal.Select(<%=GetPrimaryKeyParameters(SourceDataTable)%>);
}
public void Add<%=ClassName%>(<%=ClassName%>Info info)
{
dal.Insert(info);
}
public void Modify<%=ClassName%>(<%=ClassName%>Info info)
{
dal.Update(info);
}
public void Delete<%=ClassName%>(<%=GetPrimaryKeyParameters(SourceDataTable)%>)
{
dal.Delete(<%=GetPrimaryKeyParameters(SourceDataTable)%>);
}
}
}
MVCModel
<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="Model.cs" Inherits="Model" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>
<%@ Property Name="ClassName" Type="System.String" Default="SomeValue" Optional="True" Category="Strings" Description="This is a sample string property." %>
<%@ Property Name="NameSpace" Type="System.String" Default="ManageSite.Models" Optional="True" Category="Strings" Description="This is a sample string property." %>
<%@ Property Category="1.Database" Name="SourceDataTable" Optional="False"
Type="SchemaExplorer.TableSchema"
Description="Database to create SQLite DDL from." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text" %>
/*
insert license info here
*/
using System;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Pub.Model;
using Pub.BLL;
namespace <%=NameSpace%>
{
/// <summary>
/// Generated by MyGeneration
/// </summary>
[Serializable]
public class <%=ClassName%>Model
{
<%foreach (ColumnSchema column in SourceDataTable.Columns){%>
[DisplayName("<%=column.Description%>")]
public <%=GetCSharpVariableType(column) %> <%=InitialToUpper(column.Name.ToLower())%>
{
set;
get;
}
<%} %>
}
}
<script runat="template">
// My methods here.
</script>
posted on 2011-06-23 10:49 大飞-q4444zpf 阅读(741) 评论(0) 编辑 收藏 举报