Insight : MVC, ASP.NET 1.0 and Web Forms
The Model View Controller (MVC) architectural pattern has a long history (together with its variants) and has been used in many languages and is encompassed by a number of frameworks (Smalltalk; PHP Zend Framework; Java Swing; etc.). ASP.NET Core 1 provides Microsoft's latest version of a MVC architecture for Web Applications. The main benefits of MVC revolve around the seperation of concerns, namely, the separation of Models (Data - Domain objects); Views (which the 'user' sees) and Controllers (which respond to user input). Separation of concerns has many benefits including testability; modifyability; flexibility and team working.
Whilst there have been many previous versions of ASP.NET MVC (1, 2,..., 5) the version in ASP.NET Core 1 now has many significant new features. Amongst these is integrated support for Dependency Injection (DI) and Inversion Of Control (IOC). Whilst previous version of MVC could use Dependency Injection, this would require installing and configuring an IOC container. The integration of DI and IOC support now makes use of Dependency Injection much simpler and thus testing with mocks straightforward.
WebForms vs ASP.NET MVC
Microsofts .NET has provided the ability to create Web Applications since 2002 in the form of WebForms. WebForms has be used by many organisations for Web Sites both large and small. However, whilst WebForms has served many organisations well, it does have a number of weaknesses. WebForms is file based (.aspx) and has an 'interesting' page lifecycle which can potentially introduce confusing behaviour and potentially make debugging tricky.
ASP.NET MVC has evolved rapidly over its relatively short lifetime, but the underlying Model View Controller (MVC) architecture has remained the same. The separation of concerns provided by MVC allows testing to be performed in a much easier way than with WebForms. Essentially Unit Testing with the use of Mocks can be used with MVC Web Applications. ASP.NET MVC affords many additional benefits over WebForms Web Applications as its more 'modular' approach also helps team working and isolation of functionality.
If a new .NET Web Application is needed then there should not be any contest as to which approach to use and MVC is definitely the way to go. On the other hand if an organisation already has an existing WebForms application, unless it needs a rewrite the application can continue to be supported and enhanced. However, many benefits could be gained by creating a new (replacement) application using MVC including, maintainability, testability, flexibility, team working and efficiency. Whilst there would potentially be a lot of work involved in a rewrite, if an existing WebForms application already made use of domain models for its data and/or a separate data access layer, then there is a good chance some of this could be reused.
For those familiar WebForms wondering if they can do 'everything' with MVC that they could do with WebForms, 'mostly' yes! Some of these features are illustrated below:
WebForms MVC 'equivalent'
ASPX page Controller Action with View
User Control ViewComponent with Partial View
Server Control HTML/Tag Helpers or Partial View
Master Page Layout View
Error Page Error View
ASP.NET Core 1 takes MVC further, with support for multiple platforms (Windows, Linux and iOS); 'middleware'; integrated Dependency Injection (making testing even easier) and merging of Web API (REST services). Whilst the core architecture is the same, namely MVC, there are many subtle and not so subtle differences from earlier versions of ASP.NET MVC.
MVC (in one of its forms) is certainly the way forward and ASP.NET Core 1 has improved on previous version of ASP.NET MVC.
For more more information about our range of courses: