访问者///评论和T4模板

时间:2017-10-21 00:40:08

标签: t4

我正在使用T4模板为POCO创建访问器,当我尝试生成访问者注释时,当属性在VS中悬停时它们不起作用。

我在循环遍历列时构建访问器

// Accessors
if(i>1){stbAccessors.Append("\t\t");}
if(col.IsForeignKey)
{
    stbAccessors.Append("/// <summary>\r\n\t\t");
    stbAccessors.Append("/// FOREIGN KEY\r\n\t\t");
    stbAccessors.Append("/// <summary>\r\n\t\t");
}
stbAccessors.Append("[DataMember()]\r\n");
stbAccessors.Append("\t\tpublic ").Append(propertyType).Append(" ").Append(col_name).Append("\r\n");
stbAccessors.Append("\t\t{\r\n");
stbAccessors.Append("\t\t\tget { return").Append(" _").Append(col.Name).Append("; }\r\n");
stbAccessors.Append("\t\t\tset\r\n");
stbAccessors.Append("\t\t\t{\r\n");
stbAccessors.Append("\t\t\t\tif (").Append("_").Append(col.Name).Append(" == value){ return; }\r\n");
stbAccessors.Append("\t\t\t\t_").Append(col.Name).Append(" = value;\r\n");
stbAccessors.Append("\t\t\t\tChanged = true;\r\n");
stbAccessors.Append("\t\t\t}\r\n");
stbAccessors.Append("\t\t}\r\n\r\n");

然后我生成了这样的访问者代码。

#region " ACCESSORS "

/// <summary>
/// Returns TRUE if data on this object record has changed
/// </summary>
[DataMember()]
public bool Changed { get; set; }

<#=stbAccessors.ToString() #>
#endregion

更改的访问者在悬停时正确显示文本“如果此对象记录上的数据已更改,则返回TRUE”,但任何标记为“FOREIGN KEY”的文件都不会。

有什么想法吗? 谢谢你的帮助。

以下是我的DTO生成器的完整代码

<#@import namespace="System" #><##>
<#@import namespace="System.IO" #><##>
<#@import namespace="System.Text" #><##>
<#@assembly name="Microsoft.SqlServer.Smo"#><##>
<#@assembly name="Microsoft.SqlServer.ConnectionInfo"#><##>
<#@assembly name="Microsoft.SqlServer.Management.Sdk.Sfc"#><##>
<#@import namespace="Microsoft.SqlServer.Management.Smo"#><##>
<#@assembly name="System.Data"#><##>
<#+public void GenerateDTO(Table target_table, string classNamespace, string destinationFolder, string executed_by)
{
    // StringBuilders for Fields, Properties and Parameters.
    StringBuilder stbMembers = new StringBuilder();
    StringBuilder stbUserSaveEditMembers = new StringBuilder();
    StringBuilder stbAccessors = new StringBuilder();
    StringBuilder stbUserSaveEditAccessors = new StringBuilder();
    StringBuilder stbConstructorParameters = new StringBuilder();
    StringBuilder stbClassProperties = new StringBuilder();
    StringBuilder stbUserSaveEditClassProperties = new StringBuilder();

    // Object Name
    string ObjectName = SnakeCaseToCamelCase(target_table.Name);

    // Keep count so we don't whitespace the last property/column
    int columnCount = target_table.Columns.Count;
    int i = 0;
    string col_identity = string.Empty;

    // Iterate all columns
    foreach (Column col in target_table.Columns)
    {
        i++;
        string propertyType = GetNetDataType(col.DataType.Name);

        // If we can't map it, skip it
        if (string.IsNullOrWhiteSpace(propertyType))
        {
            // Skip
            continue;
        }

        // Make propertytype nullable for nullable columns
        if (col.Nullable && propertyType != "string" && propertyType != "byte[]")
        {
            propertyType += "?";
        }

        // Build Fields & Class Properties
        string col_name = SnakeCaseToCamelCase(col.Name);

        // Capture Identity column name
        if(col.Identity)
        {
            col_identity = col_name;
        }

        // TABLE
        // Members
        stbMembers.Append("private ").Append(propertyType).Append(" _").Append(col.Name).Append(";\r\n\t\t");

        // Accessors
        if(i>1){stbAccessors.Append("\t\t");}
        if(col.IsForeignKey)
        {
            stbAccessors.Append("/// <summary>\r\n\t\t");
            stbAccessors.Append("/// FOREIGN KEY\r\n\t\t");
            stbAccessors.Append("/// <summary>\r\n\t\t");
        }
        stbAccessors.Append("[DataMember()]\r\n");
        stbAccessors.Append("\t\tpublic ").Append(propertyType).Append(" ").Append(col_name).Append("\r\n");
        stbAccessors.Append("\t\t{\r\n");
        stbAccessors.Append("\t\t\tget { return").Append(" _").Append(col.Name).Append("; }\r\n");
        stbAccessors.Append("\t\t\tset\r\n");
        stbAccessors.Append("\t\t\t{\r\n");
        stbAccessors.Append("\t\t\t\tif (").Append("_").Append(col.Name).Append(" == value){ return; }\r\n");
        stbAccessors.Append("\t\t\t\t_").Append(col.Name).Append(" = value;\r\n");
        stbAccessors.Append("\t\t\t\tChanged = true;\r\n");
        stbAccessors.Append("\t\t\t}\r\n");
        stbAccessors.Append("\t\t}\r\n\r\n");

        // Class Properties
        stbClassProperties.Append("_").Append(col.Name).Append(" =").Append(" p").Append(col_name).Append(";");
        if(i < columnCount)
        {
            stbClassProperties.Append("\r\n\t\t\t");
        }

        // Class Parameters
        stbConstructorParameters.Append(propertyType).Append(" p").Append(col_name);
        if(i < columnCount)
        {
            stbConstructorParameters.Append(",\r\n\t\t\t\t\t\t\t\t");
        }
}#>
using System;
using System.Runtime.Serialization; 

namespace <#=classNamespace#>
{
    #region " History "

    // <remarks>
    //-------------------------------------------------------------------------------------------------------------------------------
    // Purpose       : Data Transfer Object for <#=ObjectName #>
    // T4 Template   : DTOGenerator.tt
    // Date Created  : <#=DateTime.Now.ToString(@"MM\/dd\/yyyy")#>
    // Created By    : <#=executed_by#>
    // Notice        : Copyright © <#=DateTime.Now.ToString(@"yyyy")#> GooeyPC, All rights reserved.
    //-------------------------------------------------------------------------------------------------------------------------------
    // History:
    // 
    //-------------------------------------------------------------------------------------------------------------------------------
    // </remarks>

    #endregion

    /// <summary>
    /// <#=ObjectName #> Data Transfer Object
    /// </summary>
    [DataContract()]
    public class <#=ObjectName #>DTO
    {
       #region " MEMBERS "

        // TABLE FIELDS 
        <#=stbMembers.ToString() #>
        #endregion

        #region " ACCESSORS "

        /// <summary>
        /// Returns TRUE if data on this object record has changed
        /// </summary>
        [DataMember()]
        public bool Changed { get; set; }

        <#=stbAccessors.ToString() #>
        #endregion

        #region " PROPERTIES - READ ONLY "

        /// <summary>
        /// Returns TRUE if primary key id != 0 (i.e. valid primary key id exists.)
        /// </summary>
        public virtual bool RecordExists
        {
            get { return (<#=col_identity #> != 0); }
        }

        #endregion

        #region " CONSTRUCTORS "

        /// <summary>
        /// Description of <#=ObjectName #>
        /// </summary>
        public <#=ObjectName #>DTO()
        {
            // Default constructor is needed
        }

        /// <summary>
        /// Description of <#=ObjectName #>
        /// </summary>
        public <#=ObjectName #>DTO(<#=stbConstructorParameters.ToString() #>)
        {            
            // Set Class Properties
            <#=stbClassProperties.ToString() #>
        }

        #endregion
    }
}
<#+
    // Write new class to its own file
    SaveOutput(destinationFolder, ObjectName + "DTO.cs");}
#>

这是为应该具有“FOREIGN KEY”评论的访问者生成的代码。

/// <summary>
/// FOREIGN KEY
/// <summary>
[DataMember()]
public Int32? YourChildTableNameId
{
    get { return _your_child_table_name_id; }
    set
    {
        if (_your_child_table_name_id == value){ return; }
        _your_child_table_name_id = value;
        Changed = true;
    }
}

0 个答案:

没有答案