CodeSmith模板(生成数据访问层接口)

模板如下:

<%@ Template Language="C#" TargetLanguage="C#" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" %>
<%@ Property Name="ModelNameSpace" Type="System.String" Default="Model"%>
<%@ Property Name="IDALlNameSpace" Type="System.String" Default="IDAL"%>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using <%=ModelNameSpace%>;
namespace <%=IDALlNameSpace %>
{
   public interface I<%=GetClassName() %>rService
    {
        DBHelper db = new DBHelper();
        int Add<%=this.GetClassName()%>(<%=this.GetClassName()%> <%=this.ToCamel(this.GetClassName())%>);
                                                        
        int Update<%=this.GetClassName()%>(<%=this.GetClassName()%> <%=this.ToCamel(this.GetClassName())%>);
                                                        
        <%foreach(TableKeySchema key in this.SourceTable.ForeignKeys) {%>
        int Delete<%=this.GetClassName()%>By<%=GetFKBySuffix(key)%>(<%=GetFKMethodParameters(key)%>);
        <%} %>
                                                        
        <%foreach(IndexSchema index in this.SourceTable.Indexes){ %>
        <%=this.GetClassName()%> Get<%=this.GetClassName()%>By<%=GetUniqueIndexBySuffix(index)%>(<%=GetUniqueIndexMethodParameters(index)%>);
        <%} %>
                                                        
        <%foreach(TableKeySchema key in this.SourceTable.ForeignKeys) {%>
        List<<%=this.GetClassName()%>> Get<%=this.GetClassName()%>By<%=GetFKBySuffix(key)%>(<%=GetFKMethodParameters(key)%>);
        <%} %>
        List<<%=this.GetClassName()%>> Get<%=this.GetClassName()%>s();
                                                       
    }
}
<script runat="template">
public string GetUniqueIndexSQLParameters(IndexSchema index)
{
    string s="";
    foreach(MemberColumnSchema column in index.MemberColumns)
    {
        s+="\t\t\t\tnew SqlParameter(\"@"+ToCamel(column.Name)+"\","+ToCamel(column.Name)+"),\r\n";
    }
    if(s!="")
    {
        return s.Substring(0,s.Length-3).Substring(4);
    }
    return s;
}
public string GetUniqueIndexSQLWhereParameters(IndexSchema index)
{
    string s="";
    foreach(MemberColumnSchema column in index.MemberColumns)
    {
        s+=ToCamel(column.Name)+"=@"+ToCamel(column.Name)+" And ";
    }
    if(s!="")
    {
        return s.Substring(0,s.Length-4);
    }
    return s;
}
public string GetFKMethodParameters(TableKeySchema key)
{
    string s="";
    foreach(MemberColumnSchema column in key.ForeignKeyMemberColumns)
    {
        s+=GetCSDataType(column)+" "+ToCamel(column.Name)+",";
    }
    if(s!="")
    {
        return s.Substring(0,s.Length-1);
    }
    return s;
}
public string GetFKBySuffix(TableKeySchema key)
{
    string s="";
    foreach(MemberColumnSchema column in key.ForeignKeyMemberColumns)
    {
        s+=ToPascal(column.Name)+"And";
    }
    if(s!="")
    {
        return s.Substring(0,s.Length-3);
    }
    return s;
}
public string GetUniqueIndexMethodParameters(IndexSchema index)
{
    string s="";
    foreach(MemberColumnSchema column in index.MemberColumns)
    {
        s+=GetCSDataType(column)+" "+ToCamel(column.Name)+",";
    }
    if(s!="")
    {
        return s.Substring(0,s.Length-1);
    }
    return s;
}
public string GetUniqueIndexBySuffix(IndexSchema index)
{
    string s="";
    foreach(MemberColumnSchema column in index.MemberColumns)
    {
        s+=ToPascal(column.Name)+"And";
    }
    if(s!="")
    {
        return s.Substring(0,s.Length-3);
    }
    return s;
}
public string ToPascal(string s)
{
    return s.Substring(0,1).ToUpper()+s.Substring(1);
}
public string ToCamel(string s)
{
    return s.Substring(0,1).ToLower()+s.Substring(1);
}
public string GetClassName()
{
    string s=this.SourceTable.Name;
    if(s.EndsWith("s"))
    {
        return ToPascal(s.Substring(0,s.Length-1));
    }
    return ToPascal(s);
}
public static string GetCSDataType(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;
            }
    }
}
public override string GetFileName()
{
    return "I"+this.GetClassName()+"Service.cs";
}
</script>

这是生成一张表的数据访问层。

下面是为一个数据库批量生成的模板:

<%@ Template Language="C#" TargetLanguage="C#" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="SourceDB" Type="SchemaExplorer.DatabaseSchema" %>
<%@ Register Name="IDALTempateClass" Template="IDAL.cst" %>
<%
foreach (TableSchema table in this.SourceDB.Tables)
    {
         IDALTempateClass itc=new IDALTempateClass();
         itc.SourceTable=table;
         itc.RenderToFile("f:\\模板类IDAL\\"+itc.GetFileName(),true);
    }
%>
<script runat="template">
</script>

在为一个数据库批量生成时,一般都是一模一样的套路。

Ajax的姑娘,加油!

本文出自 “Ajax的姑娘” 博客,谢绝转载!

你可能感兴趣的:(数据访问层,codesmith)