MVC, Model, View and Controller ISYS 475. The three components in a database application 1. Presentation – user interface Menus, forms, reports, etc 2.

Slides:



Advertisements
Similar presentations
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user.
Advertisements

Authenticating Users in an ASP.NET Application. Web Site Administration Tool From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration.
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
Coding ADO.NET Objects: Connection, Command, DataReader.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows.
Web Site Security ISYS 512/812. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows:
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
Introduction to Web Application Development with.Net and Web Service ISYS 350.
Coding ADO.NET Objects: Connection, Command, DataReader.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Website Security ISYS 512. Cookies Data in Cookies System.Web Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
Web Application Architecture: multi-tier (2-tier, 3-tier) & mvc
UNIT-V The MVC architecture and Struts Framework.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
® IBM Software Group © 2006 IBM Corporation Creating JSF/EGL Template Pages This section describes how to create.JTPL (Java Template Pages) using the Page.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
PHP and AJAX ISYS 475. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net,
JavaScript & jQuery the missing manual Chapter 11
Website Security ISYS 475. Authentication Authentication is the process that determines the identity of a user.
Copyright © cs-tutorial.com. Introduction to Web Development In 1990 and 1991,Tim Berners-Lee created the World Wide Web at the European Laboratory for.
Ch6:creating consistent looking web sites. Master pages Master page defines a combination of fixed content and content place holder to hold the web page(.aspx)
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
INSPIRING CREATIVE AND INNOVATIVE MINDS Module 4: Adding Code to a Microsoft ASP.NET Web Form Implementing Code-Behind Pages Adding Event Procedures to.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
Database Application Security Models Database Application Security Models 1.
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
Class 1Intro to Databases Goals of this class Understand the architecture behind web database applications Gain a basic understanding of what relational.
SQL Queries Relational database and SQL MySQL LAMP SQL queries A MySQL Tutorial and applications Database Building Assignment.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Technology & Management Club Development Software Overview.
ASP.NET - accessing the database - datareader vs dataset - datagrid vs datalist - brief look at web matrix.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Unit 3: Adding Code to a Microsoft ASP.NET Web Form.
ASP.NET The Clock Project. The ASP.NET Clock Project The ASP.NET Clock Project is the topic of Chapter 23. By completing the clock project, you will learn.
Introducing ASP.NET 2.0. Internet Technologies WWW Architecture Web Server Client Server Request Response Network HTTP TCP/IP PC/Mac/Unix + Browser (IE,
HTML Basics BCIS 3680 Enterprise Programming. Web Client/Server Architecture 2  Your browser (the client) requests a Web page from a remote computer.
Database Handling Classes ISYS 475. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
ASP.Net and HTML. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data.
Command Object’s ExecuteNonQuery Method ISYS 512.
Storing and Retrieving Data
Model View Controller MVC Web Software Architecture.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
Coding ADO.NET Objects: Connection, Command, DataReader.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
PHP Form Processing * referenced from
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
Introduction to Sequential Files. COBOL's forte  COBOL is generally used in situations where the volume of data to be processed is large.  These systems.
Coding ADO.NET Objects: Connection, Command, DataReader.
ASP.NET and ADO.NET.
Developing a Model-View-Controller Component for Joomla
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
ASP.NET and ADO.NET.
Working with Session and Application Objects
Command Object’s ExecuteNonQuery Method
Presentation transcript:

MVC, Model, View and Controller ISYS 475

The three components in a database application 1. Presentation – user interface Menus, forms, reports, etc 2. Processing logic Business rules 3. 3.Data Access logic

The Model, View AND Controller, MVC Design Pattern The MVC design is a way of breaking an application into three parts: the model, the view, and the controller. – Input --> Processing --> Output – Controller --> Model --> View

Model The model represents enterprise data and the business rules that govern access to and updates of this data. The model manages the behavior and data of the application domain, responds to requests for information about its state and responds to instructions to change state.

View A view is some form of visualization of the state of the model that may generate HTML or PDF output. The view renders the contents of a model. It accesses enterprise data through the model and specifies how that data should be presented.

Controller A controller is the means by which the user interacts with the application. A controller accepts input from the user and instructs the model and view to perform actions based on that input. The controller is responsible for mapping end- user action to application response. The controller is the piece that manages user interaction with the model. It provides the mechanism by which changes are made to the state of the model.

Concepts related to MVC Program/Data Independence DRY, Don’t Repeat Yourself Separation of presentation (interface) and processing logic

Program-data dependence When file structure changed, all programs that access the file must be modified to conform to the new file structure.

A COBOL Program Example IDENTIFICATION DIVISION. AUTHOR. Michael Coughlan. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT StudentFile ASSIGN TO "STUDENTS.DAT“ ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD StudentFile. 01 StudentDetails. 02 StudentId PIC 9(7). 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 DateOfBirth. 03 YOBirth PIC 9(4). 03 MOBirth PIC 9(2). 03 DOBirth PIC 9(2). 02 CourseCode PIC X(4). 02 Gender PIC X. PROCEDURE DIVISION. Begin. OPEN OUTPUT StudentFile DISPLAY "Enter student details using template below. Enter no data to end." PERFORM GetStudentDetails PERFORM UNTIL StudentDetails = SPACES WRITE StudentDetails PERFORM GetStudentDetails END-PERFORM CLOSE StudentFile STOP RUN.

Program-data independence demo Microsoft Asp.Net data binding protected void Page_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); GridView1.DataSource = objDataReader; GridView1.DataBind(); }

This program does not support program/data independence <?php try { $db = new PDO('mysql:host=localhost;dbname=salesdb', 'root', ''); $query="select * from customers"; $customers = $db->query($query); echo $customers->rowCount(). " rows returned."; echo " ". " CID ". " CName ". " City ". " Rating "; while ($customer = $customers->fetch()){ $cid=$customer["cid"]; $Cname=$customer["cname"]; $City=$customer["city"]; $Rating=$customer["rating"]; echo " $cid ". " $Cname ". " $City ". " $Rating "; } echo " "; } catch (Exception $e) { $error_message = $e->getMessage(); echo " Error message: $error_message "; } ?>

Supporting Program/Data Independence <?php try { $db = new PDO('mysql:host=localhost;dbname=salesdb', 'root', ''); $query="select * from customers"; $customers = $db->query($query); $customers->setFetchMode(PDO::FETCH_ASSOC); $AllCustomers=$customers->fetchAll(); if (count($AllCustomers)==0) echo "No record selected"; else { echo " "; foreach ($AllCustomers[0] as $field=>$value) { echo " $field "; } echo " "; foreach ($AllCustomers as $customer) { echo " "; foreach ($customer as $field=>$value) { echo " $value "; } } echo " "; } catch (Exception $e) { $error_message = $e->getMessage(); echo " Error message: $error_message "; } ?>

DRY, Don’t Repeat Yourself DRY is a principle of software development aimed at reducing repetition of information of all kinds, especially useful in multi-tier architectures. Also known as Single Source of Truth, this philosophy is prevalent in model-driven architectures, in which software artifacts are derived from a central object model. The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."

Some Influences of DRY 1. Using template: – Example, a method to show records of a table. 2. Server-side include: – PHP require, include – Example: applying the same header and footer to all pages.

Example of Using Template <?php function showTable($tableName) { $db = new PDO('mysql:host=localhost;dbname=salesdb', 'root', ''); $query="select * from $tableName"; $rows = $db->query($query); $rows->setFetchMode(PDO::FETCH_ASSOC); $AllRows=$rows->fetchAll(); if (count($AllRows)==0) echo "No record selected"; else { echo "Records of the $tableName table "; echo " "; foreach ($AllRows[0] as $field=>$value) { echo " $field "; } echo " "; foreach ($AllRows as $row) { echo " "; foreach ($row as $field=>$value) { echo " $value "; } } echo " "; } showTable("customers"); showTable("products"); ?>

Example of Server-side include:Header/Footer Assuming every page of your website follows the same design theme for page header and footer. – 1. Create a file called header.php. In this file you will hold all of your page design that comes before your content. – 2.Next make a file called footer.php. This file will contain all of the site design information that goes below the content. – 3.Finally create the content pages for your site. In this file you will: 1.Call the header file 2.Put in the page specific content 3.Call the footer file

Header.php (A PHP file) Welcome to My Site My Site menu goes here Link 1 | Link 2 | Link 3

footer.php (a PHP file) Copyright 2013 My Site

Content.php (PHP web page) This is the first page Here is the specific content of this page....

One Implementation of the MVC Pattern

Page Controller Example: Separation of presentation from processing logic ASP.Net webform – In ASP.NET pages, the user interface programming is divided into two distinct pieces: The visual element is called the Web Forms page. The page consists of a file containing static HTML or ASP.NET server controls, or both simultaneously. The logic for the Web Forms page consists of code that you create to interact with the form. The programming logic resides in a file that is separate from the user interface file. This file, referred to as the code-behind file. – The controller manages events. Benefit: the application logic is not combined with the low-level code that manages the event dispatching and is easy to understand.

Example

Webform Page Enter name:

Code-behind file protected void Button1_Click(object sender, EventArgs e) { Label1.Text="Hello, " + TextBox1.Text + "!"; }

PHP Page Controller HTML page to present the view The mapping of the request to the file is performed by the web server PHP script implement the logic: – <?php – echo ' Hello, ', $_GET['name'], ' '; – ?>

Front Controller The Front Controller provides a centralized entry point for handling requests.

MVC Demo

Entry point: index.php – Define a controller object – Every actions first comes to this page Model: – Customer class, Order class View: Based on the model Controller: determine the view to display

An Example of Generating Entity Class from Database Schema

MySQL Information_Schema table mns-table.html mns-table.html Example: To select the columns from the customers table of the salesdb database: mysql> select column_name -> from information_schema.columns -> where table_name='customers' and table_schema='salesdb'; | column_name | | cid | | cname | | rating | | city | rows in set (0.02 sec)

Use PHP fopen to Create a Class (text) File fopen: The fopen() function opens a file or URL. fopen(filename,mode) – Filename: Specifies the file or URL to open – Mode: "r" (Read only. Starts at the beginning of the file) "w" (Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist) fwrite(resource $handle, string $string): writes the contents of string to the file stream pointed to by handle. fclose(resource $handle): Closes an open file.

Example $f = fopen("file.txt", "w"); fwrite($f, "TEXT TO WRITE"); fclose($f);

baseClass.php: A class with all field names retrieved from mySQL information Schema. class baseClass { public $fieldNames; function __construct() { $db = new PDO('mysql:host=localhost;dbname=salesdb', 'root', ''); $query="select column_name from information_schema.columns where table_name='customers' and table_schema='salesdb';"; $results = $db->query($query); $counter=0; //Generate a class with field names as properties $f = fopen("customerBase.php", "w"); fwrite($f,"<?php require 'baseClass.php'; class customerBase extends baseClass {"); while ($record = $results->fetch()) { $this->fieldNames[$counter]= $record[0]; fwrite($f, "public $$record[0]; "); ++$counter; } fwrite($f, "} ?>"); fclose($f); }

Generated customerBase.php <?php require 'baseClass.php'; class customerBase extends baseClass { public $cid; public $cname; public $rating; public $city; } ?>

Customer Class require 'customerBase.php'; class customer extends customerBase { public $fieldValues; public function getCustomerData($searchID){ $dsn = 'mysql:host=localhost;dbname=salesdb'; $username = 'root'; $password = ''; $db = new PDO($dsn, $username, $password); $query = "SELECT * FROM customers WHERE CID='". $searchID. "'"; $customers = $db->query($query); $customers->setFetchMode(PDO::FETCH_NUM); return ($this->fieldValues=$customers->fetch()); } public function addNewCustomer(){ $dsn = 'mysql:host=localhost;dbname=salesdb'; $username = 'root'; $password = ''; $db = new PDO($dsn, $username, $password); $queryINS = "insert into customers value("; for($i=0;$i fieldNames)-1;$i++) { $queryINS=$queryINS. "?,"; } $queryINS=$queryINS. "?)"; $stmt=$db->prepare($queryINS); return ($stmt->execute($this->fieldValues)) ; }

Using Customer Class <?php require 'customer.php'; $mycust=new customer(); $mycust->getCustomerData("C1"); var_dump ($mycust->fieldValues); var_dump($mycust->fieldNames); $mycust->fieldValues[0]="X"; $mycust->fieldValues[1]="X"; $mycust->fieldValues[2]="X"; $mycust->fieldValues[3]="X"; if ($mycust->addNewCustomer()) echo "adding successful!"; else echo "adding not succesful!"; ?>