Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK -...

22
Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-14 1 AK - IT Softwarekonstruktion

Transcript of Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK -...

Page 1: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Session 08: Architecture

Controllers or Managers

Graphical User Interface (GUI)

FEN 2013-04-14 1AK - IT Softwarekonstruktion

Page 2: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

N-tier (multi-tier) Architecture

WebServer

Browser

Client

Internet

Dedicated Client

• Database access layer: All code to access database is here. Makes it possible to change data store.

• Web server accesses application layer – not the database directly.

• Easier maintenance:• No business logic in the web

server (or other clients).• Application server: All (almost)

business logic is re-used.• New client may be added without

code duplication.

DB

DatabaseServer

Application Server

Database Access Layer

Backend

Mobile

Client

Client accessing web services

FEN 2013-04-14 2AK - IT Softwarekonstruktion

New Dedicated Client

Page 3: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Controller Object

• So far we have had our business logic and “user interface” in the Main method

• This obviously not a good design!• Normally one will used what is

known as a layered ( or n tier) architecture, for instance:– UI– Controller– Model– Database

FEN 2013-04-14 AK - IT Softwarekonstruktion 3

Application

For now we focus on the application

layer

Page 4: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

More Banking

View Code (Banking4)FEN 2013-04-14 AK - IT Softwarekonstruktion 4

Page 5: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Design

• This design is based on the fact that accounts normally are accessed from Customer. (Hence the complicated and inefficient implementation of GetAllOwners).

FEN 2013-04-14 AK - IT Softwarekonstruktion 5

Page 6: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Exercises

• Exercises 1- 3 on Session08.docx.

• Extra : 4 – 6 on Session08.docx

FEN 2013-04-14 AK - IT Softwarekonstruktion 6

Page 7: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Remember Banking4 ?

View Code (Banking4)FEN 2011-03-08 UCN T&B: IT Technology 7

Page 8: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Banking4 with GUI (Banking5)

View Code (Banking4GUI)FEN 2011-03-08 UCN T&B: IT Technology 8

Page 9: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

How to Create a GUI for an Existing Project

• Firstly you must have a working project with model and controller, for instance Banking4.

• First step is to create a new solution in Visual Studio:

FEN 2011-03-08 UCN T&B: IT Technology 9

It must be a “Windows Form Application”

Give it a good name.

And choose a location for it.

Page 10: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

How to Create a GUI for an Existing Project

FEN 2011-03-08 UCN T&B: IT Technology 10

New Form in VS

Toolbox. Here you find GUI components

Properties. Here you can edit components. For instance give the

form a good name

Page 11: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

How to Create a GUI for an Existing Project

• Right click on the solution. Choose ‘Add’ and ‘Existing Project’.• Locate the project you want to add the GUI to:

FEN 2011-03-08 UCN T&B: IT Technology 11

Here is the project file for

‘Banking4’

Choose that.

Page 12: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

How to Create a GUI for an Existing Project

• Now ‘Banking4’ shows in the Solution Explorer in Visual Studio:

FEN 2011-03-08 UCN T&B: IT Technology 12

Page 13: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

How to Create a GUI for an Existing Project

• Next we want to make the project (‘Banking4’) known to our GUI project:

• You must use the namespace of Banking4:• Right click on the form and choose ‘View Code’• Type this using:

FEN 2011-03-08 UCN T&B: IT Technology 13

But the compiler doesn’t recognises

it?

Right click on ‘References’ for the GUI project in the Solution Explorer and choose ‘Add

Reference’

Page 14: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

How to Create a GUI for an Existing Project

FEN 2011-03-08 UCN T&B: IT Technology 14

Choose the project tab.

Choose the project you want – Banking4

...and ‘OK’

Now the compiler recognises ‘Banking4’

namespace and we are ready.

Page 15: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Using the Existing Project

• Right click on the form, choose ‘View Code’, and now we can write code, for instance get an instance of the controller:

namespace Bank4WithGUI

{

public partial class Form1 : Form

{

private BankCtr myBank = BankCtr.GetInstance();

public Form1()

{

InitializeComponent();

}

}

}

FEN 2011-03-08 UCN T&B: IT Technology 15

Now myBank can be used in the form, and we are able to

communicate with the Banking4 application.

Page 16: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Adding Components and Controls to the Form• Drag- and-drop.

FEN 2011-03-08 UCN T&B: IT Technology 16

Label

ToolStrip

TextBox

Button

You may change the properties of components

Page 17: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Adding Code

• Test data is added by the constructor.

• New method added by me.

FEN 2011-03-08 UCN T&B: IT Technology 17

//---public Form1() //Constructor{ CreateTestData();//my method InitializeComponent();}

//---private void CreateTestData(){ Customer c = new Customer(1, "Peter Thomsen"); myBank.AddCustomer(c); myBank.AddCustomer(new Customer(2, "Ib Helmer")); BankAccount acc1 = new BankAccount(1, 8, 100); BankAccount acc2 = new BankAccount(2, 0.25, 0); BankAccount acc3 = new BankAccount(3, 5, 1000); c = myBank.GetCustomer(1); c.AddAccount(acc1); c.AddAccount(acc2); c.AddAccount(acc3); c = myBank.GetCustomer(2); c.AddAccount(acc2);}

Page 18: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Getting some Action

• Double click on the Find Customer Button and you get an empty event handler method for the button. This method is executed when the button is clicked.

• Type in the code for the actions that you want.

FEN 2011-03-08 UCN T&B: IT Technology 18

//--- private void button1_Click(object sender, EventArgs e) {

}

private void button1_Click(object sender, EventArgs e) { string input = this.textBox1.Text; int custNo = Convert.ToInt32(input); //no error handling Customer c = myBank.GetCustomer(custNo); this.textBox2.Text = c.Name; }

Page 19: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Getting some Action

• Components are objects and may used like any other objects.

FEN 2011-03-08 UCN T&B: IT Technology 19

private void button1_Click(object sender, EventArgs e) { string input = this.textBox1.Text; int custNo = Convert.ToInt32(input); //no error handling Customer c = myBank.GetCustomer(custNo); this.textBox2.Text = c.Name; }

• Use ‘this’ and the IntelliSense to find variable names of components and properties of components.

Page 20: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Error Handling - Exceptions

• If, what is entered here, not is a valid and existing customer number; some exception (NumberFormat or NullPointer) will make the program crash.

FEN 2011-03-08 UCN T&B: IT Technology 20

private void button1_Click(object sender, EventArgs e) { string input = this.textBox1.Text; int custNo = Convert.ToInt32(input); //no error handling Customer c = myBank.GetCustomer(custNo); this.textBox2.Text = c.Name; }

Page 21: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Error Handling - Exceptions

• We want to catch and handle that exception.

• That is done by enclosing the code in a try-block.

• After that we have a catch-block where we handle the exception.

FEN 2011-03-08 UCN T&B: IT Technology 21

private void button1_Click(object sender, EventArgs e){ string inString = textBox1.Text; try { int custNo = Convert.ToInt32(inString); c = bankCtr.GetCustomer(custNo); textBox2.Text = c.Name; } catch (Exception excep) { MessageBox.Show("Customer number must be an integer"); }}

Investigate Banking4GUI

Page 22: Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN 2013-04-141AK - IT Softwarekonstruktion.

Exercises

• Exercises 7 on Session08.docx.

• Extra : 8 on Session08.docx

FEN 2013-04-14 AK - IT Softwarekonstruktion 22