locked
MVC when to use,MVC framework RRS feed

  • Question

  • Hi,

    I need to know about MVC pattern.Is it the same what vs.net generates by default whenever we create a asp.net web application(generates a .aspx page and an .aspx.cs thus seperating model and view).

    If it is different , can somebody please let me know what exatly should be where.i read many article on MVC but didn't got any working sample to understand.

    Also need to know in what cases whould we use MVC pattern, as these days everbody talkes about that his application is based on MVC.

    Java has many MVC framewokrs.Do .net also have any such?

    regards

    Monday, August 21, 2006 11:38 AM

Answers

  • Model, View, Controller pattern is not the same as the code behind in visual studio. 

    MVC splits the user interface into 3 parts. The Model is the business entity, the View is the user interface and the Controller defines how the two interact and how each should update after the other changes. 

    For example, the user interface asks the Controller to alter property x.  The controller tells the model to change property x.  The model then instructs the view (through the controller), that the change could not accur because the value was invalid.

    This has many advantages including making it easier to automate testing as visual elements are difficult to test.

    Model

    The object model.  For example, in a stock control application, the 'Add Product' window will allow the user to create Product Objects.  The model and object are synonomous here. 

    View

    The design of the user interface (Forms, Controls etc).  This is the Form you define in the visual designer in Visual Studio.

    Controller

    This defines how the View and the Model intereact.  Normally, the user interface will inform the controller what action the user wants to perform, and the controller tells the model to do something.  The Controller then informs the View how to update itself.

    Sometimes, the View will subscribe to events on the model to allow it to decide how to update itself.  This is not a perfect implementation of MVC but is pragmatic.

    However, I have worked with object models that don't define enough events for changes it makes.  Then the Controller is required to tell the Interface how to update itself.

    This is a very simplified description but I hope this gives you a better idea.

     

    Tuesday, August 22, 2006 2:36 PM
  • Hi,

    I am afraid, i dont know the name of a pattern for code behind. It is definitely not MVC though.

    It lets you handle events and pass data back and forth between business logic / data layer to the view. This could implement the busines logic itself. The code behind may also manipulate view by accessing and manipulating the controls.

    Though - its lot better than the ASP pages where you had to do all the event wiring within.

    There are third party MVC implementations like Monorail, maverick etc. but I am not sure about how popular these are and whether these are out of Beta.

    The above links ( for MVP and MVC) tell you how to implement the same without using a different framework - by structuring your code such.

    Essentially, the aspx/ascx will become the view, the code behind will become the controller ( handling events) and the model will be objects which know how to fetch the data within them.

    The view also supports templatization ( like Tiles / Struts if comparing with Java world) using asp.net web parts and using master pages.

    Regards

    Pranshu

    Monday, August 21, 2006 1:45 PM

All replies

  • Hi Thompson.

    Unfortunately, I have not been able to find a simple answer for this.

    Start by reading this

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/DesMVC.asp

    Then read about UIP application block

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/uipab.asp

    and then about CAB (Composite UI Application Block) http://www.gotdotnet.com/codegallery/codegallery.aspx?id=22f72167-af95-44ce-a6ca-f2eafbf2653c

    Possibly also read about MVP pattern http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx

    And dont miss atlas.net if you want Ajax capabibilities as well.

    Also read about asp.net web parts.

    In my opinion UIP+Atlas.net+Asp.Net web parts - used together - helps you achieve all the objectives.

     

    Pranshu

    Monday, August 21, 2006 12:36 PM
  • Hi Pranshu,

    Thanks a lot for a lot of information. i will go through all these links one by one and will get back to you.

    meanwhile can you please let  me know what pattern is exactly followed by .net when it creates a .aspx file and corresponding .aspx.cs file.

    Also how can we use this structure in avoiding redundant code.

    regards

    Monday, August 21, 2006 1:08 PM
  • Hi,

    I am afraid, i dont know the name of a pattern for code behind. It is definitely not MVC though.

    It lets you handle events and pass data back and forth between business logic / data layer to the view. This could implement the busines logic itself. The code behind may also manipulate view by accessing and manipulating the controls.

    Though - its lot better than the ASP pages where you had to do all the event wiring within.

    There are third party MVC implementations like Monorail, maverick etc. but I am not sure about how popular these are and whether these are out of Beta.

    The above links ( for MVP and MVC) tell you how to implement the same without using a different framework - by structuring your code such.

    Essentially, the aspx/ascx will become the view, the code behind will become the controller ( handling events) and the model will be objects which know how to fetch the data within them.

    The view also supports templatization ( like Tiles / Struts if comparing with Java world) using asp.net web parts and using master pages.

    Regards

    Pranshu

    Monday, August 21, 2006 1:45 PM
  • Model, View, Controller pattern is not the same as the code behind in visual studio. 

    MVC splits the user interface into 3 parts. The Model is the business entity, the View is the user interface and the Controller defines how the two interact and how each should update after the other changes. 

    For example, the user interface asks the Controller to alter property x.  The controller tells the model to change property x.  The model then instructs the view (through the controller), that the change could not accur because the value was invalid.

    This has many advantages including making it easier to automate testing as visual elements are difficult to test.

    Model

    The object model.  For example, in a stock control application, the 'Add Product' window will allow the user to create Product Objects.  The model and object are synonomous here. 

    View

    The design of the user interface (Forms, Controls etc).  This is the Form you define in the visual designer in Visual Studio.

    Controller

    This defines how the View and the Model intereact.  Normally, the user interface will inform the controller what action the user wants to perform, and the controller tells the model to do something.  The Controller then informs the View how to update itself.

    Sometimes, the View will subscribe to events on the model to allow it to decide how to update itself.  This is not a perfect implementation of MVC but is pragmatic.

    However, I have worked with object models that don't define enough events for changes it makes.  Then the Controller is required to tell the Interface how to update itself.

    This is a very simplified description but I hope this gives you a better idea.

     

    Tuesday, August 22, 2006 2:36 PM
  • Hi All,

       a good enough explanation about MVC pattern is available in "Starting with Model/View/Controller (MVC) Architecture Pattern" at http://www.skyscrapr.net/blogs/solution/archive/2006/08/07/262.aspx (the host, SkyScrapr, is a site for aspiring architects)

       At the end, it suggests an excellent webcast telling how MVC is achievable from the basic Windows Forms generated as output by Visual Studio wizards

     

    Hope it helps

    Saturday, August 26, 2006 6:34 AM
  • an implementation of MVC in WPF :

    http://www.codeplex.com/WPFMVC

     

    Thursday, May 1, 2008 10:12 PM
  • Thursday, May 1, 2008 10:16 PM
  • Hi All,

    The default pattern used by asp.net web application is the Page Controller Pattern.

    Here's a link that gives a clear understanding of the Page Controller Patern and MVC

    http://codeasp.net/articles/asp-net/30/aspnet-mvc-framework-tutorial



    -Amit
    • Proposed as answer by Amit Natekar Tuesday, December 15, 2009 5:00 AM
    Tuesday, December 15, 2009 4:59 AM