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 asp.net 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.  http://code.google.com/p/codecampserver/source/browse/trunk/src/UI/CodeTemplates/Crud+Controller/View-Edit.tt?spec=svn647&r=647 More samples are here.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Eric Hexter

I am the CTO for QuarterSpot. I (co)Founded MvcContrib, Should, Solution Factory, and Pstrami open source projects. I have co-authored MVC 2 in Action, MVC3 in Action, and MVC 4 in Action. I co-founded online events like mvcConf, aspConf, and Community for MVC. I am also a Microsoft MVP in ASP.Net.
This entry was posted in .Net, agile, Asp.Net MVC, c#, mvc, testing, Tools. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://flux88.com Ben Scheirman

    That’s a shame, as I haven’t dug deep into T4 yet, I fully agree that they are of limited value if you can’t add additional files, such as unit tests.

  • http://blogger.forgottenskies.com Steve

    I’m disappointed that the only way this works is to have the controllers inside the web application.

    I’m confident that most application will have a separate assembly for their controllers.

  • Ryan R.

    I agree with you there. It’s great what they’ve done with all the “scaffolding” and how easy it is to set up a very simple web app, but I think they’re encroaching on some dangerous territory by making it so easy to generate so much code with no tests.

  • http://pabloblamirez.blogspot.com PabloBlamirez

    Hi,
    have you seen Damien Guards great example of producing multiple outputs from one tt file?
    The helper code he presents uses a strategy for managing the interaction with visual studio. By default it adds the files created by the helper to the same project as the template file, I’m pretty sure you could use a custom management strategy to add your generated files to a different project though with a bit of deft tweaking

    See:
    http://damieng.com/blog/2009/01/22/multiple-outputs-from-t4-made-easy

    Paul

  • http://www.lostechies.com/members/erichexter/default.aspx erichexter

    @PabloBlamirez I know Damien and I am familiar with his work. The problem is with the MVC project extension, it does not provide a reference to visual studio from the host. Damien’s code works on top of the visual studio T4 host which is a totally separate implementation. So it does not apply to the MVC templates.

  • http://pabloblamirez.blogspot.com PabloBlamirez

    Fair enough, it’s not something I’ve used I’d just bookmarked it for a rainy day as it looked a neat implementation. Thats a shame