Sunday, December 26, 2010

Moving to ASP.NET MVC

I was a long time PHP guy. So, I had grown up with a thought that PHP Language is server thing and has nothing to do with HTML. That is actually great. But in ASP.NET, that doesn’t really fits all that well. From now on, I will refer ASP.NET as ASP.NET Web Forms. Web Forms were developed keeping in mind to hide to complexity of HTTP from developer having “VB6” Windows Forms background. HTTP is stateless. Web Forms has tried hard to make HTTP state full, which brings up ViewState, Post backs and Page Life Cycle. ViewState is chunk of encoded response state data that is injected into every page so that on the next request previous state can be accessed. That was a gift for Windows Forms developers to start Web Development without actually learning the core HTTP cycle. I would also like to admit here that Web Forms is very mature, it doesn’t matter you use Drag-N-Drop approach to make pages, but you can still have full control over the output. But there are still few things that really freaks me out like ViewState Size, allow only one form tag on a page, page and control events, difficulty in client side scripting because of lack of control over HTML etc.

Well, I had work with Web Forms on few projects like DVD Store, Shopping carts, Ticket system, Appointment system, Feedback gathering system etc. At the start of every project, I fall into loop of choosing the best architecture for application. It was devastating for me. After few projects, I ended up with something like below screen capture and promise myself to strict to this.






I am showing this up here because this is what I thought best approach to SoC (Separation of Concerns) in your web forms application. But when I started understanding MVC architecture, I was thinking that this was something I have tried to do Web Forms. But it is better not to reinvent the wheel. More I spend time working with MVC, more I getting convinced to use it in my next project. Things were pretty much straight forward and everything I need is there. See below screen capture of default folder structure of ASP.NET MVC Application.



There are three main folders which are highlighted in image: Controllers, Models and Views. This explains the whole MVC thing. Controllers are those which compile the request the pass the control to requested View with data from Model. Models consist of data entity classes. Views are pages which will be returned to User. See the below image which explain what I am trying to say.



Benefits of MVC over Web Forms:
  1. No ViewState, Postback and page events etc.
  2. Separation of Concerns (SoC)
  3. Full control over HTML
  4. Ease of using Javascript Frameworks like jQuery etc.
  5. Testability (TDD)
  6. RESTful approach

3 comments:

  1. Thanks for this article charandeep .

    , Ayush

    ReplyDelete
  2. Well, this article clear the concept what MVC could be but still need more information, hopefully we will update together. Bunch of Thanks.

    ReplyDelete
  3. Good job buddy! You should post more often.

    ReplyDelete