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: Model — this is the layer that represents the application’s data View — this represents the presentation or the user interface layer 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: Config.json — this would typically contain the application configuration Project.json — this file contains the project dependency information Startup.cs — this file contains the Startup class that in turn contains a Configure method 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! Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos