让开发者方便地生成数据层填充代码
<#@ template language="c#" HostSpecific="True" #> <#@ output extension= ".cs" #> <# TableHost host = (TableHost)(Host); host.NameSpace = "SPC"; string DbParaHead=host.DbParaHead; string DbParaDbType=host.DbParaDbType; string preParameter=host.preParameter; string ModelSpace = host.GetModelClass(host.TableName); ColumnInfo identityKey=host.IdentityKey; string returnValue = "void"; if (identityKey!=null) { returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName); } #> /// <summary> /// Fill the model /// </summary> private void Fill<#=ModelSpace#>(<#=ModelSpace#> model, DataRow dr) { <# foreach (ColumnInfo c in host.Fieldlist) { #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="DateTime"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#> if(dr["<#=c.ColumnName#>"].ToString()!="") { model.<#=c.ColumnName#>=<#=CodeCommon.DbTypeToCS(c.TypeName)#>.Parse(dr["<#=c.ColumnName#>"].ToString()); } <# } #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") {#> model.<#=c.ColumnName#>= dr["<#=c.ColumnName#>"].ToString(); <# } #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="byte[]") {#> if(dr["<#=c.ColumnName#>"].ToString()!="") { model.<#=c.ColumnName#>= (byte[])dr["<#=c.ColumnName#>"]; } <# } #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="Guid") {#> if(dr["<#=c.ColumnName#>"].ToString()!="") { model.<#=c.ColumnName#>= dr["<#=c.ColumnName#>"].ToString(); } <# } #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#> if(dr["<#=c.ColumnName#>"].ToString()!="") { if((dr["<#=c.ColumnName#>"].ToString()=="1")||(dr["<#=c.ColumnName#>"].ToString().ToLower()=="true")) { model.<#=c.ColumnName#>= true; } else { model.<#=c.ColumnName#>= false; } } <# } #> <# } #> }
效果:
/// <summary> /// Fill the model /// </summary> private void Fillf_peitao(f_peitao model, DataRow dr) { if (dr["id"].ToString() != "") { model.id = int.Parse(dr["id"].ToString()); } model.f_peitao = dr["f_peitao"].ToString(); }