Introduction
Besides the renaming of ASP.NET, introduction of the .NET execution environment, and the overhaul of the whole approach to Web Development in .NET, there are some really cool things that stand out in the ASP.NET 5 MVC. More about the introduction is available here.
ASP.NET 5 and Its MVC Feature
I would like to talk about the following:
- Environment
- HTML Helpers
- Middleware: Popularized by Node.js
- Dependency Injection
- Configuration Settings
Environment
In previous versions of ASP.NET Framework, there has been no way to specify environments you are in. A hack I would normally have is to add the lines below to my config file...
<add key="env" value="dev"/>
...and then check in my code if environment is dev or live or test, but I get this for free in this version of .NET with intellisense support. I can indicate easily which JavaScript I wish to load in development environment and which to load in test environment. This also comes naturally to my code anywhere in the project.
HTML Helpers
Many are already familiar with tags such as:
@Html.EditorFor<>()
Watching Scot Hanselman and Glenn Condron go through Introduction to ASP.NET 5, it was fascinating that all you need to be familiar within your View is your HTML 5 and just a few things ASP.NET 5 provides for you out of the box.
Instead of having to remember HTML helpers with all the overloads, what you now have is:
- Bind the form to a method using
asp-action
and asp-controller
- Bind the form controls to the form with
asp-for
- Submit and Model Binding maps the model in the view to the parameter of the
Action
method annotated with HttpPost
Middleware
According to Node.js Design Pattern, in the Node.js world, Middleware was popularized by Express. And then, we also have Owin chaining middleware in the Http pipeline. This fantastic feature comes in handy in this new framework. You can Use Run and Build them once you chain them properly.
What a middleware actually does is to pause the request pipeline, in order to enable you to add some more configuration, take some decisions, take actions and either allow before returning or return request. Middleware chaining in .NET 5 is similar to what we have with OWIN. You can have UseErrorHandlers
, UseMVC
and what have you.
Dependency Injection
This comes with popularity of decoupling application components to facilitate Unit test, enable ease of code change, etc. There are various libraries today available for dependency injection. Unity, MEF, Windsor Castle, Ninject and many more. What these libraries actually offer is available in the new .NET Framework.
My Database Context is easily available and I can just specify what I want to carry about in the DI container.
Configuration Settings
This will be very familiar to folks who have worked in time past with Nodejs. My configuration settings are actually specified in config.json files, I can instruct the framework to look for a particular config file, say config.development.json or whatever. This flexibility is very good for developers. How about User Secrets that prevent you from checking in sensitive information to repositories. This is indeed an amazing feature.
There are so many other features that are available aside the few mentioned above which is taking the face of development to a completely new level.
Conclusion
One thing I look forward in the MVC of this new .NET Framework is modifying the Model Binding Engine, such that I can bind multiple Models to a single view without the use of ViewModel
s. This is because in reality on a view, you can have multiple models you want to bind separately and use Ajax calls to treat such page sections.
In the near future, I will be comparing this new version of .NET with Nodejs because I see many things adapted from the Node.js world into the new .NET Framework.