Example code can be found here:
Most projects have some level of repetitive code that comes from the architecture or structure of an application. Taking ASP.NET MVC as an example, apps have controllers, views and (potentially) services that all have some similarity in structure with varying methods and underlying class names. Scaffolding these portions of an application can save time, reduce human errors and be used help maintain a particular structure. In Visual Studio, ASP.NET MVC comes with a pre built set of T4 scaffolding for generating common code.
We had a need to generate scaffolding based on T4 templates, but from a custom app that would be ran outside of Visual Studio. Our team maintains a multitenant SaaS starter kit called Protolith that is built on the ASP.NET MVC Web API and Backbone. The scaffolding requirements are quite a bit different for a Backbone based application consuming a RESTful API than the needs of a standard ASP.NET MVC app. For example, our app has typical objects (entities –> services –> repository) exposed through an API, but then consumes the API all through Backbone based models, routers and views. Our goal was to scaffold both the front and back end objects by providing meta data on the basic structure of the app.
Scaffolding with T4 is not a silver bullet. CRUD based screens often have little nuances and rules that are hard to scaffold, however scaffolding basic objects for CRUD gives us a solid starting point for new features.
The problem with a normal T4 template, is that is tied to the Microsoft.VisualStudio.TextTemplating assembly, which basically translates that you need Visual Studio to run the template transformation. The answer for our command line tool it to use preprocessed or runtime T4 templates. You can checkout Oleg Sync’s blog for more information on the differences between traditional and preprocessed T4 templates. (http://www.olegsych.com/2009/09/t4-preprocessed-text-templates/)
I had a hard time finding the ‘Hello World’ of Runtime Text Templates usage in a custom application. Often times, the simplest code provides a nice starting point for us to get going. Below we will create a small app to generate an ‘Entity’. For our starter kit, this will be the starting point for our code generation.
Here we use the private backing fields from EntityData.cs to fill in the assembly and entity name during code generation.
Lets tell the console application to create an Entity with our custom entity name and assembly name. The output from the transformation will be printed to the console. At a later point we can output this to a file, or directly into our solution. Update your Program.cs file Main method.
Note that the Entity class has two parameters for entity and assembly name that were defined in the partial class. Running the program should output our scaffolded User entity to the console.