MvcScaffolding: Overriding the T4 Templates
This blog post is part of a series about the MvcScaffolding NuGet package:
- [Introduction: Scaffold your ASP.NET MVC 3 project with the MvcScaffolding package][1]
- [Standard usage: Typical use cases and options][2]
- [One-to-Many Relationships][3]
- [Scaffolding Actions and Unit Tests][4]
- This post: Overriding the T4 templates
- [Creating custom scaffolders][5]
- [Scaffolding custom collections of files][6]
Previous posts in this series have shown how you can use MvcScaffolding’s built-in features to handle a range of CRUD scenarios, as well as quickly generate actions, view models, views, and unit test stubs using the included templates. But what if you want to do something different?
In this post and over the next two, I’ll show you how you can:
- Change the code that the built-in scaffolders generate by overriding the T4 templates they use
- Scaffold completely different types of things by creating your own new, custom scaffolders with arbitrary PowerShell logic
- **Combine multiple built-in and custom scaffolders **to generate whole structures of code files that implement useful functionality
Overriding the T4 Templates
Note: This post assumes you’ve already installed the MvcScaffolding package. If not, see the earlier posts in this series for an introduction.
The simplest form of customisation is overriding the T4 templates. For example, if you want to change what data access code is generated, you can supply your own templates that produce code based on NHibernate, or Mongo, or whatever, instead of Entity Framework which is the default.
To get started, you first need to know which existing T4 template you want to override. You can find this out either by:
- Looking in
\packages\ \tools to see the structure of scaffolders and templates - Or, by running the scaffolder whose output you want to change and passing the –Verbose flag so it tells you which templates it’s using
- Or, just by looking at the following table to see the built-in ones:
Scaffolder name | Template name | What it’s used for | |||||||||||||||||||||||||||
DbContext | DbContext | Empty data context class | |||||||||||||||||||||||||||
DbContext | DbContextEntityMember |
Entity set property used for each model type added to the data context class (e.g., DbSet
Repository
|
Repository
|
Repository interface and default concrete implementation
|
Action
|
Action
|
Action method (Note: only used when generating standalone actions. Not used by controller scaffolder)
|
Action
|
ActionPost
|
As above, for actions that handle POST requests
|
Action
|
ViewModel
|
View model used by scaffolded actions
|
ActionUnitTest
|
TestClass
|
Empty unit test class
|
ActionUnitTest
|
TestMethod
|
Unit test method stub
|
View
|
Create, _CreateOrEdit, Delete, Details, Edit, Empty, Index
|
MVC view for the corresponding view type |
(Note: CreateOrEdit has no leading underscore if you’re generating ASPX views, because that’s a Razor convention)
Controller
|
ControllerWithContext
|
Complete MVC controller with inline data access code
|
Controller
|
ControllerWithRepository
|
Complete MVC controller that uses repositories for data access
|
/// <summary> /// A dummy in-memory repository for <#= modelName #> entities. Its data will be reset each time you restart the application. /// </summary> public class <#= modelName #>Repository : I<#= modelName #>Repository { private readonly static Dictionary<<#= primaryKeyProperty.Type.AsString #>, <#= modelName #>> InMemoryCollection = new Dictionary<<#= primaryKeyProperty.Type.AsString #>, <#= modelName #>>(); public IQueryable<<#= modelName #>> All { get { return InMemoryCollection.Values.AsQueryable(); } } Scaffold Controller StockItem -Repository |