CodeSmith生成三层代码之业务层(4)

 <%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" TargetLanguage="Text" Src="" Debug="False" Description="Template description here." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context"  Description="请选择数据表!" %>
<%@ Property Name="NameSpace" Type="System.String" Default="" Optional="False" Category="Description" Description="请输入命名空间" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
using System;
using System.Data;
using System.Collections.Generic;
using <%=NameSpace%>.DALFactory;
namespace <%=NameSpace%>.BLL
{
 /// <summary>
 /// 业务逻辑类<%=SourceTable.Name%>的摘要说明。
 /// </summary>
 public class <%=SourceTable.Name%>
 {
  private readonly I<%=SourceTable.Name%> dal = DataAccess.Create<%=SourceTable.Name%>();
  
  #region 数据访问部分
  public void Add(<%=NameSpace + ".Model." + SourceTable.Name%> model)
  {
   dal.Add(model);
  }
  
  public void Update(<%=NameSpace + ".Model." + SourceTable.Name%> model)
  {
   dal.Update(model);
  }
  
  public void Delete(<%=GetPrimaryKeyType(SourceTable) + " " + GetPrimaryKeyName(SourceTable)%>)
  {
   dal.Delete(<%=GetPrimaryKeyName(SourceTable)%>);
  }
  
  public List<<%=NameSpace + ".Model." + SourceTable.Name%>> GetList(string strWhere)
  {
   return dal.GetList(strWhere);
  }
  
  public DataSet GetData(string strWhere)
  {
   return dal.GetData(strWhere);
  }
  
  public <%=NameSpace + ".Model." + SourceTable.Name%> GetModel(<%=GetPrimaryKeyType(SourceTable) + " " + GetPrimaryKeyName(SourceTable)%>)
  {
   return dal.GetModel(<%=GetPrimaryKeyName(SourceTable)%>);
  }
     #endregion
 }
}
<script runat="template">
public string GetPrimaryKeyName(TableSchema table)
{
 string primaryKeyName = "";
 for(int i=0; i<table.Columns.Count; i++)
 {
  if(table.Columns[i].IsPrimaryKeyMember)
   primaryKeyName = table.Columns[i].Name;
 }
 
 return primaryKeyName.ToLower();
}

public string GetPrimaryKeyType(TableSchema table)
{
 int temp = 0;
 for(int i=0; i<table.Columns.Count; i++)
 {
  if(table.Columns[i].IsPrimaryKeyMember)
   temp = i;
 }
 
 string para = "";
 if (table.Columns[temp].Name.EndsWith("TypeCode"))
  return table.Columns[temp].Name;
  
 switch (table.Columns[temp].DataType)
 {
  case DbType.AnsiString:
   para = "string";
   break;
  case DbType.AnsiStringFixedLength:
   para = "string";
   break;
  case DbType.Binary:
   para = "byte[]";
   break;
  case DbType.Boolean:
   para = "bool";
   break;
  case DbType.Byte:
   para = "int";
   break;
  case DbType.Currency:
   para = "decimal";
   break;
  case DbType.Date:
   para = "DateTime";
   break;
  case DbType.DateTime:
   para = "DateTime";
   break;
  case DbType.Decimal:
   para = "decimal";
   break;
  case DbType.Double:
   para = "double";
   break;
  case DbType.Guid:
   para = "Guid";
   break;
  case DbType.Int16:
   para = "short";
   break;
  case DbType.Int32:
   para = "int";
   break;
  case DbType.Int64:
   para = "long";
   break;
  case DbType.Object:
   para = "object";
   break;
  case DbType.SByte:
   para = "sbyte";
   break;
  case DbType.Single:
   para = "float";
   break;
  case DbType.String:
   para = "string";
   break;
  case DbType.StringFixedLength:
   para = "string";
   break;
  case DbType.Time:
   para = "TimeSpan";
   break;
  case DbType.UInt16:
   para = "ushort";
   break;
  case DbType.UInt32:
   para = "uint";
   break;
  case DbType.UInt64:
   para = "ulong";
   break;
  case DbType.VarNumeric:
   para = "decimal";
   break;
  default:
   para = "__UNKNOWN__" + table.Columns[temp].NativeType;
   break;
 }
  
 return para.ToLower();
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值