• CodeSmith业务逻辑层模板


    <%@ 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="MySchool.Models" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
    <%@ Property Name="DALNamespace" Default="MySchool.DAL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
    <%@ Property Name="BLLNamespace" Default="MySchool.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." %>

    <%@ 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 %>;

    namespace <%= BLLNamespace %>
    {

        public static partial class <%= GetBLLClassName() %>
        {
            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 <%= GetDALClassName() %>.Add<%= GetModelClassName() %>(<%= GetModelParamName() %>);
            }

            public static void Delete<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>)
            {
                <%= GetDALClassName() %>.Delete<%= GetModelClassName() %>(<%= GetModelParamName() %>);
            }

            public static void Delete<%= GetModelClassName() %>ById(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
            {
                <%= GetDALClassName() %>.Delete<%= GetModelClassName() %>ById(<%= GetPKParamName() %>);
            }

      public static void Modify<%= GetModelClassName() %>(<%= GetModelClassName() %> <%= GetModelParamName() %>)
            {
                <%= GetDALClassName() %>.Modify<%= GetModelClassName() %>(<%= GetModelParamName() %>);
            }
           
            public static IList<<%= GetModelClassName() %>> GetAll<%= MakePlural(GetModelClassName()) %>()
            {
                return <%= GetDALClassName() %>.GetAll<%= MakePlural(GetModelClassName()) %>();
            }

            public static <%= GetModelClassName() %> Get<%= GetModelClassName() %>By<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetPKParamName() %>)
            {
                return <%= GetDALClassName() %>.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 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.GetDALClassName() + ".cs";
    }

    public void PrintHeader()
    {
     Response.WriteLine("//============================================================");
     Response.WriteLine("// Producnt name:  BoBoARTS.CodeMad");
     Response.WriteLine("// Version:    1.0");
     Response.WriteLine("// Coded by:   Shen Bo (bo.shen@jb-aptech.com.cn)");
     Response.WriteLine("// Auto generated at:  {0}", DateTime.Now);
     Response.WriteLine("//============================================================");
     Response.WriteLine();
    }

    </script>

  • 相关阅读:
    Centos下一个server安装的版本号mysql
    android 玩愤怒的小鸟等游戏的时候全屏TP失败
    6.8 一般处理语言
    [AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem
    POJ1185:火炮(减少国家)
    教你如何下载音乐的网站只试镜
    实现js呼叫流行
    [Angular 2] Async Http
    [Typescript] Function defination
    [React] React Router: setRouteWillLeaveHook
  • 原文地址:https://www.cnblogs.com/juan/p/1423591.html
Copyright © 2020-2023  润新知