Sunday 26 June 2016

What is MVC ? why we use MVC ? comparison with other architectures


What is MVC …? And why MVC …? Its large topic to explain that why is MVC but for I will explain that what is MVC. It’s just three layer architecture M stand for MODEL V stand for VIEW and most important in this architecture is CONTROLLER like a Hero of any film. So every layer in MVC is assigned with the unique responsibility so the view is for look and feel as well as positioning and the end user will actually see. The model supply data and business logic so the models are nothing it’s just a class like employee , student etc. and model can interact with data access layer and some kind of services like WCF service or web service which gives data. And the controller which is actually the heart of MVC and as I mention above like a film Hero deals with both layer that’s why controller also called the coordinator between model and view.







In MVC if user sends request from the web browser its first come to the controller then controller send to the appropriate view if and data request from the user then controller go to the model class because model always carry data as already mention. If we change in one layer then all others layer not change this advantage makes MVC perfect. Here the second diagram explain one controller folder with the extension of files with the controller also have Model folder in which have your customers class supplier




                                                           

When end User sends action like Add Customer, update customer ,go to home or whatever then its first comes to the controller if controller says ok let me search for appropriate model for it and the return it to the View. In the third diagram here view layer can be a aspx view or Razor view engine view which is mostly used in MVC architecture. After searching appropriate model then go to the controller then its controller responsibility to send information which comes from model send to the View which can be Razor view or aspx view these all the advantages of three layer architecture user now have more flexibility.

Now talk about why MVC? Why MVC is more reliable then other’s like web forms. I explain you why only MVC not web forms lets have some background approaches of ASP.NET this pic explain more scenario of the difference between MVC and Web forms let have a view this




Let’s start about window background about Microsoft at the start Microsoft introduce him with the word visual and Microsoft success mantra visual, visual and visual so Microsoft wanted to same success story like window used in programming at the time a lot of languages like COBOL, C++, DB’S these all languages not have any Visual effect so if want one to fix a button in c++ actually he write the code for it. So what Microsoft did they ventured something called as visual programming or RAD programing (Rapid App development)  so the set that for this rather than programmer code for this for creating a window for creating a window Microsoft introduce tool box. That is why they launched visual studio and again in programming language Microsoft got success at that time power builder was completely replace by VB visual programming as same VC++ replace C++ and VF (visual FoxPro) almost killed and it replaces DB so Microsoft got success this all these languages Microsoft invent a big umbrella that is a compiler that is .NET Web Forms view is mostly based on RAD and just drag and drop view.




The RAD programming is great but it leads to some issues the issue is behind code so in other word when programmers drag and drop than code generated in partial classes lets discus some drag of this RAD programming:
Problem no 1: view Based architecture for an Action Based requirement.
If user sends action what happened it the page life cycle life cycle is just call a complex structure the page load the page in and you know other what other kind of life cycles get executed and then run the required event now it’s a really complicated mean what should happened this a required event not run others events.so what the happened in this scenario when the end user send request as action its then go to the View first again run complicated life cycle. In other words to reach necessary you go the complicated life cycle logical approach should be to action directory that means once the user sends the action request it should actually map to a method inside the program the method get invoked runs all the necessary in logic for the action and then its invoked the required view and that would actually happened in the MVC.
Problem no 2: Behind code is not reusable..
So the connected problem for selecting a bad architecture was that the behind code was not reusable so the RAD architecture is not use able but in MVC we can code reusable again and again.
Problem no 3: Html is not the only response type.
   Mostly the web pages is on HTML but in case of xml if your application is communicating with languages like JavaScript and probably its sends it to Jason than you would like to invoke the action but the action can have different kind of response type depending on situation
Problem no 4: Flexible combination of view+data
So Web Forms is a View based architecture always view have been decided but in case of MVC what happened is its first hits to controller so the possibility to combining to the is more in case of MVC but not in case of Web Forms why because the view always fix first and also first hit in the view in web forms you can in the case of web forms but it will more complicated.
Problem no 5: Behind Code
We have easy way of unit test in MVC is the option of Unit test after creating new project but in web it would quite difficult to handle.
So the solution of these problem is the simply structures just MVC in this diagram first line shows RAD or WEB FORMS architecture in which will be in the form of aspx which is divided in to two parts one is view and other is behind code which called the middle layer you know which has the business logics and this logics calls the DAL data access layer the problem in which is that the first it come in the view But in MVC its behind code is controller and first receive the request and then sends to the view and model as mention in diagram.