Question

I am trying to customise MVC Scaffolding T4 template for Repository so that it creates Ninject bindings in my App_Start\NinjectMVC3.cs class.

I can change the template to customise the repository that is created but I am at a bit of a loss as to how I can causes the template to add content to a separate file.

Anyone done anything similar? Also I guess splitting the repository and its interface into seperate files would be handy.

Thanks

Was it helpful?

Solution

If you need to create a different class file from the original T4 template, I think it's better if you create a new template for this purpose. If you want to save a file in a tempalte, you can create the file using Syste.IO library :

<#@ import namespace=“System.IO” #>

<#+
  void SaveOutput(string outputFileName)
  {
      string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
      string outputFilePath = Path.Combine(templateDirectory, outputFileName);
      File.WriteAllText(outputFilePath, GetMyContent()); 
  }
#>
<#+
  string GetMyContent()
  {
      // clean the environment 
      this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
      #>
        This is my content
      <#+
      // return new content
      return this.GenerationEnvironment.ToString(); 
  }
#>

You need to develope your GetMyContent() to fill the output file with the correct content. In this example GetMyContent cleans the environment every time, so remember to use the SaveOutput method after all the other files are created.

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