自定义代码生成器
這里使用的是動(dòng)軟的模板.
這是動(dòng)軟代碼生成器的一個(gè)整體界面。
下面做的示例是從右邊模板管理中的選一個(gè)模板進(jìn)行修改,這里我選了簡(jiǎn)單三層模板中的DAL.cmt模板
?
<#@ template language="c#" HostSpecific="True" #><#@ output extension= ".cs" #><# TableHost host = (TableHost)(Host); string DbParaHead=host.DbParaHead; string DbParaDbType=host.DbParaDbType; string preParameter=host.preParameter; string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName); ColumnInfo identityKey=host.IdentityKey; string returnValue = "void"; if (identityKey!=null) { returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName); }#>?
在模板上右鍵,編輯查看。就能看到上面的代碼,這是模板中自定義的一些變量。
?
using System; using System.Text;using System.Data.SqlClient;using System.Collections.Generic; using System.Data;using Maticsoft.DBUtility;namespace DAL <# if( host.Folder.Length > 0){ #> .<#= host.Folder #><# } #>{ <# if( host.TableDescription.Length > 0) {#> //<#= host.TableDescription #> <# } #> public partial class <#= host.GetDALClass(host.TableName) #>DAO {?
上面的代碼也是模板中我們非常熟悉的引用,接下來(lái)我來(lái)說(shuō)下我今天要修改的內(nèi)容。UPDATE功能,動(dòng)軟中的update功能是將一個(gè)實(shí)體傳進(jìn)去,全部修改,這里我把它修改成,只修改實(shí)體中存在的值
?
/// <summary>/// this is a new update module/// </summary>public void Update(<#= ModelSpace #>Entity model) { StringBuilder strSql=new StringBuilder(); List<SqlParameter> parameters = new List<SqlParameter>(); }?
先來(lái)定義兩個(gè)變量,因?yàn)楹蛯?shí)際代碼中是一樣的,所以這里只有一點(diǎn),就是<# = ModelSpace#>,這個(gè)變量已經(jīng)在文件的開頭定義了,就是該模板的空間名+類名。
接下來(lái)我們需要寫一個(gè)for循環(huán),來(lái)判斷哪些代碼要添加的sql語(yǔ)句中,哪些代碼不需要。
?
strSql.Append("update <#= host.TableName #> set ");<# for(int i=0;i< host.Fieldlist.Count;i++){ ColumnInfo c = host.Fieldlist[i]; #><# if (!c.IsIdentity) {#> if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){ strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> <# if (i< host.Fieldlist.Count-1 ) {#>,<#}#> ");}<# }#><# }#>?
這里的幾個(gè)變量解釋寫
| <#= host.TableName #> | 表名 |
| host.Fieldlist.Count | 字段數(shù) |
| c.IsIdentity | 是否主鍵 |
| <#=preParameter#> | @符號(hào) |
| <#=c.ColumnName#> | 列名 |
.
最后加一個(gè)條件語(yǔ)句
strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true ,host.DbType) #> ");現(xiàn)在sql語(yǔ)句寫好了,當(dāng)時(shí)發(fā)現(xiàn)沒有寫變量;添加變量比較簡(jiǎn)單。只要在每個(gè)判空的條件語(yǔ)句后面添加就可以了
?
<# if (!c.IsIdentity) {#> if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){ strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , "); parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>)); }<# }#><# }#>?
添加之后,會(huì)發(fā)現(xiàn)所有實(shí)體的屬性都被用string.IsNullOrEmpty()來(lái)判斷,所以這里還要添加一個(gè)判斷屬性類型的條件
<# if (!c.IsIdentity) {#> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") {#>if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){ strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , "); parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>)); }<# }#><# 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(model.<#=c.ColumnName#>!=0){ strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , "); parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>)); } <#}#><#}#>?
上面是個(gè)例子,具體情況具體分析。
最后整體的UPDATE代碼如下
/// <summary> /// this is a new update module l||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| /// </summary> public bool Update(<#= ModelSpace #>Entity model) { StringBuilder strSql=new StringBuilder(); List<SqlParameter> parameters = new List<SqlParameter>(); strSql.Append("update <#= host.TableName #> set "); <# for(int i=0;i< host.Fieldlist.Count;i++) { ColumnInfo c = host.Fieldlist[i]; #> <# if (!c.IsIdentity) {#> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string") {#> if(!string.IsNullOrEmpty(model.<#=c.ColumnName#>)){ strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , "); parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>)); }<# }#> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#> strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , "); parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>)); <# }#> <# 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(model.<#=c.ColumnName#>!=0){ strSql.Append(" <#= c.ColumnName #> = <#=preParameter#><#=c.ColumnName#> , "); parameters.Add(new SqlParameter("<#=preParameter#><#=c.ColumnName#>",model.<#=c.ColumnName#>)); } <#}#> <#}#> <# }#> strSql = strSql.Remove(strSql.Length - 2,2); strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true ,host.DbType) #> "); <# for(int i=0;i< host.Keys.Count;i++) { ColumnInfo key = host.Keys[i]; #> <# if (key.IsPrimaryKey || !key.IsIdentity) {#> parameters.Add(new SqlParameter("<#=preParameter#><#=key.ColumnName#>",model.<#=key.ColumnName#>)); <#}#> <# }#> int rows=<#= host.DbHelperName#>.ExecuteSql(strSql.ToString(),parameters.ToArray()); if (rows > 0) { return true; } else { return false; } }轉(zhuǎn)載于:https://www.cnblogs.com/TivonStone/archive/2012/02/28/2371256.html
總結(jié)
- 上一篇: .NET MYSQL数据库操作基类( C
- 下一篇: CSS3 border-image的使用