ASP.NET三层架构源码(CodeSmith版)之十二:View-StoreProcedure(存储过程)

动软代码生成器生成的ASP.NET三层架构代码比较规范,是学习ASP.NET的好例子

此三层架构改造自动软的工厂模式模板,使用CodeSmith进行重写,以方便大家修改模板文件

以下是针对视图生成操作存储过程的源码:

<%@ CodeTemplate Inherits="CodeTemplate" language="C#" TargetLanguage="Text" Description="NetTiers main template."Debug="True" ResponseEncoding="UTF-8"%>
<%-- 加载访问数据库的组件SchemaExplorer,并声明其使用的命名空间 --%>
<%@ Assembly Name="SchemaExplorer" %>  
<%@ Import Namespace="SchemaExplorer" %>  
<%@ Import Namespace="System.Text" %> 
<%@ Assembly Name="System.Data" %>

<%@ Property Name="Author" Type="String" Category="Context"  Description="Author" Default="chenr"%>  
<%@ Property Name="bGenOne" Type="Boolean"  DeepLoad="True" Optional="false" Category="Context" Description="Generate One View SP Or All" %>

<%-- 添加源数据库属性 --%>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"  DeepLoad="True" Optional="False" Category="01. Required" Description="Database" %>
<%@ Property Name="vName" Type="ViewSchema"  DeepLoad="True" Optional="True" Category="01. Required" Description="ViewName" %>

<% 

ViewSchema ViewName;

for (int i=0; i<this.SourceDatabase.Views.Count; i++)    
{    
    if (bGenOne)
    {
        ViewName = vName;
    }
    else
    {
        ViewName = this.SourceDatabase.Views[i];
    }
%>
-- ===========================================================
-- ViewName:    <%=ViewName.Name%>
-- Author:       <%=Author%> 
-- Create Time:  <%=DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%>
-- ===========================================================

if exists (select * from dbo.sysobjects where id = object_id(N'[<%=ViewName.Name%>_GetMaxId]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [<%=ViewName.Name%>_GetMaxId]
GO
 
<% PrintTitle("是否已经存在","",""); %>
CREATE PROCEDURE <%=ViewName.Name%>_Exists
@where varchar(100)
AS
	DECLARE @TempID int
	Exec('SELECT @TempID = count(1) FROM [<%=ViewName.Name%>] '+@where)
	IF @TempID = 0
		RETURN 0
	ELSE
		RETURN 1

GO

<% PrintTitle("查询记录信息","",""); %>
CREATE PROCEDURE <%=ViewName.Name%>_GetList
 AS 
	SELECT <%= GetAllRows(ViewName) %>
	 FROM [<%=ViewName.Name%>]

GO
<%
    if (bGenOne)
    {
        break;
    }
}%>

<script runat="template">
public void PrintTitle(string purpose, string name, string desc)
{
	Response.WriteLine("-----------------------------------------");
	Response.WriteLine("-- Purpose: \t"+purpose);
	Response.WriteLine("-- Name: \t\t"+name);
	Response.WriteLine("-- Description: "+desc);
	Response.WriteLine("-- Modify Time: ", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
	Response.WriteLine("-----------------------------------------");	
}

public string GetAllRows(ViewSchema ViewName)
{
    string strReturn = "";
    
    for (int i=0; i<ViewName.Columns.Count; i++) 
    {
        strReturn += ViewName.Columns[i].Name+",";
    }
    
    return strReturn.TrimEnd(',');
}


</script>

 


Comments are closed.