Creating 3-Tier Architecture
Creating 3-Tier Architecture
Creating 3-Tier Architecture
Architecture
Software Architecture
2-Tier
Web
Services
3-Tier
Architecture
Good Software!
ClientServer
APIs
n-Tier
3-tier/layer Architecture
Presentation Tier
PL
Data
Source
DAL
Data Tier - DL
Modularized
http://en.wikipedia.org/wiki/Multitier_architecture
3-tier/layer Architecture
Presentation Tier
This is the topmost level of the application.
The presentation tier displays information related to such services as browsing
merchandise, purchasing and shopping cart contents.
It communicates with other tiers by which it puts out the results to the
browser/client tier and all other tiers in the network.
In simple terms it is a layer which users can access directly such as a web page,
or an operating systems GUI
Application tier (business logic, logic tier, data access tier, or middle tier)
The logical tier is pulled out from the presentation tier and, as its own layer.
It controls an applications functionality by performing detailed processing.
Data tier
This tier consists of database servers. Here information is stored and retrieved.
This tier keeps data neutral and independent from application servers or
business logic.
Giving data its own tier also improves scalability and performance.
http://en.wikipedia.org/wiki/Multitier_architecture
3-tier Architecture
Presentation Tier
Presentation Tier
Presentation Tier
Logic Tier
Data Access Tier
Stored Procedures
Database
Data Tier
3-tier Architecture
Presentation Tier
Presentation Tier
Presentation Tier
Logic Tier
Data Access Tier
Stored Procedures
Database
Data Tier
Client-side
Server(s)
Web
Server
Presentation
Tier
Web
Services
Business/Data
Logic Tier
Stored
Procedures
Data
Source
Data
Tier
Web
Server
Web
Services
Business/Dat
a Logic Tier
Stored
Procedures
Data
Source
Data
Tier
TFS Cient
Presentation
Tier
Client
Client
Client
Internet
Presentation Layer
Firewall
Presentation Layer
Presentation Layer
Client
Client
Web Service
Presentation Layer
Server
Presentation Layer
Presentation Layer
Stored
Database Procedures
Web
Server
Server
Business Logic
Exercises
1. Create Data Tier (Database)
2. Create Logic Tier (Database Communication Logic)
Create Presentation Tier (User Interface Logic):
3. WebApp: Using ASP.NET Web Forms (WS normally not needed)
4. Desktop App: Using WinForms
A. Without Web Services (We assume the App will be used
only in the LAN and that we have direct access to the
Database)
B. With Web Services (We assume the App should be used
on Internet outside the Firewall without direct DB access)
Web App
Firewall
WinForms
API
Presentation Tier
Clients
API
API
Database
Server
Web Server
Web Service
Client
Client
Client
Client
Local
Network
API
Business Tier
Logic Tier
Database
Stored Procedures
Views
Tables
Data Tier
3-tier
Architecture
Scenarios
Note! The different Tiers
can be on the same
Computer (Logic Layers) or
on different Computers in a
network (Physical Layers)
15
Data Tier
We are going to create the Database / Data Layer/Tier,
including:
1. Tables
2. Views
Note! Install them
in this order
3. Stored Procedures
4. Triggers
5. Script for some Dummy Data
Download Zip Files with Tables, Views, Stored Procedures
and Triggerse in order to create the Data Tier in SQL Server
(The ZIP File is located on the same place as this File)
16
Data Tier
Triggers
Stored Procedures
Views
Tables
Data Tier
SQL Server
17
Database Tables
18
19
20
WinForms
Presentation Tier
Presentation Tier
Logic Tier
Data Tier
Purpose:
All the Apps should/could
share the same Logic Tier
To make your Apps easier
to maintain and extend
etc.
Database
21
22
LogicTier
23
StudentData.cs
24
25
26
Code (StudentData.cs):
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
namespace Tuc.School.LogicTier
{
public class StudentData
{
public DataSet GetStudentDB(string connectionString)
{
string selectSQL = "select StudentName, StudentNumber, SchoolName, ClassName,
Grade from StudentData order by StudentName";
27
29
Presentation Layer
Web App: ASP.NET WebForms
We will create a WebForm like this where the data comes from our Logic Tier
30
31
32
33
System;
System.Collections.Generic;
System.Linq;
System.Web;
using System.Data;
using Tuc.School.LogicTier;
namespace Tuc.School.WebApp
{
public class Student
{
public DataSet GetStudent(string connectionString)
{
StudentData studentData = new StudentData();
return studentData.GetStudentDB(connectionString);
}
}
}
34
35
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Student Information</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Student Information</h1>
</div>
<asp:GridView ID="gridStudentData" runat="server">
</asp:GridView>
</form>
</body>
</html>
37
Note!
namespace WebApp
{
public partial class StudentInformation : System.Web.UI.Page
{
Web.Config
38
<configuration>
<connectionStrings>
<add name="SCHOOLConnectionString" connectionString="Data Source=macwin8;Initial
Catalog=SCHOOL;Persist Security Info=True;User ID=sa;Password=xxxxxx"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
Then you can easly switch Databsse without changing the Code!!
39
Note! We have used a View in order to get data from several tables
40
Additional
Exercise:
41
42
Presentation Layer
Desktop App: WinForms
Part A: Without Web Services (we assume the App will be used only in the local LAN (or
local on the same computer where the database is located) and that we have direct access
to the Database)
Label
DataGridView
43
44
45
46
using Tuc.School.LogicTier;
namespace Tuc.School.WinFormApp
{
class StudentWinForm
{
public DataSet GetStudent(string connectionString)
{
StudentData studentData = new StudentData();
return studentData.GetStudentDB(connectionString);
}
}
}
Create Form
Label
DataGridView
48
49
WinForm Code
using System.Configuration;
using Tuc.School.WinFormApp;
Note!
namespace WinFormApp
{
public partial class Form1 : Form
{
ConnectionString is
stored in App.config
50
51
52
Test it
It works!!!
53
54
Presentation Layer
Desktop App: WinForms
Part B: Using Web Services (we assume the The App should be used on Internet outside
the Firewall)
Label
DataGridView
55
SchoolWS
SchoolWS.asmx
Add Web Service:
56
Database ConnectionString
is located in Web.config
58
It Works!!
59
60
61
Test if WS working:
http://localhost/SchoolWS
62
WinFormAppWSClient
63
64
Create GUI
Label
DataGridView
Create Code
66
WinForm Code
using System.Windows.Forms;
namespace WinFormAppWSClient
{
public partial class FormWSClient : Form
{
public FormWSClient()
{
InitializeComponent();
}
private void FormWSClient_Load(object sender, EventArgs e)
{
FillStudentGrid();
}
private void FillStudentGrid()
{
Fill GridView
67
Test it:
It works!!!
68
69