C# .NET - MVC Architecture - Asked By s j on 16-Nov-11 06:17 AM



Hi,

I have read some articles about MVC Architecture  , but now i have to do code to implement MVC Architecture  in my website / project.

Need sample programs to work on this.
Plzz provide any links to get more on this.



Cheers,
Samantha.
Jitendra Faye replied to s j on 16-Nov-11 06:19 AM

The Model-View-Controller (MVC) pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes

  • Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).


  • View. The view manages the display of information.


  • Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.


  • Figure 1 depicts the structural relationship between the three objects.

    Ff649643.des_MVC_Fig01(en-us,PandP.10).gif

    Follow this link for more detail-
    http://msdn.microsoft.com/en-us/library/ff649643.aspx
    Hope this will help you.

    dipa ahuja replied to s j on 16-Nov-11 06:23 AM

    Model View Controller 

    Model view controller is nothing but a design pattern used to achieve customizability in our application. Change is the only thing in the world, which will never change. All the products that we develop to our clients will undergo many changes.

    Good Layering Approach

    MVC follows the most common approach of Layering. Layering is nothing but a logical split up of our code in to functions in different classes. This approach is well known and most accepted approach. The main advantage in this approach is re-usability of code. Good example of layering approach is UI, Business Logic, and Data Access layer


    Visual Inheritance in .Net

    Before getting in to Visual Inheritance, we will discuss the basic difference between Inheritance and Interfaces.

    http://www.dotnetspider.com/resources/316-Model-View-Controller-MVC-Architecture.aspx

    Suchit shah replied to s j on 16-Nov-11 06:24 AM
    This Hands-On Lab is based on MVC (Model View Controller) Music Store, a tutorial application that introduces and explains step-by-step how to use ASP.NET MVC and Visual Web Developer 2010 Express (which is free). Throughout the lab you will learn the simplicity, yet power of using these technologies together. You will start with a simple application and will build it until you have a fully functional MVC Web Application.

    Refer : http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3fundamentals
    http://www.c-sharpcorner.com/UploadFile/rmcochran/MVC_intro12122005162329PM/MVC_intro.aspx

    Hope this much help u to understand very well
    s j replied to Jitendra Faye on 16-Nov-11 06:24 AM


    Hi Vickey,

    I have read about this, Can you plz tell me the sample code to write in each module means, in model and in controller.
    And also connections to the database.


    Cheers,
    Samantha.
    Suchit shah replied to s j on 16-Nov-11 06:25 AM

    Browser Request (Step 1)

    Browser request happens with a specific URL. Let’s assume that the user enters URL like: [xyz.com]/home/index/

    Job of Global.asax – MVC routing (Step 2)

    The specified URL will first get parsed via application_start() method inside Global.asax file. From the requested URL, it will parse the Controller, Action and ID.

    So for [xyz.com]/home/index/:

    • Controller = home
    • Action = index()
    • ID = empty — we have not specified ID in [xyz.com]/home/index/, so it will consider as empty string

    Controller and Action methods (Step 3)

    MVC now finds the home controller class in controller directory. A controller class contains different action methods,

    There can be more than one action method, but MVC will only invoke the action method which has been parsed from the URL, its index() in our case.

    So something like: homeController.index() will happen inside MVC controller class.

    Invoking action method can return plain text string OR rendered HTML by using view.

    Call to View (Step 4)

    Invoking view will return view(). A call to view will access the particular ASPX page inside the view directory and generate the rendered HTML from the ASPX and will respond back to the browser.

    In our case, controller was home and action was index(). So calling view() will return a rendered HTML from the ASPX page located at /views/home/index.aspx.

    Jitendra Faye replied to s j on 16-Nov-11 06:46 AM
    Follow this link for example-

    http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
    Reena Jain replied to s j on 16-Nov-11 09:12 AM
    hi,

    The main aim of the MVC architecture  is to separate the business logic and application data from the presentation data to the user.
    
    Here are the reasons why we should use the MVC design pattern.
    
       1. They are resuable : When the problems recurs, there is no need to invent a new solution, we just have to follow the attern and adapt it as necessary.
       2. They are expressive: By using the MVC design pattern our application becomes more expressive.
    
    1).  Model: The model object knows about all the data that need to be displayed. It is model who is aware about all the
    operations that can be applied to transform that object. It only represents the data of an application. The model represents enterprise data and the business rules that govern access to and updates of this data. Model is not aware about the presentation data and how that data will be displayed to the browser.  
    
    2). View : The view represents the presentation of the application. The view object refers to the model. It uses
    the query methods of the model to obtain the contents and renders it. The view is not dependent on the application
    logic. It remains same if there is any modification in the business logic. In other words, we can say that it is the
    responsibility of the of the view's to maintain the consistency in its presentation when the model changes.
    
    3). Controller:  Whenever the user sends a request for something then it always go through the controller. The
    controller is responsible for intercepting the requests from view and passes it to the model for the appropriate action.
    After the action has been taken on the data, the controller is responsible for directing the appropriate view to the
    user. In  GUIs, the views and the controllers often work very closely together.
    
    Hope this will help you