BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

MVC Scaffolding Provides configurable Code Generation for ASP.NET MVC

| by David Cooksey on Apr 08, 2011. Estimated reading time: 2 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and dont miss out on content that matters to you

BT