CodeSmith .NET三层架构模板

Model模板文件:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a class including a special informational header" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="NameSpace" Type="String" Category="Context" Description="The namespace to use for this class" %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema" DeepLoad="True" Optional="False" Category="Context" Description="The name of the table to generate" %>
<%@ Property Name="Author" Type="String" Category="Context"  Description="Author" %>
<%@ Property Name="TablePrefix" Type="System.String" Default="T" Category="Context" Description="The prefix to remove from table names" %>
/*------------------------------------------------
// File Name:<%=ClearPrefix(TableName.Name) %>Info.cs
// File Description:<%=ClearPrefix(TableName.Name) %> DataBase Entity
// Author:<%=Author%>
// Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%>
//------------------------------------------------*/

using System;

namespace <%=NameSpace%>.Model
{
   /// 
   /// Description <%=TableName.Description %>
   /// 
   [Serializable]
   public class <%=ClearPrefix(TableName.Name) %>Info
   {
  		#region Private Protery
  		<%for(int i=0;i
  		private <%=CSharpType(TableName.Columns[i])%> _<%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%>;
  		<%}%>
  		#endregion
  
  		#region Constructor	
  		/// 
  		/// <%=ClearPrefix(TableName.Name) %> Constructor
  		/// 
    	public <%=ClearPrefix(TableName.Name) %>Info()
    	{
    	}
		
  		/// 
  		/// <%=ClearPrefix(TableName.Name) %> Constructor With Parameters
  		/// 
  		<%for(int i=0;i
  		///<%if(TableName.Columns[i].Description.Length>0){%><%=TableName.Columns[i].Name+"  "+TableName.Columns[i].Description%><%}else{%><%=TableName.Columns[i].Name%><%}%>
  		<%}%>
  		public <%=ClearPrefix(TableName.Name) %>Info(<%for(int i=0;i<%=CSharpType(TableName.Columns[i])%> <%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%><% if (i < TableName.Columns.Count - 1) { %>,<%}%><%}%>)
  		{
  		<%for(int i=0;i
   		this._<%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%> = <%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%>;
  		<%}%>
  		}
  		#endregion
    
  		#region Public Property
        <%for(int i=0;i
  		///
  		/// <%=TableName.Columns[i].Name%><%if(TableName.Columns[i].Description.Length>0){%>
  		/// <%=TableName.Columns[i].Description%><%}%>
  		///
 		public <%=CSharpType(TableName.Columns[i]).Trim()%> <%=TableName.Columns[i].Name%>
  		{
  		get { return _<%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%>; }
   		set {  _<%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%> = value; }
  		}
		<%}%>
  		#endregion  
   }
}

IDAL模板文件:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a class including a special informational header" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="Namespace" Type="String" Category="Context" Description="The namespace to use for this class" %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema" DeepLoad="True" Optional="False" Category="Context" Description="The name of the table to generate" %>
<%@ Property Name="Author" Type="String" Category="Context"  Description="Author" %>
<%@ Property Name="TablePrefix" Type="System.String" Default="T" Category="Context" Description="The prefix to remove from table names" %>
/*------------------------------------------------
// File Name:I<%=ClearPrefix(TableName.Name) %>.cs
// File Description:<%=ClearPrefix(TableName.Name) %> Interface
// Author:<%=Author%>
// Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%>
//------------------------------------------------*/

using System;
using System.Text;
using System.Collections.Generic;
using <%=Namespace%>.Model;

namespace <%=Namespace%>.IDAL
{
	/// 
    /// <%=ClearPrefix(TableName.Name) %> Interface
    /// 
	public interface I<%=ClearPrefix(TableName.Name) %>
	{
		/// 
        /// Add <%=ClearPrefix(TableName.Name) %>
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Model
        /// True or False
		bool Add(<%=ClearPrefix(TableName.Name) %>Info model);
		
		/// 
        /// Delete <%=ClearPrefix(TableName.Name) %> By ID
        /// 
        /// <%=ClearPrefix(TableName.Name) %> ID
        /// True or False
		bool DeleteByID(int id);
		
		/// 
        /// Delete <%=ClearPrefix(TableName.Name) %> By Name
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Name
        /// True or False
		bool DeleteByName(string name);
		
		/// 
        /// Update <%=ClearPrefix(TableName.Name) %>
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Model
        /// True or False
		bool Update(<%=ClearPrefix(TableName.Name) %>Info model);
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> By ID
        /// 
        /// <%=ClearPrefix(TableName.Name) %> ID
        /// <%=ClearPrefix(TableName.Name) %> Model
		<%=ClearPrefix(TableName.Name) %>Info Get<%=ClearPrefix(TableName.Name) %>InfoByID(int id);
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> By Name
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Name
        /// <%=ClearPrefix(TableName.Name) %> Model
		<%=ClearPrefix(TableName.Name) %>Info Get<%=ClearPrefix(TableName.Name) %>InfoByName(string name);
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> List
        /// 
        /// <%=ClearPrefix(TableName.Name) %> List
		IList<<%=ClearPrefix(TableName.Name) %>Info> Get<%=ClearPrefix(TableName.Name) %>ListByPage();
		
		/// 
        /// Check If <%=ClearPrefix(TableName.Name) %> Exist
        /// 
		/// <%=ClearPrefix(TableName.Name) %> Name
        /// True or False
		bool Is<%=ClearPrefix(TableName.Name) %>Exist(string name);
	}
}

SQLServerDAL模板文件:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a class including a special informational header" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="Namespace" Type="String" Category="Context" Description="The namespace to use for this class" %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema" DeepLoad="True" Optional="False" Category="Context" Description="The name of the table to generate" %>
<%@ Property Name="Author" Type="String" Category="Context"  Description="Author" %>
<%@ Property Name="TablePrefix" Type="System.String" Default="T" Category="Context" Description="The prefix to remove from table names" %>
/*------------------------------------------------
// File Name:<%=ClearPrefix(TableName.Name) %>.cs
// File Description:<%=ClearPrefix(TableName.Name) %> SQL Server DataBase Access
// Author:<%=Author%>
// Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%>
//------------------------------------------------*/

using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using <%=Namespace%>.Model;
using <%=Namespace%>.IDAL;

namespace <%=Namespace%>.DAL
{
	/// 
    /// <%=ClearPrefix(TableName.Name) %>
    /// 
	public class <%=ClearPrefix(TableName.Name) %>:I<%=ClearPrefix(TableName.Name) %>
	{
		public <%=ClearPrefix(TableName.Name) %>(){}
		
		#region <%=ClearPrefix(TableName.Name) %> Interface Implement
		/// 
        /// Add <%=ClearPrefix(TableName.Name) %>
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Model
        /// True or False
		public bool Add(<%=ClearPrefix(TableName.Name) %>Info model){
			SqlParameter[] param = new SqlParameter[<%=TableName.Columns.Count-1 %>];
        	<%for(int i=0;i
			param[<%=i %>] = new SqlParameter("@<%=TableName.Columns[i].Name%>",model.<%=TableName.Columns[i].Name%>);
  			<%}%>
			
        	try
			{
				SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure,"SP_Add<%=ClearPrefix(TableName.Name) %>", param);
				return true;
			}catch
			{
				return false;
			}
		}
		
		/// 
        /// Delete <%=ClearPrefix(TableName.Name) %> By ID
        /// 
        /// <%=ClearPrefix(TableName.Name) %> ID
        /// True or False
		public bool DeleteByID(int id){
			SqlParameter[] param = new SqlParameter[1];
			param[0] = new SqlParameter("@id", id);
			try
			{
				SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure,"SP_Del<%=ClearPrefix(TableName.Name) %>ByID", param);
				return true;
			}catch
			{
				return false;
			}
		}
		
		/// 
        /// Delete <%=ClearPrefix(TableName.Name) %> By Name
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Name
        /// True or False
		public bool DeleteByName(string name){
			SqlParameter[] param = new SqlParameter[1];
			param[0] = new SqlParameter("@name",name);
			try
			{
				SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure,"SP_Del<%=ClearPrefix(TableName.Name) %>ByName", param);
				return true;
			}catch
			{
				return false;
			}
		}
		
		/// 
        /// Update <%=ClearPrefix(TableName.Name) %>
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Model
        /// True or False
		public bool Update(<%=ClearPrefix(TableName.Name) %>Info model){
			SqlParameter[] param = new SqlParameter[<%=TableName.Columns.Count-1 %>];
        	<%for(int i=0;i
			param[<%=i %>] = new SqlParameter("@<%=TableName.Columns[i].Name%>",model.<%=TableName.Columns[i].Name%>);
  			<%}%>
        	try
			{
				SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure,"SP_Update<%=ClearPrefix(TableName.Name) %>", param);
				return true;
			}catch
			{
				return false;
			}
		}
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> By ID
        /// 
        /// <%=ClearPrefix(TableName.Name) %> ID
        /// <%=ClearPrefix(TableName.Name) %> Model
		public <%=ClearPrefix(TableName.Name) %>Info Get<%=ClearPrefix(TableName.Name) %>InfoByID(int id){
			SqlParameter[] param = new SqlParameter[1];
			param[0] = new SqlParameter("@id", id);
			<%=ClearPrefix(TableName.Name) %>Info model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure,"SP_Get<%=ClearPrefix(TableName.Name) %>ByID", param))
            {
                while (dr.Read())
                {
                    model = Get<%=ClearPrefix(TableName.Name) %>Model(dr);
                }
                return model;
            }
		}
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> By Name
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Name
        /// <%=ClearPrefix(TableName.Name) %> Model
		public <%=ClearPrefix(TableName.Name) %>Info Get<%=ClearPrefix(TableName.Name) %>InfoByName(string name){
			SqlParameter[] param = new SqlParameter[1];
			param[0] = new SqlParameter("@name", name);
			<%=ClearPrefix(TableName.Name) %>Info model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure,"SP_Get<%=ClearPrefix(TableName.Name) %>ByName", param))
            {
                while (dr.Read())
                {
                    model = Get<%=ClearPrefix(TableName.Name) %>Model(dr);
                }
                return model;
            }
		}
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> List
        /// 
        /// <%=ClearPrefix(TableName.Name) %> List
		public IList<<%=ClearPrefix(TableName.Name) %>Info> Get<%=ClearPrefix(TableName.Name) %>ListByPage(){
			using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "SP_Get<%=ClearPrefix(TableName.Name) %>ListByPage", null))
            {
                IList<<%=ClearPrefix(TableName.Name) %>Info> list = new List<<%=ClearPrefix(TableName.Name) %>Info>();
                while(dr.Read())
                {
                    list.Add(Get<%=ClearPrefix(TableName.Name) %>Model(dr));
                }
                return list;
            }
		}
		
		/// 
        /// Check If <%=ClearPrefix(TableName.Name) %> Exist
        /// 
		/// <%=ClearPrefix(TableName.Name) %> Name
        /// True or False
		public bool Is<%=ClearPrefix(TableName.Name) %>Exist(string name){
			SqlParameter[] param = new SqlParameter[1];
			param[0] = new SqlParameter("@name", name);
			return Convert.ToBoolean(SQLHelper.ExecuteScalar(CommandType.StoredProcedure,"SP_Is<%=ClearPrefix(TableName.Name) %>Exist", param));
		}
		#endregion
		
		#region Private Methods	
		private <%=ClearPrefix(TableName.Name) %>Info Get<%=ClearPrefix(TableName.Name) %>Model(SqlDataReader dr){
			<%=ClearPrefix(TableName.Name) %>Info model=new <%=ClearPrefix(TableName.Name) %>Info();
			<%for(int i=0;i
			model.<%=TableName.Columns[i].Name%>=SQLDataHelper.<%=CSharpType(TableName.Columns[i])%>(dr, "<%=TableName.Columns[i].Name%>");
  			<%}%>
			return model;
		}
		
		private IList<<%=ClearPrefix(TableName.Name) %>Info> Get<%=ClearPrefix(TableName.Name) %>List(SqlDataReader dr){
			IList<<%=ClearPrefix(TableName.Name) %>Info> list = new List<<%=ClearPrefix(TableName.Name) %>Info>();
            while (dr.Read())
            {
                list.Add(Get<%=ClearPrefix(TableName.Name) %>Model(dr));
            }
            return list;
		}
		#endregion
	}
}

BLL模板文件:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a class including a special informational header" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Text" %>
<%@ Property Name="Namespace" Type="String" Category="Context" Description="The namespace to use for this class" %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema" DeepLoad="True" Optional="False" Category="Context" Description="The name of the table to generate" %>
<%@ Property Name="Author" Type="String" Category="Context"  Description="Author" %>
<%@ Property Name="TablePrefix" Type="System.String" Default="T" Category="Context" Description="The prefix to remove from table names" %>
/*------------------------------------------------
// File Name:<%=ClearPrefix(TableName.Name) %>BLL.cs
// File Description:<%=ClearPrefix(TableName.Name) %> Business Logic
// Author:<%=Author%>
// Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%>
//------------------------------------------------*/

using System;
using System.Text;
using System.Collections.Generic;
using <%=Namespace%>.Model;
using <%=Namespace%>.IDAL;

namespace <%=Namespace%>.BLL
{
	/// 
    /// <%=ClearPrefix(TableName.Name) %>BLL
    /// 
	public class <%=ClearPrefix(TableName.Name) %>BLL
	{
		//Get <%=ClearPrefix(TableName.Name) %> Entity Class from Factory Layer
        private static readonly I<%=ClearPrefix(TableName.Name) %> dal = <%=Namespace%>.DALFactory.DataAccess.<%=ClearPrefix(TableName.Name) %>();
		
		/// 
        /// Add <%=ClearPrefix(TableName.Name) %>
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Model
        /// True or False
		public bool Add(<%=ClearPrefix(TableName.Name) %>Info model){
			return dal.Add(model);
		}
		
		/// 
        /// Delete <%=ClearPrefix(TableName.Name) %> By ID
        /// 
        /// <%=ClearPrefix(TableName.Name) %> ID
        /// True or False
		public bool DeleteByID(int id){
			return dal.DeleteByID(id);
		}
		
		/// 
        /// Delete <%=ClearPrefix(TableName.Name) %> By Name
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Name
        /// True or False
		public bool DeleteByName(string name){
			return dal.DeleteByName(name);
		}
		
		/// 
        /// Update <%=ClearPrefix(TableName.Name) %>
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Model
        /// True or False
		public bool Update(<%=ClearPrefix(TableName.Name) %>Info model){
			return dal.Update(model);
		}
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> By ID
        /// 
        /// <%=ClearPrefix(TableName.Name) %> ID
        /// <%=ClearPrefix(TableName.Name) %> Model
		public <%=ClearPrefix(TableName.Name) %>Info Get<%=ClearPrefix(TableName.Name) %>InfoByID(int id){
			return dal.Get<%=ClearPrefix(TableName.Name) %>InfoByID(id);
		}
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> By Name
        /// 
        /// <%=ClearPrefix(TableName.Name) %> Name
        /// <%=ClearPrefix(TableName.Name) %> Model
		public <%=ClearPrefix(TableName.Name) %>Info Get<%=ClearPrefix(TableName.Name) %>InfoByName(string name){
			return dal.Get<%=ClearPrefix(TableName.Name) %>InfoByName(name);
		}
		
		/// 
        /// Get <%=ClearPrefix(TableName.Name) %> List
        /// 
        /// <%=ClearPrefix(TableName.Name) %> List
		public IList<<%=ClearPrefix(TableName.Name) %>Info> Get<%=ClearPrefix(TableName.Name) %>ListByPage(){
			return dal.Get<%=ClearPrefix(TableName.Name) %>ListByPage();
		}
		
		/// 
        /// Check If <%=ClearPrefix(TableName.Name) %> Exist
        /// 
		/// <%=ClearPrefix(TableName.Name) %> Name
        /// True or False
		public bool Is<%=ClearPrefix(TableName.Name) %>Exist(string name){
			return dal.Is<%=ClearPrefix(TableName.Name) %>Exist(name);
		}
	}
}

 

你可能感兴趣的:(ASP.NET)