1

I'm writing a T4 template in VS 2015 to generate some C# repository classes. The template works great, and provides me a .gen.cs per class as we want. But it also generates a blank .cs as well. I'm trying to figure out how to get it to NOT generate the extra .cs file.

So, if I have OrderRepositories.tt I will get:
OrderRepository.gen.cs
OrderItemRepository.gen.cs
OrderRepositories.cs --This file is empty unless the template errors

How can I skip that empty .cs file? (If I have not provided enough information below, please comment.)

Here is the include portion of the .tt file:

<#@ template language="C#" debug="True" #>
<#@ include file="T4Toolbox.tt" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>

Here's the main method in the .tt file:

public void RunTemplate()
{
    var types = GetTypesInNamespace(assembly, nameSpace);

    foreach (var type in types) {
        var fileName = string.Format(@"{0}Repository.gen.cs", type.Name);
        var name = type.Name;
        var repoTemplate = new RepositoryTemplate(fileName, repositoryNamespace, type.Namespace, name, dbContextName, dbContextNamespace, type);
        repoTemplate.RenderToFile(fileName);
    }
}

And the template portion of the .tt file:

<#+
private class RepositoryTemplate : Template {

    public RepositoryTemplate(string fileName, string repoNameSpace, string typeNameSpace, string name, string dbContextName, string dbContextNamespace, Type thisType)
    {
        _fileName = fileName;
        _repoNameSpace = repoNameSpace;
        // ...
    }

    public override string TransformText()
    {
using System;
using System.Data;

namespace <#= _repoNameSpace #> 
{
    public partial interface I<#= _name #>Repository : IBaseRepository<<#= _name #>> 
    {
    }

    public partial class <#= _name #>Repository : BaseRepository<<#= _name #>>, I<#= _name #>Repository 
    {
        public <#= _name #>Repository() : base("Order")
        {            
        }

        // ... other code
    }
}
        <#+
        return this.GenerationEnvironment.ToString();
    }
}#>
M Kenyon II
  • 4,136
  • 4
  • 46
  • 94
  • Seems similar to http://stackoverflow.com/questions/2601202/how-not-to-output-default-t4-generated-file, but that was also not fully solved. – kiziu Sep 28 '16 at 15:11

0 Answers0