Web Application Frameworks - MVC ?· Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March…

  • Published on
    07-Nov-2018

  • View
    212

  • Download
    0

Embed Size (px)

Transcript

  • Web Application Frameworks - MVCMMIS 2 VU SS 2011 - 707.025

    Denis Helic

    KMI, TU Graz

    March 24, 2011

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 1 / 57

  • Separation of Concerns

    Core design pattern of all software engineering

    Easily supported by object-oriented software development

    Definition by Edsger Dijkstra

    We know that a program must be correct and we can study it from thatviewpoint only; we also know that is should be efficient and we can studyits efficiency on another day [...] But nothing is gained - on the contrary -by tackling these various aspects simultaneously. It is what I sometimeshave called the separation of concerns [...]On the role of scientific thought

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 2 / 57

  • Separation of Concerns

    You want to isolate different aspects of a software application fromeach other

    You can work on each aspect in detailsYou can be consistent within each aspectAt a single moment you work on a single aspect (e.g., you are notdistracted with other aspects)Also, a basis for the team work (e.g., different teams work on differentaspects)

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 3 / 57

  • Separation of Concerns

    Divide-and-conquer method of designing algorithms

    Applied to developing software applications

    OO principles easily support SOC

    Classes, objects, encapsulation isolate one aspect from anotherIf interfaces are clearly defined you can work on different aspects inisolation

    Architectural and design patterns are also used to achieve SOC

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 4 / 57

  • Benefits of SOC

    Improved comprehension of the application domain

    Reduced complexity

    Component (aspect) integration

    Reuse

    Adaptibility, customization (through component exchange)

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 5 / 57

  • User-oriented database applications - SOC

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 6 / 57

  • User-oriented database applications - SOC

    The interface between UI and Data Management is crucial!

    It is where the SOC is violated most

    Why is this so?

    An example: You have a database containing info about the studentsregistered for a course

    This info is composed of name, matrikel number and study field

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 7 / 57

  • User-oriented database applications - SOC

    In your process logic you have a Student class

    The Student class has getter and setter methodsgetName(), getStudyField(), etc.

    In your UI script (e.g., a PHP script) you retrieve a list of Studentobjects

    You iterate through the list and use the getter methods to write info inan HTML table

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 8 / 57

  • User-oriented database applications - SOC

    Your customer tells you that there is another external studentdatabase

    It contains contact info such as student addresses, e-mails, etc.

    You customer wants to have students e-mail addresses in the list ofthe registered students

    You are too lazy and want to accomplish it fast

    In the UI script you use the matrikel number of each studentYou connect to the external database and retrieve the needed info

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 9 / 57

  • User-oriented database applications - SOC

    You mix UI and data management

    Consequences can be tremendous: different UI scripts need to beupdated, maintained, etc.

    Proper way of implementing such changes

    Modify the Student class and extend it with getEMail(), setEMail()In the DM module connect to the external database and populatestudent objects with setEMail()

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 10 / 57

  • User-oriented database applications - SOC

    SOC is supported by OO programming languages but it is notenforced!

    Developers need to take care about this!

    Scripting languages are even more vulnerable

    You do not need compiling, it is fast to make such changes!

    But, Java is vulnerable too, here you need to take care also!

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 11 / 57

  • User-oriented database applications - SOC

    One way to improve the situation: a layered architecture

    The UI communicates only with the PL module

    Again, hard to enforce this

    You can only hope that developers will follow the principle!

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 12 / 57

  • User-oriented database applications - SOC

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 13 / 57

  • Model-View-Controller

    Model-View-Controller is a particular design pattern that supportsSOC

    It was invented in the early days of GUIs

    To decouple the graphical interface from the application data and logicGUI is further separated into data presentation and users input

    Invented at Xerox Parc in the 70s

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 14 / 57

  • Model-View-Controller

    The first appereance in Smalltalk-80

    One of the first OO languagesPure OO language, i.e., everything is an object

    MVC invented by Trygve Reenskaug:http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 15 / 57

    http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html
  • Model-View-Controller

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 16 / 57

  • Model-View-Controller

    Controller

    Handles user input (e.g., mouse clicks, keyboard,...)Updates the modelInstructs the view to redraw itself

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 17 / 57

  • Model-View-Controller

    View

    Presents the model in a specific wayNote different views for the same modelVery important not only in GUIs but also in Web applications (e.g.,XHTML, PDF, etc.)

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 18 / 57

  • Model-View-Controller

    Model

    Contains the data and application logic

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 19 / 57

  • Model-View-Controller

    Very easily accomplished with an OO programming model

    Objects encapsulate the dataObjects implement behaviour (as methods)Interaction between different objects (i.e., invoking methods) supportsthe application logic

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 20 / 57

  • Model-View-Controller

    Where does the data come from?

    If in memory - everything is covered

    If in the file system or in a DBMS

    We need a special Data Management module

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 21 / 57

  • Model-View-Controller

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 22 / 57

  • Model-View-Controller

    A special case of the MVC uses Observer design pattern

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 23 / 57

  • Model-View-Controller

    When the model changes it notifies the views about the change

    All views redraw as the result of the notification

    Until Ajax not applicable in a Web application

    Page-oriented applicationsYou need a user request for each particular view (e.g., HTML, PDF,..)

    With Ajax it is possible (Asynchronous request)

    It improves the responsiveness of a Web app tremendously

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 24 / 57

  • Model-View-Controller

    The purpose of the controller is not to separate the model from theview

    E.g., in GUI applications the purpose of the controller is to handleuser events

    To achieve this separation another design pattern is needed

    Typically acheived by means of the Observer pattern

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 25 / 57

  • MVC Variations and Derivatives

    A number of further developments od the MVC pattern

    Model-View-Presenter

    Presentation-Abstraction-Control

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 26 / 57

  • Model-View-Presenter

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 27 / 57

  • Model-View-Presenter

    The Model refers to the data and business functionality of theapplication

    Selections are components which specify what portion of the datawithin the Model is to be operated upon

    Commands are components which define the operations which can beperformed on the data

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 28 / 57

  • Model-View-Presenter

    The View is the visual representation of the Model and is comprisedof the screens and widgets used within an application

    Interactors are components which address how user events aremapped onto operations performed on the Model

    The Presenter is a component which orchestrates the overallinteraction of the other components within the application

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 29 / 57

  • Model-View-Presenter

    Actually, model-view-presenter is how a typical GUI application isbuilt today

    Only selections and commands are separate components

    In a typical system selections and commands are defined throughinterfaces - such as DAO interfaces

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 30 / 57

  • Presentation-Abstraction-Control

    It is a hierarchical variation of MVC

    E.g. there is a hierarchy of sub-systems where each sub-systemfollows MVC pattern

    The links between sub-systems model associations within anapplication, e.g. similar to links on the Web

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 31 / 57

  • Presentation-Abstraction-Control

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 32 / 57

  • Presentation-Abstraction-Control

    The Presentation is the visual representation of a particularabstraction within the application

    The Abstraction is the business domain functionality within theapplication, i.e. it is the model

    The Control is a component which maintains consistency between theabstractions within the system and their presentation to the user inaddition to communicating with other Controls within the system

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 33 / 57

  • MVC on the server side

    The server recieves different HTTP requests

    Requests can include different parameters submitted by the userOn the basis of these parameters the server produces the responseThe server can dispatch the request to different handlers (actions)The server needs a registry of mappings of parameters onto actions

    The server, registry, dispatcher and actions are the Controller

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 34 / 57

  • MVC on the server side

    The Model is accessed from the actions

    It is data and behaviour encapsulated within objects

    Each action is associated with a View

    When the action finishes the Controller invokes the ViewThe View accesses the Model, retrieves (!) the data and present itThe registry includes also associations between actions and views

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 35 / 57

  • MVC on the server side

    In terms of MVP

    Registry contains interactors, i.e. mapping of user parameters ontoactionsDispatcher is the presenter that controls the workflowActions interact with commands and selectorsCommands and selectors might be model interfaces or special objectsthat manipulate the model

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 36 / 57

  • MVC on the server side

    In terms of PAC

    Each sub-system such as user management, administration, applicationlogic is modeled by a MVCThere are always links between sub-systemsAdministration tool is linked with user management moduleData management module is linked with user management module

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 37 / 57

  • Struts MVC

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 38 / 57

  • Ruby on Rails

    Ruby is a pure object-oriented programming language

    http://www.ruby-lang.org/en/

    Pure means everything is an object, e.g. the number 1 is an instanceof class Fixnum

    Interpreted scripting language

    Dynamically, weakly typed

    Single inheritance, but can be extended with so-called modules(similar to Java interfaces)

    Rich text processing functionality (similar to Perl)

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 39 / 57

    http://www.ruby-lang.org/en/
  • Ruby on Rails

    Rails is open source Web application framework

    http://www.rubyonrails.org/

    Supports development of database-backed application

    User-oriented Web database applications

    Follows MVC architecture and design pattern

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 40 / 57

    http://www.rubyonrails.org/
  • Ruby on Rails

    Three main guiding principles

    Model-driven (domain-driven) development

    You start with a data model and add the functionality, controllers,views on top of it

    Convention over configuration

    Set of naming conventions (similar to JavaBeans but more in depth)

    Less software, i.e., less code

    Generating default code that you adjust to fit your needs

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 41 / 57

  • Ruby on Rails

    Domain-driven development

    Based on an ORM framework called ActiveRecord

    http://wiki.rubyonrails.com/rails/pages/ActiveRecord

    ActiveRecord is a generic ORM framework

    Similar to HibernateUses a naming convention to provide the default mappingYou can adjust the default mapping if you need to

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 42 / 57

    http://wiki.rubyonrails.com/rails/pages/ActiveRecord
  • Ruby on Rails

    ActiveRecord naming convention

    http://ar.rubyonrails.com/classes/ActiveRecord/Base.html

    Names of classes and tables

    Give names to your classes as English singular, and to your tables asEnglish pluralStart the name of the class with an upper case, all other letters arelower case (Student)Table name is all lower case (students)

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 43 / 57

    http://ar.rubyonrails.com/classes/ActiveRecord/Base.html
  • Ruby on Rails

    Student class

    class Student < ActiveRecord::Base

    end

    Student table

    create table students (

    id int not null auto_increment,

    name varchar(80),

    study_field varchar(10),

    primary key(id)

    );

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 44 / 57

  • Ruby on Rails

    Names of the table columns and instance variables

    Map 1-to-1, i.e., student.name maps to name column in studentsPrimary key must be named id in the table

    Immediatelly you can use all methods from ActiveRecord

    @students = Student.find_all

    @student = Student.new

    Denis Helic (KMI, TU Graz) Web Application Frameworks - MVC March 24, 2011 45 / 57

  • Ruby on Rails

    Internally, ActiveRecord uses a single table to map the whole classhierarchy

    http://wiki.rubyonrails.com/rails/pages/

    SingleTabl...

Recommended

View more >