Trying to extend the Asp.Net MVC (T4) item templates to create multiple files.


One of the features I was really excited about for the MVC RC was the template/Model based scaffolding support in the Add View and Add Controller menu options inside of visual studio.  I think that they have made a great first version effort that will be light years ahead of other Microsoft web technologies.   (For more details see ScottGu’s post.)


Here is the walkthrough of the Add controller feature.  Click Add – > Controller.. –> Type in a Name and optionally select the Add action methods.

  This technology works by using T4 (Text Template Transformation Toolkit) it is a technology that feels a lot like markup.  So someone who is familiar with MVC should fee right at home. 

The real bang for your buck with this type of templates is to do the right thing and force the developer into the Pit of Success .  In terms of a Controller template the pit of success would mean.  For a Data Edit / CRUD scenario doing the following:

  • Create a Model for what your controller displays and acts upon first.
  • Create a Controller that knows about that model.. The model should be posted to the Save Method and potentially other methods as well.
  • Creating the unit test class and have full test coverage over the controller that was just “created”.  This is huge for me.  With all the industry and the Microsoft community has learned about the value of unit tests, why would we want to generate code and not provide the testing of that code as well.  How will we know that a change in the code did not cause other unintentional behavior?
  • The views that use this model for each of the actions should be created as well.

This is what the pit of success looks like to me.  So how do we go about doing that.?

The first challenge is to create more than one file out of the template.  I spiked out this in the following sample from the Code Camp Server project.

What this code does is runs through a method to generate the markup for the unit test, writes it to a file and than clears out the output of the unit test and allows the creation of the Controller to continue as normal.

This seems like a great solution until you look the unit test project and see that the unit test file is missing.  It is not missing, but rather the file was not added to the Unit Test project.  If you click on show all files you can see the file in the solution browser and see that it is not part of the project but it is in the correct space on the disk drive.  The only way I know of overcoming this problem is to get a reference to the Visual Studio automation object.  EnvDTE, than using that object we could add code to the template that could rectify this problem. Looking around the web this problem seemed to be solved, the visual studio T4 host automatically sends itself as the host to the T4 template. The problem with the MVC Template is that the host that is running the template is a custom host that was implemented just for the mvc project and they did not provide a mechanism for accessing the current DTE object.  This leave me at a place where the solution ends and it is not complete.  This method can be used to create views as well, but I do not see a lot of value for the following reasons.

  • The files cannot be added to the project so creating more files through this method actually adds more mouse clicks to show the hidden files and than including them in the projects.
  • The current MVC template host does not allow the include directive which would allow for more maintainable solution and have a one template file per project file created.
  • We could do more with the Add View scenario as well, but realistically the same limitations are in place so we cannot chain the creation of multiple views.  Which would really increase productivity.

These are deal breakers for me.  I wish this solution could work but it is half baked at this point. 

Closing Words

While I think the MVC Tooling team has actually lead the Visual Studio team with some innovations that really focus on improving productivity rather than putting all of their effort into the File New Project wizard, they still have a lot to do. I believe the Pit of Success for he creation of crud type operations in the MVC platform would force a developer to create a Model first.  Than the rewards for this would be giving a way to have some customizable templates that can take away the 80% of the  grudge work that is in visual studio for creating files and stubbing out code that can be easily inferred from a Model object.

Where do we go next…?  I am prototyping a scaffolding generator that will support the Pit of Success , more to come on this soon.. Here is what an example template would look like. More samples are here.

Warning: Using the IDataErrorInfo feature in an MVC application should be considered a Worst Practice.