If you're building ASP.NET MVC 3 applications, you'll want to know about the MvcScaffolding project on CodePlex. Software scaffolding is a way to template software applications so that you can easily get a site up and running, with complete CRUD functionality.
What is MvcScaffolding and why use it?
MvcScaffolding is tool available to ASP.NET MVC developers using Visual Studio that generates customizable controllers and views for any number of entities in your data model. In other words, MvcScaffolding is a way to quickly create an application layout based on data. This means there are less routine tasks that developers need to do, for example, creating your own controllers/views, CRUD methods, and validation, and more time spent on solving business problems.
MvcScaffolding generates the files as plain old .cs and .cshtml code files, with no designer files, so you're free to modify them as you want. You can scaffold controllers, views, repositories, etc... for CRUD operations in an app with a few commands, and have a fully functional application to work with. Combine the power of MvcScaffolding with Entity Framework's Code First feature, and you can start knocking out web apps much faster than before.
You can further customize the scaffolding itself using T4 templates, so that the generated output suits the needs of your application, in case you need to regenerate the scaffolding occasionally during development.
Getting Started with MvcScaffolding
Since a NuGet package exists for MvcScaffolding, you'll want to use "Package Manager Console" to install and use MvcScaffolding. Of course, you'll need NuGet first, which you can get by choosing "Extensions Manager" from the Tools menu.
Once NuGet is installed, you can then open the Package Manager Console.
The PMC is a Visual Studio 2010 PowerShell command line tool that allows you to manipulate NuGet packages and run commands. To install MvcScaffolding, just enter the following command at the PM> command line:
Scaffolding (and possible dependencies) are now ready to use. Keep the Package Manager Console window open, as you'll need it to run some commands to create the scaffolding itself after preparing the data model.
Preparing the data model for scaffolding
Because MvcScaffolding looks at the data model to determine what needs to be generated, you must have a data model in place, such as a set of POCOs in an EF Code First model. During the scaffolding process, MvcScaffolding takes the model's relationships, constraints, and validation into account when building the controllers and views.
Below is an annotated EF Code First data model with POCOs that represent products and categories in a database. Most of the code is basic C# classes, with the FourthCoffeeWebContext inheriting from the DbContext class which gives it the capabilities to perform ORM style tasks. The Products and Categories properties of the FourthCoffeWebContext class are type parameters for the DbSet class. This means the DbSet class allows the Products and Categories to interact with the DbContext and the database.
For a detailed look at this data model see my post on building a data model with EF Code First .
Seeding the database with data
Now that the data model is ready, you can seed it with data as well. Seeding will take place at runtime, when you first run the application after writing the code. You'll need a class that inherits from either the DropCreateDatabaseIfModelChanges or the DropDatabaseAlways classes, with your DbContext class as the type parameter. You'll also need to override the Seed method to be able to add data to the database. Below, some sample code demonstrates how to seed the database, based on the model.
The above code creates a list of categories and adds two category objects to it. As it creates each category, the code also creates and adds a related set of products. The code above takes advantage of object initializers of C# 3.0 to setup the objects. With the model and seed data in place, we can move onto scaffolding the app.
Off to Scaffold!
By entering the Get- PowerShell command and supplying DefaultScaffoler as an argument, you can see a list of currently available scaffolding commands. As you can see, there's more than controllers and views, as you can scaffold MVC applications extensively.
Here in the Package Management Console window you can use the Scaffold command with one of the default scaffolds listed in the above images to generate the files
Scaffold Controller Categories -force
The Controller scaffold creates both the controllers and views from the model. The -force argument ensures a regeneration of all scaffolds for the Categories, however the related Products still need to be shown, as shown below.
Scaffold Controller Products
You can watch the output in the Package Manager Console to view the files the scaffolding is creating, in real time. Once it is done scaffolding, run the application and access the /Products controller to see results in the browser similar to the image below.
The Edit view of a product reveals that MvcScaffolding created the relationships between the products and categories in the form of a dropdown list.
If you'd like, you can change the dropdown list to a 3rd party dropdown helper or use your own custom code, perhaps incorporating the jQueryUI. As you investigate the files generated by MvcScaffolding, you'll notice that they follow all convention over configuration, so it's easy to see where things are wired up.
MvcScaffolding is a great tool to get apps rolled out much faster than before with ASP.NET MVC. Now that you've seen what MvcScaffolding can do, you're probably ready to scaffold everything on earth - and you can (just about)! MvcScaffolding also contains commands to scaffold repositories, unit tests, and even create your own custom scaffolding. MvcScaffolding also generates clean, easily maintainable code. The code regeneration options such as scaffolding individual files only or using the -force switch to overwrite the files increase ease of maintenance by selecting the smallest possible amount of code to regenerate and re-test.
NOTE: Although this post has covered MvcScaffolding at the command line, the recently released MVC 3 Tools Update allows you to do create scaffolding with some of these options (blog post on using the MVC 3 tools update coming soon).