COMP2001
Information Management
and Retrieval
Dr Shirley Atkinson
Single Page Application 1. User navigates to web
address
2. Server responds with
HTML and multiple
• In traditional web applications user Javascript files containing
navigates between different pages client-side code
retrieved separately from server on 3. Browser gets files, displays
to user. User interacts (eg
request clicks button)
4. Javascript code handles
• SPA only has one page click, calls server side for
• Contains whole application data
5. Server receives request for
• Every UI change is therefore made data, retrieves it, sends it
using Javascript back
6. Javascript handles
response, generates HTML
Welcome modern
frameworks of Angular,
React, Viee etc..
Database Connectivity
Azure Data Studio
• So far – have used tools to view the database
• SQL is the language used to extract or
Presentation
manipulate data
• Data is residing in the data layer
• In the middle layer we see data logic, data
transformations and ”how” to talk to the Business
database
• So connectivity often referred to as “middleware”
Data
Connectivity options (not exhaustive)
• Native SQL Connectivity – provided by vendor
• ODBC, DAO and RDO
• Open Database Connectivity
• Data Access Objects
• Remote Data Objects
• OLE-DB – Object Linking and Embedding for Database
• ADO.NET
• PDO – PHP Data Objects
Database Connections
• Design decisions to be made
• Use of middleware
• API
• Direct link
• Context dependent
• Some data can be shared via API (more on that later)
• Some functions that use data need to run on the DB (stored
procedures) and results shared via API
• Some applications need a direct link
Simple PHP Example
• I am using PHPStorm (we now have access to IntelliJ products)
• I have an application where the database connectivity is in a
separate file dbFunctions.php
• My interface file index.php “includes” the database file
• The results must come back as an array or the php code cannot
process it
• Uses a PDO object
• Not MySQL library or mysqli
Stack in use = MySQL, PHP,
Apache
WAMP/MAMP/LAMP
Filename: index.php Filename: dbFunctions.php
<!DOCTYPE html> <?php
<?php
ini_set('display_errors', 1); function getAll($tablename){$sql = "SELECT *
require_once ‘dbFunctions.php’; FROM ".$tablename;
$con = getConnection();
if(isset($_POST['table’])) { $tableName = $run = $con->prepare($sql);
$_POST['table’]; }. ?> $run->execute();
<html> $results = $run-
…. >fetchAll(PDO::FETCH_ASSOC);
<form action="<?php echo return $results;
$_SERVER['PHP_SELF']; ?>" method="post"> }
<h1>Select Table</h1> function getConnection(){
… $mySQLConnection = new
<?php PDO("mysql:host=$domain;dbname=$db, $user,
if(isset($tableName)). {. $results = $password");
getAll($tableName); return $mySQLConnection;
}
if($results)
{ …..
Key points
• PDO = PHP Data Object is wrapped in one function to allow it to
be called from where-ever
• What would be the issue of writing this in the files where it would be
used?
• Connection is used to get data
• SQL statement prepared
• Connection used to execute statement
• Results retrieved and used in interface file
• BUT this is tightly coupled… What if we wanted to change our
database?
Development Environment
• Need to have a web-server running
• Visual Studio have built in web-servers
• Visual Studio does NOT work well with PHP
• There is a plug in but it costs money
• Other environments might need specific web-server to be
installed
• Eg: use of WAMPServer, Ampps (like we did in year 1)
Simple ASP.NET MVC Example
• Using Visual Studio to examine standard MVC application
• Connection String specifies the location and name for the database
• Plus configuration settings
• You need to make sure you have the ADO.NET connection strings for
a .NET application
• Connection strings should be one unbroken line
• Stored in JSON file called appsettings.json
• LocalDb– a free local installation of Microsoft SQL Server that
comes with Visual Studio.
• Means you can code locally
Stack in use = Microsoft SQL
• BUT you do need to set up for remote working later Server, IIS, ASP.NET Core
Reminders for PHP
• <?php … ?> - all code is held in tags
• $variable – all variables are indicated with $
• Line by line building up of HTML output interspersed with PHP
• Anything to be submitted to the server needs to be inside a
<form> tag
PHP : Setting up the connections
As you would expect, CONST is used for values that do not change during the course of
the script. These are automatically global.
What might happen if we just declared these as variables?
PHP : getConnection function
What might be an issue
PHP : getting the data here?
Because of how I want to manipulate the data – I am asking for it to be returned as an
associative array FETCH_ASSOC.
This gives me key value pairs to use as column names
ASP.NET and C#
• Creating a traditional web application to begin with
• SPA – Single Page Applications mentioned earlier are used when you
need a rich interface with many features
• SPA’s will rely heavily on the Javascript frameworks
• Create an MVC application based on the database you already
have
• Code First workflow
• Storing password details in appsettings.json is not that secure
for deployment and you might use the built in Secret Manager*
*Not expected for this
module
ASP.NET : Application
• Use NuGet to install Entity Framework. Use the Package
Manger Console.
• Install-Package Microsoft.EntityFrameworkCore
• POCO – Plain Old Class Object
• Must map to the data types for the columns Uses the repository
• Use getter and setter combined with unit of
work patterns
• Create a DbContext class
• Inherits from the DbContext base class
• Add in DbSet to represent the values from the tables
Note the pluralisations of the DbSets
University of Plymouth
Advancing knowledge, transforming lives