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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值