動軟自定義模版


這里使用的是動軟的模板.

image

這是動軟代碼生成器的一個整體界面。

下面做的示例是從右邊模板管理中的選一個模板進行修改,這里我選了簡單三層模板中的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 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    {}

上面的代碼也是模板中我們非常熟悉的引用,接下來我來說下我今天要修改的內容。UPDATE功能,動軟中的update功能是將一個實體傳進去,全部修改,這里我把它修改成,只修改實體中存在的值

/// <summary>
/// this is a new update module
/// </summary>
public void Update(<#= ModelSpace #>Entity model)    
{        
	StringBuilder strSql=new StringBuilder();        
	List<SqlParameter> parameters = new List<SqlParameter>();    
}

先來定義兩個變量,因為和實際代碼中是一樣的,所以這里只有一點,就是<# = ModelSpace#>,這個變量已經在文件的開頭定義了,就是該模板的空間名+類名。

接下來我們需要寫一個for循環,來判斷哪些代碼要添加的sql語句中,哪些代碼不需要。

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 ) {#>,<#}#> ");}<# }#>
<# }#>

這里的幾個變量解釋寫

<#= host.TableName #> 表名
host.Fieldlist.Count 字段數
c.IsIdentity 是否主鍵
<#=preParameter#> @符號
<#=c.ColumnName#> 列名

.

最后加一個條件語句

strSql.Append(" where <#= CodeCommon.GetWhereParameterExpression(host.Keys, true ,host.DbType) #> ");

現在sql語句寫好了,當時發現沒有寫變量;添加變量比較簡單。只要在每個判空的條件語句后面添加就可以了

 

<# 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#>));
    }<# }#>
<# }#>

 

添加之后,會發現所有實體的屬性都被用string.IsNullOrEmpty()來判斷,所以這里還要添加一個判斷屬性類型的條件

<# 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#>));
    }
    <#}#>
<#}#>

 

上面是個例子,具體情況具體分析。

最后整體的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;
            }
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM