The term “Scaffolding” is used by many software technologies to mean “quickly generating a basic outline of your software that you can then edit and customise”. The scaffolding package we’re creating for ASP.NET MVC is greatly beneficial in several scenarios:
- If you’re learning ASP.NET MVC for the first time, because it gives you a fast way to get some useful, working code, that you can then edit and adapt according to your needs. It saves you from the trauma of looking at a blank page and having no idea where to start!
- If you know ASP.NET MVC well and are now exploring some new add-on technology such as an object-relational mapper, a view engine, a testing library, etc., because the creator of that technology may have also created a scaffolding package for it.
- If your work involves repeatedly creating similar classes or files of some sort, because you can create custom scaffolders that output test fixtures, deployment scripts, or whatever else you need. Everyone on your team can use your custom scaffolders, too.
Other features in MvcScaffolding include:
- Support for C# and VB projects
- Support for the Razor and ASPX view engines
- Supports scaffolding into ASP.NET MVC areas and using custom view layouts/masters
- You can easily customize the output by editing T4 templates
- You can add entirely new scaffolders using custom PowerShell logic and custom T4 templates. These (and any custom parameters you’ve given them) automatically appear in the console tab-completion list.
- You can get NuGet packages containing additional scaffolders for different technologies (e.g., there’s a proof-of-concept one for LINQ to SQL now) and mix and match them together
Installation:
You can install the package using the NuGet Package Manager Console, so it only takes a few seconds and you don’t have to download anything using your browser. To do so,
- Open the Package Manager Console window using Visual Studio’s View->Other Windows->Package Manager Console menu item.
- Enter the following:
Install-Package MvcScaffolding
Create Model:
Add the following class to your Models folder, then compile your solution (Ctrl-Shift-B):
namespace MvcApplication1.Models { public class Test { public int TestId { get; set; } public string Name { get; set; } public string City { get; set; } public DateTime Founded { get; set; } } }
Let The Magic Begin!
Create a complete Create-Read-Update-Delete (CRUD) UI for this model by issuing a single scaffolding command into the Package Manager Console:
Scaffold Controller Team
Notice that it’s created everything you needed, including the controller and all of the logic required for the CRUD functionality, and the associated views. It will include form validation as well. It’s all there and ready to go. Does it get any easier?