The document discusses the Model-View-Controller (MVC) design pattern. MVC separates an application's logic into three main components: the model, the view, and the controller. The model manages the application's data and logic, the view displays the data to the user, and the controller interprets user input and updates the model. MVC improves separation of concerns and makes applications more modular, extensible, and testable. It is commonly used for web applications, where the server handles the model and controller logic while the client handles the view.
In this document
Powered by AI
MVC is a software design pattern for web applications, separating data representation from user interaction. It is one of the ASP.NET models.
First presented by Trygve Reenskaug in 1979, initially used in Smalltalk-80 framework for Apple interfaces.
Smalltalk's MVC influenced various GUI frameworks, including NEXTSTEP, Cocoa, MFC, Java Swing, and Qt Toolkit.
MVC consists of three parts: Model, View, and Controller, each fulfilling distinct roles in web applications.
The Model manages application data, responding to both view requests and controller instructions.
The View presents data from the Model based on requests, often displayed in HTML format in web applications.
The Controller handles user interactions, reads data from the View, controls input, and updates the Model.
User input initiates a cycle where the Controller updates the Model, which in turn informs the View for display.
MVC follows a dependency hierarchy to minimize dependencies, ensuring the Model is independent of View and Controller modifications.
Originally for desktop, MVC architecture adapts to web applications across programming languages with frameworks enabling model-view separation.
Detailing the typical operations of MVC within web applications and its reliance on user interactions.
MVC is a lightweight alternative to ASP.NET Web Forms, designed for high testability and integration with ASP.NET features.
Steps to create a new Project in Visual Web Developer, setting name and location for the MVC application.
Outline of the typical MVC project structure, including folders for properties, application data, controllers, and views.
Key benefits include clear separation of concerns, parallel development, enhanced maintenance, and independent object structures.
Challenges include increased complexity, potential inefficiencies, necessity for multiple technologies, and team collaboration requirements.
Example of the Observer pattern within MVC, allowing classes to maintain minimal knowledge about each other while notifying views.
Illustrating the interaction process among Controller, BankAccount, and Views, demonstrating the observer notifications in action.
Introduction
• Model ViewController or MVC as it is popularly called, is
a software design pattern for developing web
applications.
• MVC is one of three ASP.NET programming models.
• Model–view–controller (MVC) is a software
architecture pattern which separates the representation
of information from the user's interaction with it .
2.
History of MVC
•Presented by Trygve Reenskaug in 1979
• First used in the Smalltalk-80 framework
– Used in making Apple interfaces (Lisa and
Macintosh
3.
MVC uses
Smalltalk’s MVCimplementation inspired many
other GUI frameworks such as:
• The NEXTSTEP and OPENSTEP development
environments encourage the use of MVC.
• Cocoa and GNUstep, based on these
technologies, also use MVC.
• Microsoft Foundation Classes (MFC) (also called
Document/View architecture)
• Java Swing
• The Qt Toolkit (since Qt4 Release).
4.
Parts of MVC
•A Model View Controller pattern is made up of the
following three parts:
• Model
• View
• Controller
5.
The MVC modeldefines web
applications with 3 logic
layers:
The business layer (Model
logic)
The display layer (View logic)
The input control (Controller
logic)
7.
Model
• The modelis responsible for managing the data
of the application.
• It responds to the request from the view and it
also responds to instructions from the controller
to update itself
• It is the lowest level of the pattern which is
responsible for maintaining data.
• The Model represents the application core (for
instance a list of database records).
• It is also called the domain layer
8.
View
• The Viewdisplays the data (the database
records).
• A view requests information from the
model, that it needs to generate an output
representation.
• It presents data in a particular format like
JSP, ASP, PHP.
• MVC is often seen in web applications,
where the view is the HTML page.
9.
Controller
• The Controlleris the part of the
application that handles user interaction.
• Typically controllers read data from a
view, control user input, and send input
data to the model.
• It handles the input, typically user actions
and may invoke changes on the model
and view.
10.
Workflow in MVC
ThoughMVC comes in different flavours, the
control flow generally works as follows:
1. The user interacts with the user interface in
some way (e.g., user presses a button)
2. A controller handles the input event from the
user interface, often via a registered handler or
callback.
3. The controller accesses the model, possibly
updating it in a way appropriate to the user’s
action (e.g., controller updates user’s shopping
cart).
11.
4. A viewuses the model to generate an
appropriate user interface (e.g., view
produces a screen listing the shopping cart
contents).
The view gets its own data from the model.
The model has no direct knowledge of the
view.
5. The user interface waits for further user
interactions, which begins the cycle anew.
12.
Dependence hierarchy
• Thereis usually a kind of hierarchy in the
MVC pattern.
• The Model knows only about itself.
• That is, the source code of the Model has
no references to either the View or
Controller.
13.
• The Viewhowever, knows about the
Model. It will poll the Model about the
state, to know what to display.
• That way, the View can display something
that is based on what the Model has done.
• But the View knows nothing about the
Controller.
14.
• The Controllerknows about both the Model and
the View.
• Take an example from a game: If you click on
the "fire" button on the mouse, the Controller
knows what fire function in the Model to call.
• If you press the button for switching between
first and third person, the Controller knows what
function in the View to call to request the display
change.
15.
Why dependence hierarchyis
used?
• The reason to keep it this way is to
minimize dependencies.
• No matter how the View class is modified,
the Model will still work.
• Even if the system is moved from a
desktop operating system to a smart
phone, the Model can be moved with no
changes.
• But the View probably needs to be
updated, as will the Controller.
16.
Use in webapplications
• Although originally developed for personal
computing, Model View Controller has been
widely adapted as an architecture for World Wide
Web applications in all major programming
languages.
• Several commercial and
noncommercial application frameworks have been
created that enforce the pattern.
• These frameworks vary in their interpretations,
mainly in the way that the MVC responsibilities
are divided between the client and server
17.
• Early webMVC frameworks took a thin client approach
that placed almost the entire model, view and controller
logic on the server.
• In this approach, the client sends either
hyperlink requests or form input to the controller and
then receives a complete and updated web page from
the view; the model exists entirely on the server.
• As client technologies have matured, frameworks such
as JavaScript MVC and Backbone have been created
that allow the MVC components to execute partly on the
client
Web forms vs.MVC
• The MVC programming model is a lighter
alternative to traditional ASP.NET (Web
Forms).
• It is a lightweight, highly testable
framework, integrated with all existing
ASP.NET features, such as Master Pages,
Security, and Authentication.
20.
Creating the WebApplication
• If you have Visual Web Developer installed, start Visual Web Developer and
select New Project.
21.
Steps
• In theNew Project dialog box:
Open the Visual C# templates
Select the template ASP.NET MVC 3 Web Application
Set the project name to MvcDemo
Set the disk location to something
like c:example_demo
Click OK
• When the New Project Dialog Box opens:
Select the Internet Application template
Select the Razor Engine
Select HTML5 Markup
Click OK
Advantages
• Clear separationbetween presentation
logic and business logic.
• Each object in mvc have distinct
responsibilities.
• parallel development
• easy to maintain and future
enhancements
• All objects and classes are independent of
each other.
25.
Disadvantages
• Increased complexity
•Inefficiency of data access in view
• Difficulty of using MVC with modern user
interface too.
• For parallel development there is a
needed multiple programmers.
• Knowledge on multiple technologies is
required.
26.
Example
• The Observerpattern allows the
BankAccount class to notify multiple views
without minimal information.
• Observers can register themselves with
their Subjects. No strings attached!
27.
Observer Class Diagram
Observable Observer
+addObserver(Observer) +update(Observable,
+deleteObserver(Observer) Object)
+notifyObservers(Object)
#hasChanged() : boolean
#setChanged() AccountView
+update(Observable,
Object)
BankAccount
+widthdraw(double) : long
+deposit(double) : long SummaryView
+getBalance() : double +update(Observable,
Object)