Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.

Slides:



Advertisements
Similar presentations
Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
Advertisements

CS0004: Introduction to Programming Visual Studio 2010 and Controls.
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Advanced Object-Oriented Programming Features
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Creating web service (and client application) with Visual Studio 2008 Create web service project New project->web->ASP.NET web service application (with.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
1 Web Services Visual C# 2008 Step by Step Chapter 30.
Graphical Tree-Based Scientific Calculator: CalcuWiz Will Ryan Christian Braunlich.
DT265-2 Object Oriented Software Development 2 Lecture 3 : Windows Programming Lecturer Pat Browne
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Lecture 2 Managing Windows OS Introduction to.NET Framework C# & Microsoft Visual Studio.NET 2008.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Working with Unmanaged Code.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
Web Services Week 2 Aims: Getting started with creating simple C# applications within Visual Studio.NET Objectives: –An introduction to the syntax of C#.NET.
FEN Data connection DataReader DataSet Bonus info: Concurrency and Database Transactions Embedded SQL.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN AK - IT Softwarekonstruktion.
1 Working with Menus and Dialog Boxes. 2 Objectives You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu.
Neal Stublen Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating.
UNIT 9.2: Learning Objectives Agile Development – Bruce Feiler on Agile Programming Database access from code – Database Cycle Review – SQL Command Types.
Car Rental DB Explained. RHS – Car Rental DB Explained The application demonstrates the Model- View-Controller (MVC) pattern The user can –Add.
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.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Architectures Classic Client/Server Architecture Classic Web Architecture N-tier (multi-tier) Architecture FEN Databaser og Modellering.
V 1.0 Programming III. Automatic notifications (…Changed, INofityPropertyChanged, ObservableCollection ) Data formatters Data conversions Resources.
11 Using ADO.NET II Textbook Chapter Getting Started Last class we started a simple example of using ADO.NET operations to access the Addresses.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
CSC 157 (Blum)1 Hello World. CSC 157 (Blum)2 Start/Programs/Microsoft Visual Studio.NET 2003/Microsoft Visual Studio.NET 2003.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
1 Avoiding Hacker Attacks. 2 Objectives You will be able to Avoid certain hacker attacks and crashes due to bad inputs from users.
Module 4: Creating a Web Application with Web Forms
Creating a GUI Class An example of class design using inheritance and interfaces.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Lecture Set 7 Procedures and Event Handlers Part B - The Structure of an Application Event Handlers.
User Interface Programming in C#: Basics and Events Chris North CS 3724: HCI.
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Visual Basic Fundamental Concepts
ASP.NET Programming with C# and SQL Server First Edition
INF230 Basics in C# Programming
Visual Basic..
Social Media And Global Computing Introduction to Visual Studio
Understanding the Visual IDE
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
GUI Programming in Visual Studio .NET
Presentation transcript:

Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio

Architecture So far we have had our “user interface” and “database” in the Main method This won’t do with reel users! Normally one will use what is known as a layered (or n tier) architecture, for instance: UI Controller Model Database Application Until now we have focused on the application layer

Architecture Application In this session we are going to focus on the Data Access layer

Banking4 View Code (Banking4View Code (Banking4)

Banking4 with GUI (Banking5) View Code (Banking5)

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: It must be a “Windows Form Application” Give it a good name. And choose a location for it.

How to Create a GUI for an Existing Project New Form in VS Toolbox. Here you find GUI components Properties. Here you can edit components. For instance give the form a good name

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: Here is the project file for ‘Banking4’ Choose that.

How to Create a GUI for an Existing Project Now ‘Banking4’ shows in the Solution Explorer in Visual Studio:

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: But the compiler doesn’t recognises it? Right click on ‘References’ for the GUI project in the Solution Explorer and choose ‘Add Reference’

How to Create a GUI for an Existing Project Choose the project tab. Choose the project you want – Banking4...and ‘OK’ Now the compiler recognises ‘Banking4’ namespace and we are ready.

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(); } Now myBank can be used in the form, and we are able to communicate with the Banking4 application.

Adding Components and Controls to the Form Drag- and-drop. Label ToolStrip TextBox Button You may change the properties of components

Adding Code Test data is added by the constructor. New method added by me. //--- 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); }

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. //--- 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; }

Getting some Action Components are objects and may be used like any other objects. 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.

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. 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; }

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. 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 Banking5

Adding a Database So far we have create some test data and kept them in memory. This won’t do for real application! Normally one will use what is known as a layered (or n tier) architecture, for instance: UI Controller Model Database And the database will be some SQL server. Application Now we focus on adding a database

A Side: Creating the Database First we define our database tables (this is done using SQL Server Management Studio, but can also be done from Visual Studio (not Express): One table per domain object: Customer (Bank)Account View the database in SQL Server Management Studio SQL scripts SQL scripts for creating the database and inserting test data can be found here. The 1- n association from Customer to BankAccount is implemented in the database by including custNo as a foreign key in Account

Adding a Database - Architecture We will add a database to the Banking Project. Architecture: The Controller is responsible for communication with the database connection classes (DBLayer). The DBLayer encapsulates the code for accessing the database and stores and builds objects. The DBLayer takes the role of containers. View Code SQL Server

Architecture – in Visual Studio One Solution – 5 projects, each project having its own namespace. Architecture: Model: (old Banking4). The controller is put in its own project: Controller. Banking5 is the old GUI. The DBLayer has classes for accessing the database and storing and building objects. BankingWithFullMonty is the main project with the new start-up form

DBLayer We use a SQL Server Database. The Class AccessDbSQLClient is responsible for opening and closing the connection and for creating a Command object that can be passed to the SQL Server. The class CustomerDBSQLClient is responsible for retrieving and storing Customer objects in the database. View Code

The Controller Old implementation using lists is change Instead we use calls to the database. //public void AddCustomer(Customer c) //{ // customers.Add(c); //} public void AddCustomer(Customer c) { CustomerDBSQLClient.CreateCustomer(c.CustNo, c.Name); } //public List Customers //{ // get { return customers; } //} public List Customers { get { return CustomerDBSQLClient.GetCustomers(); } }

The Controller Complex search loops are replaced by calls to the database. //public Customer GetCustomer(int no) //{ // //if customer not found, null is returned // Customer c = null; // int i = 0; // bool found = false; // while (!found && i < customers.Count) // { // c = customers[i]; // if (c.CustNo == no) // found = true; // else // i++; // } // return c; //} public Customer GetCustomer(int no) { return CustomerDBSQLClient.FindCustomerByNo(no); }

class CustomerDBSQLClient public static Customer FindCustomerByNo(int custNo) { string sql * from Customer where custNo = " +custNo; dbCmd = AccessDbSQLClient.GetDbCommand(sql); IDataReader dbReader; dbReader = dbCmd.ExecuteReader(); Customer c; if (dbReader.Read()) c = new Customer(Convert.ToInt32(dbReader["custNo"].ToString()), dbReader["name"].ToString()); else c = null; dbReader.Close(); AccessDbSQLClient.Close(); return c; } The SQL Statement is build Command object with this SQL statement is build The command is sent to the SQL Server and executed The result is returned in a reader object – The Customer object is build from the reader DB Connection is closed Eventually the Customer object is returned.