PetaPoco的T4模板優化改進

主要修改Database.tt這個文件

優化點:

1.修改默認參數;

2.修改實體類名成爲首字母大寫的駝峯式寫法,自動去掉下劃線;

3.修改字段爲首字母大寫的駝峯式寫法,自動去掉下劃線;

<#@ include file="PetaPoco.Core.ttinclude" #>
<#
    // Settings
    ConnectionStringName = "";          // Uses last connection string in config if not specified
    Namespace = "";
    RepoName = "";
    GenerateOperations = true;
    GeneratePocos = true;
    GenerateCommon = true;
    ClassPrefix = "";
    ClassSuffix = "";
    TrackModifiedColumns = false;
    ExplicitColumns = true;
    ExcludePrefix = new string[] {}; // Exclude tables by prefix.

    // Read schema
    var tables = LoadTables();


/*
    // Tweak Schema
    tables["tablename"].Ignore = true;                          // To ignore a table
    tables["tablename"].ClassName = "newname";                  // To change the class name of a table
    tables["tablename"]["columnname"].Ignore = true;            // To ignore a column
    tables["tablename"]["columnname"].PropertyName="newname";   // To change the property name of a column
    tables["tablename"]["columnname"].PropertyType="bool";      // To change the property type of a column
*/
    foreach(Table tbl in from t in tables select t)  
    {  

            //處理表名稱  
            string srcStr = tbl.Name.ToLower();  
            string newString = "";  
            int first = 0;  
            while (srcStr.IndexOf("_") != -1)  
            {  
                first = srcStr.IndexOf("_");  
                if (first != srcStr.Length)  
                {  
                    newString = newString + srcStr.Substring(0, first);  
                    srcStr = srcStr.Substring(first + 1);  
                    srcStr = srcStr.Substring(0, 1).ToUpper() + srcStr.Substring(1);  
                }  
            }  
            newString = newString + srcStr;  

            tables[tbl.Name].ClassName =ClassPrefix+newString.Substring(0, 1).ToUpper() + newString.Substring(1)+ClassSuffix;
            //處理字段名稱
            foreach(Column col in from c in tbl.Columns select c)
            {
                if(col.Name.StartsWith("_"))
                {
                    tables[tbl.Name][col.Name].Ignore=true; 
                }
                string srcStrCol = col.Name.ToLower();
                string newStringCol = "";
                int firstCol = 0;
                while (srcStrCol.IndexOf("_") != -1)
                {
                    firstCol = srcStrCol.IndexOf("_");
                    if (firstCol != srcStrCol.Length)
                    {
                        newStringCol = newStringCol + srcStrCol.Substring(0, firstCol);
                        srcStrCol = srcStrCol.Substring(firstCol + 1);
                        srcStrCol = srcStrCol.Substring(0, 1).ToUpper() + srcStrCol.Substring(1);
                    }
                }
                newStringCol = newStringCol + srcStrCol;
                tables[tbl.Name][col.Name].PropertyName=newStringCol.Substring(0, 1).ToUpper() + newStringCol.Substring(1); 
            }
    }
    // Generate output
    if (tables.Count>0)
    {
#>
<#@ include file="PetaPoco.Generator.ttinclude" #>
<# } #>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章