Joydip Kanjilal
Contributor

New features in MVC 6

opinion
Apr 23, 20155 mins
Software Development

Take advantage of the unified programming model in ASP.Net 5 MVC 6 to build applications that are cloud optimized

The Model View Controller pattern is one of the most popular design patterns that helps you to build applications that are easier to test and maintain. The Model View Controller (commonly known as MVC) framework promotes easier testability and code re-use. The ASP.Net MVC framework is built on top of the ASP.Net runtime and follows the MVC design pattern. In this post I will examine the Model View Controller design pattern and also present an overview of the new features in ASP.Net MVC 6.

The Model View Controller design pattern as the name suggests, comprises of three major components. These include the following:

  1. Model — this is the layer that represents the application’s data
  2. View — this represents the presentation or the user interface layer
  3. Controller — this layer typically contains the business logic of your application

The Model View Controller design pattern enables you to isolate the concerns and makes your application’s code easier to test and maintain.

The latest version of this framework is MVC 6. With MVC 6 the dependency on System.Web.dll has been eliminated — you will need to include the Microsoft.AspNet.Mvc namespace unlike System.Web.Mvc you did in the previous versions of ASP.Net MVC framework. The dependency on System.Web has been removed as it was very expensive — MVC 6 provides you a much leaner framework, faster startup time and reduced resource consumption.

The MVC 6 framework is designed for the cloud and is incorporated as part of the cloud optimized ASP.Net 5 runtime which in turn would be available as part of Visual Studio 2015. The advantage of having a cloud optimized framework is that you can have different versions of the CLR reside side by side for different websites running in the cloud. With ASP.Net 5, the MVC and Web API frameworks have been unified into a single programming model. So, MVC, Web API and the ASP.Net runtime are now all merged into one unified programming model. MVC 6 is host agnostic — other than having the capability to be hosted on IIS, it can be self-hosted also. MVC 6 also provides support for OWIN abstraction and includes Web API and Web Pages to eliminate the overlap between these three frameworks.

Dependency injection (also known as Inversion of Control) is a software design pattern that is used to implement loosely coupled, testable and reusable objects in your application. You can leverage the IServiceProvider interface to add your custom dependency injection container. This interface provides a level of abstraction over the actual dependency injection container implementation. Note that you have a default dependency injection container but with limited functionality. You can use this default dependency injection container if you need limited functionality. If you need added functionality, you can build your own dependency injection container and use the IServiceProvider interface to add the custom dependency injection container you have created.

Unlike its earlier counterparts, MVC 6 supports an environment based configuration system — deploying MVC 6 applications in the cloud is now simple. When you create a new MVC 6 project in Visual Studio, the new set of configuration files you would observe include the following:

  1. Config.json — this would typically contain the application configuration
  2. Project.json — this file contains the project dependency information
  3. Startup.cs — this file contains the Startup class that in turn contains a Configure method
  4. Global.json — this file contains information on the project references

After you create a MVC 6 project in Visual Studio, the Startup.cs file looks like this:

using Microsoft.Owin;

using Owin;

[assembly: OwinStartupAttribute(typeof(IDG.Startup))]

namespace IDG

{

    public partial class Startup

    {

        public void Configuration(IAppBuilder app)

        {

        }

    }

}

The following code snippet illustrates how a typical Config method of the Startup class looks like.

public void Configure(IApplicationBuilder app)      

    {   

       var configuration = new Configuration().AddJsonFile(“config.json”).AddEnvironmentVariables();

    }

Note the IApplicationBuilder parameter (this parameter is passed by the host when the application is started) in the Configure method. An instance of Configuration class is created and the configuration sources are passed. You can have any number of configuration sources — each configuration source is associated with a configuration value provider. This approach facilitates moving your application to the cloud if need be, seamlessly.

You can also use the ConfigureServices method to add Entity Framework services to the services container. The following code snippet shows how a typical ConfigureServices method would look like.

public void ConfigureServices(IServiceCollection services)

        {

          services.AddEntityFramework().AddSqlServer().AddDbContext();

          services.AddMvc();

          //Other code

        }

You can also specify the route information using the UseMvc extension method as shown in the code snippet below.

            {

                routes.MapRoute(

                    name: “default”,

                    template: “{controller}/{action}/{id}”,

                    defaults: new { controller = “IDG”, action = “Index” });

Note that the AddEntityFramework() and AddMvc() are extension methods defined in the IServiceCollection interface.

I will write more articles on MVC 6 in my future blog posts here. So, stay tuned!

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author