Transcript chap13

Chapter 13
Introduction to Data
Access Classes and
Persistence
Object-Oriented Application Development Using
VB .NET
1
Objectives
In this chapter, you will:
• Make objects persistent
• Design a data access class
• Communicate with a data access class
• Use a relational database with VB .NET
Object-Oriented Application Development Using
VB .NET
2
Making Objects Persistent
• Object persistence
– The capability to store and retrieve information
from files
• Two approaches to achieving persistence
– Attribute storage: store/retrieve Instance attributes
• When retrieving Instance attributes, the Instance
should be created first.
– Object storage: store/retrieve Instance itself.
Object-Oriented Application Development Using
VB .NET
3
Making Objects Persistent
• Attribute storage
– Involves
• Retrieving attribute values to be filled in instance
• Writing attribute values to a file
– Disadvantage
• It is necessary to re-create the object
Object-Oriented Application Development Using
VB .NET
4
Making Objects Persistent
Object-Oriented Application Development Using
VB .NET
5
Making Objects Persistent
•
Methods for storing and retrieving objects:
– Attribute storage
1. Attribute storage and retrieval using the
StreamWriter and StreamReader
2. Attribute storage and retrieval using databases
– Object Storage
1. Object serialization: Binary, or SOAP (supports
XML)
Object-Oriented Application Development Using
VB .NET
6
Making Objects Persistent
• Object serialization
– An easy way to store and retrieve objects
• Serialization process
– Transforms an object into a stream that can be saved
to a sequential file
• Deserialization
– Transforms the stream from the file back to the object
state before the object was stored
– Advantage
• The object can be retrieved an Initialized object
• It is not necessary to create the object first, just
define reference variable.
Object-Oriented Application Development Using
VB .NET
7
Designing a Data Access Class
•
•
Purpose of a DA class
–
To provide methods that store and retrieve data
–
Make instances of a PD class persistent
Reasons for placing data storage and retrieval tasks in a
DA class
1. Data input and output code are isolated from other
classes: can reduce the maintenance process.
2. Separate classes for each tier make deployment easier
in a client-server environment
•
GUI, PD, and DA functions may reside on multiple
machines at various sites
Object-Oriented Application Development Using
VB .NET
8
Designing a Data Access Class
• DA methods can be invoked only by
the PD class
– Services provided by the DA class
will be appear to be provided by the
PD class to others (GUI Classes)
Object-Oriented Application Development Using
VB .NET
9
Data Access Methods
• A separate DA class is written for each PD class
• Example:
– CustomerDA
• Provides data storage and retrieval services for
customers (Customer Instances)
• Methods can only be invoked by the PD class
(Customer Class), when others asks it for data
store/retrieve operation.
Object-Oriented Application Development Using
VB .NET
10
The three tier classes relationship
Object-Oriented Application Development Using
VB .NET
11
Data Access Methods
• Four basic tasks provided by CustomerDA and
Customer:
– Retrieve a customer info to be set in a Customer
Instance.
– Store a customer info to a database (to make it
persistent).
– Change a customer’s data (update the current info
of a customer instance)
– Remove a customer info from database.
Object-Oriented Application Development Using
VB .NET
12
Data Access Methods
• CustomerDA class (All methods are shared)
– Find method
• Functionality implemented: receives the phone No. and retrieves
a customer if found. Otherwise, return an exception.
– AddNew method
• Functionality implemented: receives a customer instance and
stores it if it was not exist previously. Otherwise, return an
exception.
– Update method
• Functionality implemented: receives a customer instance &
updates its data if it is exist. Otherwise, return an exception.
– Delete method
• Functionality implemented: receives a customer instance &
removes its data a customer if it is exist. Otherwise, return an
exception.
Object-Oriented Application Development Using
VB .NET
13
Data Access Methods
• CustomerDA class (continued)
– Additional methods
• Initialize for initialization like opening connection.
• Terminate for termination operation like closing
connection.
• GetAll to return all instances of the class stored in
the system.
– Exception handler classes
• NotFoundException used in all methods.
• DuplicateException used in AddNew method
Object-Oriented Application Development Using
VB .NET
14
Communicating with a Data Access
Class
• Customer class
– Invokes methods in CustomerDA
– Methods
•
•
•
•
•
•
•
Find
AddNew
Find
Delete
Initialize
Terminate
GetAll
Object-Oriented Application Development Using
VB .NET
15
Finding a Customer
• The PD Find method
– Invokes the DA Find method
Public Shared Function Find(ByVal PhoneNo As String) As _
Customer
Return CustomerDA.Find(PhoneNo)
End Function
Object-Oriented Application Development Using
VB .NET
16
Adding a Customer
• The PD AddNew method
– Invokes the AddNew method in the DA class to
store a new customer instance
Public Sub AddNew()
CustomerDA.AddNew(Me)
End Sub
Object-Oriented Application Development Using
VB .NET
17
Changing a Customer
• The PD Update method
– Invokes the Update method in the DA class
Public Sub Update()
CustomerDA.Update(Me)
End Sub
Object-Oriented Application Development Using
VB .NET
18
Deleting a Customer
• The PD Delete method
– Invokes the Delete method in the DA class
Public Sub Delete()
CustomerDA.Delete(Me)
End Sub
Object-Oriented Application Development Using
VB .NET
19
Additional Problem Domain Methods
• Additional PD methods
– Initialize
Public Shared Sub Initialize()
CustomerDA.Initialize()
End Sub
– Terminate
Public Shared Sub Terminate()
CustomerDA.Terminate()
End Sub
Object-Oriented Application Development Using
VB .NET
20
Additional Problem Domain Methods
• Additional PD methods (continued)
– GetAll
Public Shared Function GetAll() As ArrayList
Return CustomerDA.GetAll( )
End Function
Object-Oriented Application Development Using
VB .NET
21
VB .NET Database Access Classes
• ADO .NET managed providers for accessing data:
– The OleDb data provider
• Designed for accessing Microsoft Access databases
and other data sources
• Namespace: OleDb
– The SQLServer data provider
• Designed for accessing Microsoft SQLServer
databases
• Namespace: SQLClient
• Object database connectivity (ODBC)
– Can be used to access most other databases
Object-Oriented Application Development Using
VB .NET
22
VB .NET Database Access Classes
Object-Oriented Application Development Using
VB .NET
23
VB .NET and DBMS Example
• Example
– Project involves working with data from a relational
database
• Selecting, adding, updating, and deleting rows from
a table
– The GUI includes
• A data grid
• Four buttons
–
–
–
–
Add Record
Update Record
Delete Record
Find
Object-Oriented Application Development Using
VB .NET
24
VB .NET and DBMS Example
Object-Oriented Application Development Using
VB .NET
25
Implementing Object Persistence with
a Database
• To implement object persistence with a relational
database
– Initialize method establishes a connection to the
database
– Find, AddNew, Update, Delete, and GetAll methods
access the database directly
– Terminate method closes the database connection
Object-Oriented Application Development Using
VB .NET
26
Summary
• Object persistence: storing instance data for
future retrieval
• Persistence can be achieved by storing either
attribute values or entire instances
• A data access (DA) class provides methods that
store and retrieve data
• Methods of a DA class are invoked only by the
methods of the corresponding PD class
• Methods of a DA class: Find, AddNew, Update,
Delete, GetAll, Initialize, and Terminate
Object-Oriented Application Development Using
VB .NET
27
Summary
• VB .NET’s stream approach to I/O views data
input and output as a flow of bytes
• Persistence can be achieved using sequential
files, object serialization, or relational databases
• A relational database is used to organize data
into tables
• In a relational database, each column represents
a field and each row represents a record
• SQL is a popular, standard language used to
access relational databases
Object-Oriented Application Development Using
VB .NET
28