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>

你可能感兴趣的:(code)