在t4模板中查找带有外键的子表

时间:2017-08-02 02:31:16

标签: visual-studio-2013 foreign-keys t4

我很擅长使用T4模板生成POCO,我现在有一系列工作模板可以生成我的DTO,DAL和Manager对象以及相关的CRUD SQL文件,历史触发器和表。

它适用于单个表,但我无法弄清楚如何在DTO和DAL中包含子表。基本上,我想在我的DTO中创建只读属性,并在DAL对象中为外键的相关子DTO创建它们的Get Data方法。

这是我的DTOGenerator.tt,它是从另一个.tt中执行的,包括Table,Namespace,Ddestination文件夹和一个字符串来表示执行模板的用户。

提前感谢您的帮助。

<#@ import namespace="System" #><##>
<#@ import namespace="System.IO" #><##>
<#@ import namespace="System.Text" #><##>
<#@ assembly name="System.Data" #>
<#@assembly name="Microsoft.SqlServer.Smo"#><##>
<#@assembly name="Microsoft.SqlServer.ConnectionInfo"#><##>
<#@assembly name="Microsoft.SqlServer.Management.Sdk.Sfc"#><##>
<#@import namespace="Microsoft.SqlServer.Management.Smo"#><##>
<#+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();

    // 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;
        }
        else if (col.IsForeignKey)
        {
            //Create Read Only property to return child DTO via the child table DAL
        }

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

        // Accessors
        if(i>1){stbAccessors.Append("\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 <#=target_table.Name #>
    // 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

    [DataContract()]
    public class <#=target_table.Name #>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 <#=target_table.Name #>
        /// </summary>
        public <#=target_table.Name #>DTO()
        {
            // Default constructor is needed
        }

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

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

0 个答案:

没有答案