BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News MVC Scaffolding Provides configurable Code Generation for ASP.NET MVC

MVC Scaffolding Provides configurable Code Generation for ASP.NET MVC

This item in japanese

Steve Sanderson recently introduced MVC Scaffolding, a customizable code generation tool for ASP.NET MVC 3. MVC Scaffolding uses a simple command-line interface to automatically generate code based on templates. Standard templates allow for automated generation of many common elements, including Views, Actions, and Unit Test stubs.

The MVC Scaffolding Beta is available through the free NuGet package management tool. It can be installed through the Package Manager Console window via Install-Package MvcScaffolding. Once installed, the Get-Scaffolder command will display all available scaffolders. A scaffolder is a PowerShell script and a set of T4 templates that define how the code generation works. Scaffolder name aliases can be set or viewed using Set-DefaultScaffolder and Get-DefaultScaffolder.

The Controller scaffolder will automatically generate Create, Read, Update, and Delete views for a model with an accompanying controller and data context. View scaffolders exist for both webforms and Razor view engines. Basic validation is included, so fields with non-nullable types will be required in the generated views. A repository can optionally be created via the –Repository flag. This will generate a repository class and interface based on the model and also creates a controller that acts only on the interface. Other parameters include ControllerName, ModelType, Project, CodeLanguage, DbContextType, Area, Layout, Force, and NoChildItems.

Actions can be generated via the Scaffold Action command. This will create a method for the action on the indicated controller and will also make a view for the action. The WithViewModel switch will create a ViewModel class specific to the action and wire it up inside the controller and the generated view. The Post flag will generate a method stub for handling an Http post from the action.

The Scaffold UnitTest command can create test stubs for both Actions and post-handling Actions. Optional parameters include WithViewModel and ViewModel, which set the test stub up to use the specified model type. The ActionWithUnitTest scaffolder combines the functionality of the Action and UnitTest scaffolders into a single command.

Views, database contexts, actions, repositories, and Unit Test stubs also have their own scaffolders, and more are being added as development continues.

Templates can be overridden using the CustomTemplate scaffolder. This will generate an editable T4 template in the CustomTemplate folder. Templates in this folder are always chosen over standard templates of the same name.

ASP.NET MVC 3 is a .NET framework for building web applications using the Model-View-Controller pattern. In this pattern the Model represents the data, the View represents a particular display of the model, and the Controller adjusts the Model and View based on input from the user. A ViewModel may be used to pass only necessary data to the view.

Rate this Article

Adoption
Style

BT