Reputation: 111
I have an abstract Enumeration class that that i want to inherit from in a T4 template. The abstract class sets up the class structure. It also pulls values from the database.
For example this is what I want the T4 to generate:
public class Region : Enumeration
{
public static readonly Region Northeast = new Region(1, "Northeast", "1.jpg");
public static readonly Region Midwest = new Region(2, "Midwest", "2.jpg");
public static readonly Region South = new Region(3, "South", "3.jgp");
public static readonly Region West = new Region(4, "West","4.jpg");
public Region(int id, string name, string imageName)
: base(id, name, imageName)
{
}
}
This is my first T4 template so I'm making some assumptions here. If I understand the process correctly, I have to create code in the template that iterates through a list of values, and outputs the text, in this example: "public static readonly Region Northeast = new Region(1, "Northeast", "1.jpg");" for each value in the list.
My question is, should I place the code that gets the db values inside a Features function, call the function then iterate inside the code block of the T4 template? Also I'm assuming that to write text out, the text that needs to be written is place before/after the code block?
Thanks in advance
Upvotes: 2
Views: 333
Reputation: 111
After reading up on T4 templates I was able to answer my own question. Here is my template:
The line: IList<WebAPIMVC.Models.Region> regions = WebAPIMVC.APIClasses.ApplicationSetup.GetRegions();
Retrieves the data from the database using:
Note: I'm using EF6 here and cannot create a context as I would normally do. A context had to be created with the connection string embedded in the code, since the web config is not accessed. I then made a call to the database and returned a list of objects.
The template iterates through the objects and creates the code lines needed. I ended up with what I needed:
Upvotes: 1