Question

I have a template that reads from database the tables needed to be generated as and objects. there is no problem in that, the template does generate the related file each one alone in a cs file (great) i use TemplateFileManager2.1 to achieve that. here in the tables loop i would like to call another template to create the Interface needed for that table using the tablename in the loop and put out that interface in a different file.

foreach (var table in tables)
    {  
        if(table.name !="sysdiagrams")
        {
        var manager2 = TemplateFileManager.Create(this);
            manager2.StartNewFile(table.name+"mm.cs","Generator","Deneme"); 
            InterfaceGeneratora(table.name);
            manager2.Process();
            manager2=null;


        manager.StartNewFile(table.name+".cs","Generator","GeneratedTables"); 

here manager object is used to out put the tables class and manager2 is for the Interfaces. the poit here is when manager2 is called and InterfaceGeneratora(table.name); runs manager2 start to replace the created tableinterface with the new table intefarce and at the end i have all table class created well but i have just the last table interface in hand. any help is appreciated.

Was it helpful?

Solution

As far as i know the Template file manager replaces all previously generated output files on the .Process() method. So you should call the .Process() method at the very end of your template.

In order to "outsource" the code generating the interface file you could either

  1. use a method inside the same template

    foreach(var table in tables)
    {
       if (table.name != "sysdiagrams")
       {
            manager.StartNewFile(table.name+"mm.cs", ...);
            // create interface code
       }
    
       manager.StartNewFile(table.name, ...);
       // create object code
    }
    manager.Process();
    
  2. use a includable template containing a method generating the interface code via the <#@ include #> directive

    <#@ include file="interfaces.ttinclude" #>
    foreach(var table in tables)
    {
        if (table.name != "sysdiagrams")
            Interfacegeneratora(table.Name, manager);
    
        // generate object code
        manager.StartNewFile(table.name, ...);
        // generate object code here
    }
    manager.Process();
    

    Where the file "interfaces.ttinclude" contains something like

    <#+ void Interfacegeneratora(string tableName, TemplateFileManager manager)
        {
            manager.StartNewFile(tableName + "mm.cs", ...);
            // generate interface code
        }
    #>
    
  3. process another template manually

    foreach(var table in tables)
    {
        if (table.name != "sysdiagrams")
        {
            var InterfaceT4 = new InterfaceTemplate();
            InterfaceT4.Session = new Dictionary<string, object>();
            InterfaceT4.Session.Add("tableName", table.name);
            InterfaceT4.Session.Add("manager", manager);
            InterfaceT4.InitializeSession();
            InterfaceT4.ProcessTemplate();
        }
    
        manager.StartNewFile(table.Name, ...);
        // generate object code
    }
    manager.Process();
    

    Where you create another T4 template, named "InterfaceTemplate.tt" that is a Preprocessed template. That realizes the same code as the .ttinclude file.

Note: the code is not double checked but shows how it should principally work.

Hope that helps, Nico

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top