CodeSmith三层模板代码C#版.docx
《CodeSmith三层模板代码C#版.docx》由会员分享,可在线阅读,更多相关《CodeSmith三层模板代码C#版.docx(37页珍藏版)》请在冰豆网上搜索。
CodeSmith三层模板代码C#版
用CodeSmith编写的三层模板,仅供参考。
只需要Copy到CodeSmith中就可以了,然后就可以执行用了!
一般情况下开发大多都是用两层,也就是没有业务逻辑层。
但是我们开发时,项目较大或者需要用到设计模式时,两层就不利于一个开发了!
此文档没有表示层,有模型层,业务逻辑层,数据访问层。
一、模型层
模型层(Model)
<%--
Name:
模型层代码生成模版
Author:
弦夜歌
Description:
根据数据库的内容生成模型层代码
Version:
V2.0
--%>
<%@CodeTemplateLanguage="C#"TargetLanguage="C#"ResponseEncoding="UTF-8"%>
<%@PropertyName="Namespace"Type="String"Default="Model"Category="内容"Description="命名空间名称"%>
<%@PropertyName="SourceTable"Type="SchemaExplorer.TableSchema"Category="内容"Description="数据源表"%>
<%@PropertyName="ObjectName"Type="String"Category="注释"Description="对象名称,为生成注释而用"%>
<%@AssemblyName="System.Data"%>
<%@AssemblyName="SchemaExplorer"%>
<%@ImportNamespace="System.Data"%>
<%@ImportNamespace="SchemaExplorer"%>
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespace<%=Namespace%>
{
///
///<%=ObjectName%>的模型
///
[Serializable()]
publicclass<%=GetModelName()%>
{
private<%=GetPrimaryKeyType()%><%=GetPrimaryKeyFieldName()%>;
<%
foreach(ColumnSchemacoluminSourceTable.NonPrimaryKeyColumns)
{
%>
private<%=GetDataTypeByColumn(colum)%><%=GetFieldNameByColumn(colum)%>;
<%
}
%>
public<%=GetModelName()%>(){}
public<%=GetPrimaryKeyType()%><%=GetPrimaryKeyPropertyName()%>
{
get{return<%=GetPrimaryKeyFieldName()%>;}
set{<%=GetPrimaryKeyFieldName()%>=value;}
}
<%
foreach(ColumnSchemacoluminSourceTable.NonPrimaryKeyColumns)
{
%>
public<%=GetDataTypeByColumn(colum)%><%=GetPropertyNameByColumn(colum)%>
{
get{return<%=GetFieldNameByColumn(colum)%>;}
set{<%=GetFieldNameByColumn(colum)%>=value;}
}
<%
}
%>
}
}
publicstringGetClassName()
{
stringtempTable;
if(SourceTable.Name.EndsWith("s"))
{
tempTable=SourceTable.Name.Substring(0,SourceTable.Name.Length-1);
}
else
{
tempTable=SourceTable.Name;
}
returntempTable;
}
publicstringGetModelName()
{
returnConvertToPascal(GetClassName());
}
publicstringGetPrimaryKeyType()
{
if(SourceTable.PrimaryKey!
=null)
{
if(SourceTable.PrimaryKey.MemberColumns.Count==1)
{
returnGetCSharpDataTypeByDBColumn(SourceTable.PrimaryKey.MemberColumns[0]);
}
else
{
thrownewApplicationException("此模板只支持单个列的主键");
}
}
else
{
thrownewApplicationException("此模板需要有主键的表");
}
}
publicstringGetPrimaryKeyName()
{
if(SourceTable.PrimaryKey!
=null)
{
if(SourceTable.PrimaryKey.MemberColumns.Count==1)
{
returnConvertToCamel(SourceTable.PrimaryKey.MemberColumns[0].Name);
}
else
{
thrownewApplicationException("此模板只支持单个列的主键");
}
}
else
{
thrownewApplicationException("此模板需要有主键的表");
}
}
publicstringGetPrimaryKeyFieldName()
{
returnConvertToCamel(GetPrimaryKeyName());
}
publicstringGetPrimaryKeyPropertyName()
{
returnConvertToPascal(GetPrimaryKeyName());
}
publicstringGetDataTypeByColumn(ColumnSchemacolumn)
{
returnGetCSharpDataTypeByDBColumn(column);
}
publicstringGetFieldNameByColumn(ColumnSchemacolumn)
{
returnConvertToCamel(column.Name);
}
publicstringGetPropertyNameByColumn(ColumnSchemacolumn)
{
returnConvertToPascal(column.Name);
}
publicstringGetCSharpDataTypeByDBColumn(ColumnSchemacolumn)
{
switch(column.DataType)
{
caseDbType.AnsiString:
return"string";
caseDbType.AnsiStringFixedLength:
return"string";
caseDbType.Binary:
return"byte[]";
caseDbType.Boolean:
return"bool";
caseDbType.Byte:
return"byte";
caseDbType.Currency:
return"decimal";
caseDbType.Date:
return"DateTime";
caseDbType.DateTime:
return"DateTime";
caseDbType.Decimal:
return"decimal";
caseDbType.Double:
return"double";
caseDbType.Guid:
return"Guid";
caseDbType.Int16:
return"short";
caseDbType.Int32:
return"int";
caseDbType.Int64:
return"long";
caseDbType.Object:
return"object";
caseDbType.SByte:
return"sbyte";
caseDbType.Single:
return"float";
caseDbType.String:
return"string";
caseDbType.StringFixedLength:
return"string";
caseDbType.Time:
return"TimeSpan";
caseDbType.UInt16:
return"ushort";
caseDbType.UInt32:
return"uint";
caseDbType.UInt64:
return"ulong";
caseDbType.VarNumeric:
return"decimal";
default:
{
return"__UNKNOWN__"+column.NativeType;
}
}
}
publicstringConvertToPascal(stringstr)
{
returnstr.Substring(0,1).ToUpper()+str.Substring
(1);
}
publicstringConvertToCamel(stringstr)
{
returnstr.Substring(0,1).ToLower()+str.Substring
(1);
}
publicoverridestringGetFileName()
{
returnGetModelName()+".cs";
}
二、数据访问层
数据访问层(SQLServerDAL)
<%--
Name:
数据访问层代码生成模版
Author:
弦夜歌
Description:
根据数据库的内容生成模型层代码
Version:
V2.0
--%>
<%@CodeTemplateLanguage="C#"TargetLanguage="C#"ResponseEncoding="UTF-8"%>
<%@PropertyName="Namespace"Type="String"Default="SQLServerDAL"Category="内容"Description="命名空间名称"%>
<%@PropertyName="SourceTable"Type="SchemaExplorer.TableSchema"Category="内容"Description="数据源表"%>
<%@PropertyName="ObjectName"Type="String"Category="注释"Description="对象名称,为生成注释而用"%>
<%@AssemblyName="System.Data"%>
<%@AssemblyName="SchemaExplorer"%>
<%@ImportNamespace="System.Data"%>
<%@ImportNamespace="SchemaExplorer"%>
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingNCL.Data;
namespace<%=Namespace%>
{
usingModel;
///
///<%=ObjectName%>的数据访问操作类
///
publicclass<%=GetModelName()%>Service
{
///
///添加<%=ObjectName%>
///
///"><%=ObjectName%>
///最新编号
publicstaticintAdd<%=GetModelName()%>(<%=GetModelName()%><%=GetModelParameterName()%>)
{
stringsql=<%=GetInsertSql()%>;
SqlParameter[]pas=newSqlParameter[]
{
<%
for(inti=0;i{
ColumnSchemacolumn=SourceTable.NonPrimaryKeyColumns[i];
%>
newSqlParameter("@<%=column.Name%>",SqlDbType.<%=GetSqlDbType(column)%>,<%=column.Size%>)<%=(i==(SourceTable.NonPrimaryKeyColumns.Count-1)?
"":
",")%>
<%
}
%>
};
inti=0;
<%
for(inti=0;i{
%>
pas[i++].Value=<%=GetModelParameterName()%>.<%=SourceTable.NonPrimaryKeyColumns[i].Name%>;
<%
}
%>
returnConvert.ToInt32(SqlHelper.ExecuteScalar(sql,pas));
}
///
///更新<%=ObjectName%>
///
///"><%=ObjectName%>
///bool
publicstaticboolUpdate<%=GetModelName()%>(<%=GetModelName()%><%=GetModelParameterName()%>)
{
stringsql=<%=GetUpdateSql()%>;
SqlParameter[]pas=newSqlParameter[]
{
<%
for(inti=0;i{
ColumnSchemacolumn=SourceTable.NonPrimaryKeyColumns[i];
%>
newSqlParameter("@<%=column.Name%>",SqlDbType.<%=GetSqlDbType(column)%>,<%=column.Size%>),
<%
}
%>
newSqlParameter("@<%=GetPrimaryKeyName()%>",SqlDbType.<%=GetSqlDbType(GetPrimaryKeyColumn())%>,<%=GetPrimaryKeySize()%>)
};
inti=0;
<%
for(inti=0;i<=SourceTable.NonPrimaryKeyColumns.Count;i++)
{
if(i{
%>
pas[i++].Value=<%=GetModelParameterName()%>.<%=SourceTable.NonPrimaryKeyColumns[i].Name%>;
<%
}
else
{
%>
pas[i++].Value=<%=GetModelParameterName()%>.<%=GetPrimaryKeyName()%>;
<%
}
}
%>
introws=SqlHelper.ExecuteNonQuery(sql,pas);
return(rows==1);
}
///
///根据Id删除<%=ObjectName%>
///
///"><%=ObjectName%>Id
///bool
publicstaticboolDelete<%=GetModelName()%>By<%=GetPrimaryKeyPropertyName()%>(<%=GetPrimaryKeyType()%><%=GetPrimaryKeyFieldName()%>)
{
stringsql="DELETEFROM<%=SourceTable.Name%>WHERE<%=GetPrimaryKeyName()%>=@<%=GetPrimaryKeyName()%>";
SqlParameterpa=newSqlParameter("@<%=GetPrimaryKeyName()%>",SqlDbType.<%=GetPrimaryKeySqlDbType()%>,<%=GetPrimaryKeySize()%>);
pa.Value=<%=GetPrimaryKeyFieldName()%>;
introws=SqlHelper.ExecuteNonQuery(sql,pa);
return(rows==1);
}
///
///根据Id查询<%=ObjectName%>
///
///"><%=ObjectName%>Id
///<%=ObjectName%>对象
publicstatic<%=GetModelName()%>Get<%=GetModelName()%>By<%=GetPrimaryKeyPropertyName()%>(<%=GetPrimaryKeyType()%><%=GetPrimaryKeyFieldName()%>)
{
stringsql=<%=GetSelectOneModelSql()%>;
SqlParameterpa=newSqlParameter("@<%=GetPrimaryKeyName()%>",SqlDbType.<%=GetPrimaryKeySqlDbType()%>,<%=GetPrimaryKeySize()%>);
pa.Value=<%=GetPrimaryKeyFieldName()%>;
returnGetSingle<%=GetModelName()%>BySql(sql,pa);
}
///
///获得所有<%=ObjectName%>
///
///<%=ObjectName%>集合
publicstaticIList<<%=GetModelName()%>>GetAll<%=GetModelName()%>s()
{
stringsql=<%=GetSelectAllModelSql()%>;
returnGet<%=GetModelName()%>sBySql(sql);
}
///
///根据Sql获得单个<%=ObjectName%>
///
///<%=ObjectName%>对象
publicstatic<%=GetModelName()%