CodeSmith 创建Ado.Net自定义模版(四)
接上一篇: CodeSmith 创建Ado.Net自定义模版(三)
编写业务逻辑层的模版:Step4_BLL.cst
<%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Description="实体类" %>
<%@ Property Name="NameSpace" Type="System.String" Default="Service" Category="Property" Description="命名空间" %>
<%@ Property Name="ModelNameSpace" Type="System.String" Default="Model" Category="String" Description="实体层的命名空间" %>
<%@ Property Name="DALNameSpace" Type="System.String" Default="Service" Category="String" Description="数据访问层的命名空间" %>
<%@ Property Name="Author" Type="System.String" Default="Wilson" Category="Property" Description="作者名" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="True" Category="db" Description="表映射文件" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<script runat="template">
///<summary>
///把数据库类型转化为C#类型
///</summary>
public string DataType2CSharpType(System.Data.DbType dbType)
{
switch (dbType)
{
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.DateTime2:
return "DateTime";
case DbType.DateTimeOffset:
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 "DateTime";
case DbType.UInt16:
return "ushort";
case DbType.UInt32:
return "uint";
case DbType.UInt64:
return "ulong";
case DbType.VarNumeric:
return "decimal";
case DbType.Xml:
return "string";
default:
return "object";
}
}
///<summary>
/// 字符串转化为小写
///</summary>
public string ToLowercase(string str)
{
str = str.ToLower();
return str;
}
///<summary>
/// 泛型
///</summary>
public string ToGeneric(string str)
{
str = "<" + str + ">";
return str;
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using <%=DALNameSpace%>;
using <%=ModelNameSpace%>;
namespace <%=NameSpace%>
{
///<summary>
///<%=SourceTable.Name%>业务逻辑
/// 创建人:<%=Author%>
/// 创建时间:<%=DateTime.Now.ToShortDateString() %>
///</summary>
public class <%=SourceTable.Name%>Manager
{
<% if(SourceTable.HasPrimaryKey) %>
<%{%>
#region##通过主键ID得到<%=SourceTable.Name%>
///<summary>
/// 功能:通过ID得到<%=SourceTable.Name%>
/// 创建人: <%=Author%>
/// 创建时间:<%=DateTime.Now.ToShortDateString() %>
///</summary>
///<param name="">主键ID</param>
///<returns></returns>
public <%=SourceTable.Name%> Get<%=SourceTable.Name%>ById(<%=DataType2CSharpType(SourceTable.PrimaryKey.MemberColumns[0].DataType)%> <%=ToLowercase(SourceTable.PrimaryKey.MemberColumns[0].Name)%>)
{
using (<%=SourceTable.Name%>Service ws = new <%=SourceTable.Name%>Service())
{
return ms.Get<%=SourceTable.Name%>ById(<%=ToLowercase(SourceTable.PrimaryKey.MemberColumns[0].Name)%>);
}
}
#endregion
#region##通过主键ID删除<%=SourceTable.Name%>
///<summary>
/// 功能:通过主键ID删除<%=SourceTable.Name%>
/// 创建人: <%=Author%>
/// 创建时间:<%=DateTime.Now.ToShortDateString() %>
///</summary>
///<param name="">主键ID</param>
///<returns></returns>
public int DeleteById(<%=DataType2CSharpType(SourceTable.PrimaryKey.MemberColumns[0].DataType)%> <%=ToLowercase(SourceTable.PrimaryKey.MemberColumns[0].Name)%>)
{
using (<%=SourceTable.Name%>Service ws = new <%=SourceTable.Name%>Service())
{
return ws.DeleteById(<%=ToLowercase(SourceTable.PrimaryKey.MemberColumns[0].Name)%>);
}
}
#endregion
<%}%>
#region##添加<%=SourceTable.Name%>
///<summary>
/// 功能:添加<%=SourceTable.Name%>
/// 创建人: <%=Author%>
/// 创建时间:<%=DateTime.Now.ToShortDateString() %>
///</summary>
///<param name=""></param>
///<returns></returns>
public int Add<%=SourceTable.Name%>(<%=SourceTable.Name%> <%=ToLowercase(SourceTable.Name)%>)
{
using (<%=SourceTable.Name%>Service ws = new <%=SourceTable.Name%>Service())
{
return ws.Add<%=SourceTable.Name%>(<%=ToLowercase(SourceTable.Name)%>);
}
}
#endregion
#region##得到<%=SourceTable.Name%>集合
///<summary>
/// 功能:得到<%=SourceTable.Name%>集合
/// 创建人: <%=Author%>
/// 创建时间:<%=DateTime.Now.ToShortDateString() %>
///</summary>
///<returns></returns>
public IList<%=ToGeneric(SourceTable.Name)%> GetList()
{
using (<%=SourceTable.Name%>Service ws = new <%=SourceTable.Name%>Service())
{
return ws.GetList();
}
}
#endregion
#region##修改<%=SourceTable.Name%>
///<summary>
/// 功能:修改<%=SourceTable.Name%>
/// 创建人: <%=Author%>
/// 创建时间:<%=DateTime.Now.ToShortDateString() %>
///</summary>
///<param name=""></param>
///<returns></returns>
public int Mod<%=SourceTable.Name%>(<%=SourceTable.Name%> <%=ToLowercase(SourceTable.Name)%>)
{
using (<%=SourceTable.Name%>Service ws = new <%=SourceTable.Name%>Service())
{
return ws.Mod<%=SourceTable.Name%>(<%=ToLowercase(SourceTable.Name)%>);
}
}
#endregion
#region##修改<%=SourceTable.Name%>
///<summary>
/// 修改<%=SourceTable.Name%>
/// 创建人: <%=Author%>
/// 创建时间:<%=DateTime.Now.ToShortDateString() %>
///</summary>
///<param name=""></param>
///<returns></returns>
public int SelectCount()
{
using (<%=SourceTable.Name%>Service ws = new <%=SourceTable.Name%>Service())
{
return ws.SelectCount();
}
}
#endregion
}
}
总结:这套模版分四步,这样就完成了一个三层建构的网站的大体部分,其它部分可以自行扩充,比如CodeSmith直接生成文件夹目录及文件夹到本地,或是中间要加入接口层等。
要写创建文件夹,及文件,就是一个IO操作,我这里就不写了。。
如果有要扩展的在头部引用
<%@ Import Namespace="System.IO" %>
把方法写在<script runat="template">标签中
比如:写一个方法
public CreatDir()
{
System.IO.DirectoryInfo di = new DirectoryInfo(OutputDirectory);
di.CreateSubdirectory("Domain");
....
}
相关篇张:
源码下载:http://download.csdn.net/source/3535328
源码下载二:https://files.cnblogs.com/zhongweiv/AdoTemp.rar