基于 Microsoft .NET Pet Shop 4.0 框架模板

初地贵地,希望能结交各位英雄豪杰!

发一个陈年作品,的确老土没什么技术。但如果用Microsoft .NET Pet Shop 开发的朋友还是很实用,有Microsoft .NET Pet Shop 有少少改进,只要在重用方面!


Model数据模型

<%-- 
Name:Model数据模型模板 
Author: yixiong.chou 
Description: 基于 Microsoft .NET Pet Shop 
4.0 框架的Model数据模型模板 
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="" Debug="True" Description="Template description here." %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema"  Optional="True" Category="Strings" Description="date" %>
<%@ Property Name="Namespace" Type="System.String" Default="com." Category="Object" Description="The class namespace that the mapping file should use" %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<script runat="template">
public string GetSqlParameterStatement(ColumnSchema column)
{
    
string param = "private";
    
switch(column.DataType)
    
{
        
       
case DbType.Int32:
            
{
            param 
+=" int "+column.Name;
            
break;
            }

        
default:
        
{
             
if (column.Size > 0)
             
{
                param 
+= "(" + column.Size + ")";

             }

            
break;

        }

    }

    
return param;

}


public string GetParameter(){
     
int i=0;
     
string param="";
     
foreach(ColumnSchema cs in TableName.Columns)
        
{
            param
+=CSharpAlias[cs.SystemType.FullName]+" ";
            param
+=CamelCase(cs.Name);
            
if (i < TableName.Columns.Count-1)
              param
+=",";
            i
++;
        }

        
return param;
    }

</script>
using System;
using System.Collections.Generic;
using System.Text;

namespace <%= Namespace %>.Model
{   
    [Serializable]
    
public class <%=TableName.Name%>Info
    
{   
        
Member Variables
        
        
        
Constructors

        
Public Properties
   }

}

<!-- #include file="inc.inc" -->

 

<%-- 
Name:IDAL 数据接口模板
Author: yixiong.chou 
Description: 基于 Microsoft .NET Pet Shop 
4.0 框架的IDAL 数据接口模板 
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema"  Optional="True" Category="Strings" Description="" %>
<%@ Property Name="Namespace" Type="System.String" Default="com" Category="Object" Description="The class namespace that the mapping file should use" %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System" %>
<%@ Import Namespace="System" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
using System;
using System.Collections.Generic;
using System.Text;
using <%= Namespace %>.Model;
namespace <%= Namespace %>.IDAL
{
    
public interface I<%=TableName.Name%>
    
{
     
<%string nameToLower=TableName.Name.Substring(0,1).ToLower()+TableName.Name.Substring(1);%>
        
bool Insert(<%=TableName.Name%>Info <%=nameToLower%>Info);
        
bool Edit(<%=TableName.Name%>Info <%=nameToLower%>Info);
        
bool Delete(<%=CSharpType(TableName.PrimaryKey.MemberColumns[0])%> <%=CamelCase(TableName.PrimaryKey.MemberColumns[0].Name)%>);
        IList
<<%=TableName.Name%>Info> Get<%=TableName.Name%>s();
        
<%=TableName.Name%>Info Get<%=TableName.Name%>ById(int id);
        
    }

}

<!-- #include file="inc.inc" -->

 

<%-- 
Name:DAL 数据层模板
Author: yixiong.chou 
Description: 基于 Microsoft .NET Pet Shop 
4.0 框架的DAL 数据层模板 
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="" Debug="true" Description="Template description here." %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema"  Optional="True" Category="Strings" Description="????." %>
<%@ Property Name="Namespace" Type="System.String" Default="SQLConnString" Category="Object" Description="The class namespace that the mapping file should use" %>
<%@ Property Name="SQLConnString" Type="System.String" Default="com" Category="Object" Description="" %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System" %>
<%@ Import Namespace="System" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>

<script runat="template">
  
public string getConstants(string param){
     
      
string temp="PARAM";
      
for(int i=0;i<param.Length;i++){
        
if(Char.IsUpper(param,i)){
            temp
+="_";
            temp
+=param.Substring(i,1);
            }

        
else{
            temp
+=param.Substring(i,1);
            }

        }

       
       
return temp.ToUpper();
    }

    
   
public string getDataType(ColumnSchema column){
    
string type="SqlDbType.";
    
    
switch(column.DataType)
    
{
       
case DbType.DateTime:
            
{
            type 
+="DateTime";
            
break;
            }

        
case DbType.AnsiString:
            
{
            type 
+="VarChar";
            
break;
            }

        
default:
            
{
                type 
+=column.NativeType.ToString();
            
//type +=column.NativeType.Substring(0,1).ToUpper()+column.NativeType.Substring(1);
            break;
            }

    }

    
return type;
   }


    
public string GetDefault(DataObjectBase column)
    
{
        
switch (column.NativeType)
        
{
            
case "bit"return "false";
            
case "datetime"return "new DateTime()";
            
case "decimal"return "0.00"
            
case "int"return "0";
            
case "ntext"return "string.Empty";
            
case "varchar"return "string.Empty";
            
case "nvarchar"return "string.Empty";
            
case "text"return "string.Empty";
            
defaultreturn "0";
            
        }

    }


    
public string GetSqlDbType(DataObjectBase column)
    
{

        
switch (column.NativeType)

        
{

        
case "bigint"return "BigInt";

        
case "binary"return "Binary";

        
case "bit"return "Bit";

        
case "char"return "Char";

        
case "datetime"return "DateTime";

        
case "decimal"return "Decimal";

        
case "float"return "Float";

        
case "image"return "Image";

        
case "int"return "Int";

        
case "money"return "Money";

        
case "nchar"return "NChar";

        
case "ntext"return "NText";

        
case "numeric"return "Decimal";

        
case "nvarchar"return "NVarChar";

        
case "real"return "Real";

        
case "smalldatetime"return "SmallDateTime";

        
case "smallint"return "SmallInt";

        
case "smallmoney"return "SmallMoney";

        
case "sql_variant"return "Variant";

        
case "sysname"return "NChar";

        
case "text"return "Text";

        
case "timestamp"return "Timestamp";

        
case "tinyint"return "TinyInt";

        
case "uniqueidentifier"return "UniqueIdentifier";

        
case "varbinary"return "VarBinary";

        
case "varchar"return "VarChar";

        
defaultreturn "__UNKNOWN__" + column.NativeType;

        }


    }



   
public string FormatString(string str){
    
return str.Substring(7);
    }

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

namespace <%= Namespace %>.DAL
{
<%int i=0;%>
    
public class <%=TableName.Name%> : I<%=TableName.Name%> 
    
{
        
//
        private const string SQLSP_INSERT_<%=TableName.Name.ToUpper()%>="insert_<%=TableName.Name.ToLower()%>";
        
private const string SQLSP_EDIT_<%=TableName.Name.ToUpper()%>="edit_<%=TableName.Name.ToLower()%>";
        
private const string SQLSP_DELETE_<%=TableName.Name.ToUpper()%>="delete_<%=TableName.Name.ToLower()%>";
        
private const string SQLSP_GET_<%=TableName.Name.ToUpper()%>S="get_<%=TableName.Name.ToLower()%>s";
        
private const string SQLSP_GET_<%=TableName.Name.ToUpper()%>_BY_ID="get_<%=TableName.Name.ToLower()%>_by_id";
        
        
        
//
      <%
         
foreach(ColumnSchema cs in TableName.Columns)
         
{%>
        
<%string name=PascalCase(cs.Name);%>
        
private const string <%=getConstants(name)%>="@<%=cs.Name%>";
         
<%}
%>
      
      
      
<%string nameToLower=TableName.Name.Substring(0,1).ToLower()+TableName.Name.Substring(1);%>
        
public bool Insert(<%=TableName.Name%>Info <%=nameToLower%>Info)
        
{
            SqlParameter[] Params
=Set<%=TableName.Name%>Parameters(<%=nameToLower%>Info);
            
int result = SqlHelper.ExecuteNonQuery(SqlHelper.<%=SQLConnString%>, CommandType.StoredProcedure, SQLSP_INSERT_<%=TableName.Name.ToUpper()%>, Params);
            
if (result == 0return false;
            
return true;
        }

    
        
public bool Edit(<%=TableName.Name%>Info <%=nameToLower%>Info)
        
{
            SqlParameter[] Params
=Set<%=TableName.Name%>Parameters(<%=nameToLower%>Info);
            
int result = SqlHelper.ExecuteNonQuery(SqlHelper.<%=SQLConnString%>, CommandType.StoredProcedure, SQLSP_EDIT_<%=TableName.Name.ToUpper()%>, Params);
            
if (result == 0return false;
            
return true;
        
        }

    
        
/// <summary>
        
/// Delete From 
        
/// </summary>

        public bool Delete(<%=CSharpType(TableName.PrimaryKey.MemberColumns[0])%> <%=TableName.PrimaryKey.MemberColumns[0].Name%>)
        
{             
            SqlParameter param 
= new SqlParameter(<%=getConstants(PascalCase(TableName.PrimaryKey.MemberColumns[0].Name))%>, SqlDbType.<%=GetSqlDbType(TableName.PrimaryKey.MemberColumns[0])%>,<%=TableName.PrimaryKey.MemberColumns[0].Size%>);
            param.Value 
= <%=TableName.PrimaryKey.MemberColumns[0].Name%>;
            
int result=SqlHelper.ExecuteNonQuery(SqlHelper.<%=SQLConnString%>, CommandType.StoredProcedure, SQLSP_DELETE_<%=TableName.Name.ToUpper()%>, param);
            
if (result == 0return false;
            
return true;
        }

        
        
public <%=TableName.Name%>Info Get<%=TableName.Name%>ById(<%=CSharpType(TableName.PrimaryKey.MemberColumns[0])%> id)
        
{
            SqlParameter[] parms 
= new SqlParameter[] {new SqlParameter(<%=getConstants(PascalCase(TableName.PrimaryKey.MemberColumns[0].Name))%>,SqlDbType.<%=GetSqlDbType(TableName.PrimaryKey.MemberColumns[0])%><%=TableName.PrimaryKey.MemberColumns[0].Size%>)};
            parms[
0].Value=id;
            
return reads(SQLSP_GET_<%=TableName.Name.ToUpper()%>_BY_ID,parms)[0];
        }

        
        
public IList< <%=TableName.Name%>Info> Get<%=TableName.Name%>s()
        
{
            
return reads(SQLSP_GET_<%=TableName.Name.ToUpper()%>S,null);
        }

    
        
private static SqlParameter[] Set<%=TableName.Name%>Parameters(<%=TableName.Name%>Info <%=nameToLower%>Info)
        
{
            
// Get each commands parameter arrays
            SqlParameter[] Params = Get<%=TableName.Name%>Parameters();
            
// Set up the parameters
             <%
            i
=0;
            
foreach(ColumnSchema cs in TableName.Columns){%>
              
<%string value=cs.Name.Substring(0,1).ToUpper()+cs.Name.Substring(1);%>
            Params[
<%=i%>].Value =<%=nameToLower%>Info.<%=value%>;
             
<%
            i
++;
            }
%>
            
return Params;
        }

    
    
        
private IList< <%=TableName.Name%>Info> reads(string sp,SqlParameter[] param)
        
{
            IList
< <%=TableName.Name%>Info> <%=nameToLower%>Infos = new List<<%=TableName.Name%>Info>();
            
using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.<%=SQLConnString%>, CommandType.StoredProcedure, sp, param))
            
{
                
while (reader.Read())
                
{
                    
<%=TableName.Name%>Info <%=nameToLower%>Info = Reader(reader);
                    
<%=nameToLower%>Infos.Add(<%=nameToLower%>Info);
                }

            }

            
return <%=nameToLower%>Infos;
        }

    
    
        
/// <summary>
        
/// Internal function to get cached parameters
        
/// </summary>

        private static SqlParameter[] Get<%=TableName.Name%>Parameters()
        
{
            SqlParameter[] parms 
= SqlHelper.GetCachedParameters(SQLSP_INSERT_<%=TableName.Name.ToUpper()%>);
            
if (parms == null{
                parms 
= new SqlParameter[] {
                
<%
                i
=0;
                
foreach(ColumnSchema cs in TableName.Columns){%>
                
<%string name=cs.Name.Substring(0,1).ToUpper()+cs.Name.Substring(1);%>
                
new SqlParameter(<%=getConstants(name)%>,SqlDbType.<%=GetSqlDbType(cs)%>,<%=cs.Size%>)<% if (i < TableName.Columns.Count - 1%>,<%}%>
                
<%}
%>
            }
;
            SqlHelper.CacheParameters(SQLSP_INSERT_
<%=TableName.Name.ToUpper()%>, parms);
            }


        
return parms;
        }

    
        
/// <summary>
        
/// Internal function to Read to a row data form object of SqlDataReader 
        
/// </summary>

        private <%=TableName.Name%>Info Reader(SqlDataReader reader)
        
{
            
<%foreach(ColumnSchema cs in TableName.Columns){%>
            
<%string value=cs.Name.Substring(0,1).ToLower()+cs.Name.Substring(1);%>
            
<%=CSharpAlias[cs.SystemType.FullName]%> <%=value%>=<%=GetDefault(cs)%>;
            
if(reader.GetValue(<%=i%>)!=DBNull.Value)
            
<%=value%>=reader.Get<%=FormatString(cs.SystemType.ToString())%>(<%=i%>);
            
<%
            i
++;
            }
%>
            
<%=TableName.Name%>Info <%=TableName.Name.ToString().ToLower()%>=new <%=TableName.Name%>Info( 
            
<%
            i
=0;
            
foreach(ColumnSchema cs in TableName.Columns)
            
{
                Response.Write(cs.Name.Substring(
0,1).ToLower()+cs.Name.Substring(1));
                
if (i < TableName.Columns.Count - 1)
                Response.Write(
",");
                i
++;
            }
%>);
             
return <%=TableName.Name.ToString().ToLower()%>;
        }
 
    }

}

    
<!-- #include file="inc.inc" -->


 Bll业务层

<%-- 
Name:BLL业务层模板
Author: yixiong.chou 
Description: 基于 Microsoft .NET Pet Shop 
4.0 框架的BLL业务层模板
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Property Name="TableName" Type="SchemaExplorer.TableSchema"  Optional="True" Category="Strings" Description="" %>
<%@ Property Name="Namespace" Type="System.String" Default="com" Category="Strings" Description="The class namespace that the mapping file should use" %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System" %>
<%@ Import Namespace="System" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
using System;
using System.Collections.Generic;
using System.Text;
using  <%= Namespace %>.IDAL;
using  <%= Namespace %>.Model;
using  <%= Namespace %>.DALFactory;
<%string name=PascalCase(TableName.Name);%>
namespace <%= Namespace %>.BLL
{
    
public class <%=TableName.Name%>
    
{
        
private static readonly I<%=name%> <%=CamelCase(name)%>= DataAccess.Create<%=name%>();


        
public bool Insert(<%=TableName.Name%>Info <%=CamelCase(name)%>Info)
        
{
            
return <%=CamelCase(name)%>.Insert(<%=CamelCase(name)%>Info);
        }

        
public bool Edit(<%=TableName.Name%>Info <%=CamelCase(name)%>Info)
        
{
            
return <%=CamelCase(name)%>.Edit(<%=CamelCase(name)%>Info);
        }

        
public bool Delete(<%=CSharpType(TableName.PrimaryKey.MemberColumns[0])%> <%=CamelCase(TableName.PrimaryKey.MemberColumns[0].Name)%>)
        
{
            
return <%=CamelCase(name)%>.Delete(<%=CamelCase(TableName.PrimaryKey.MemberColumns[0].Name)%>);
        }

        
public IList<<%=TableName.Name%>Info> Get<%=TableName.Name%>s()
        
{
            
return <%=CamelCase(name)%>.Get<%=TableName.Name%>s();
        }

        
public <%=TableName.Name%>Info Get<%=TableName.Name%>ById(int id)
        
{
            
return <%=CamelCase(name)%>.Get<%=TableName.Name%>ById(id);
        }


    }

}

<!-- #include file="inc.inc" -->

源文件
https://files.cnblogs.com/yixiong/yixong%20Template.rar
 

posted @ 2008-06-04 15:21  yixiong  阅读(734)  评论(0编辑  收藏  举报