Visual Basic: An Object Oriented Approach 9 - Persistence.

23
Visual Basic: An Object Oriented Approach 9 - Persistence
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    1

Transcript of Visual Basic: An Object Oriented Approach 9 - Persistence.

Page 1: Visual Basic: An Object Oriented Approach 9 - Persistence.

Visual Basic: An Object Oriented Approach

9 - Persistence

Page 2: Visual Basic: An Object Oriented Approach 9 - Persistence.

Persistence in software Information entered into a program

is volatile Can switch computer off Power can be removed accidentally Power-cuts System could crash

We need to make data persist beyond a single run of a program

Page 3: Visual Basic: An Object Oriented Approach 9 - Persistence.

Files File - an orderly line

In admin, a file is used to keep like items together in an order

In a computer, a file is a storage mechanism, usually associated with long-term storage on magnetic media

A computer file has an order items are written to the file in strict sequence items are read from the file in the same

sequence

Page 4: Visual Basic: An Object Oriented Approach 9 - Persistence.

Computer file mechanisms In a computer, files are handled by the

operating system Provides facilities to open, read items from,

write items to and close files to maintain a link between a program and

an open file, the O/S assigns a file handle; a number by which the file can be identified

Programs use operating system services Normally facilities are added to

programming languages to access these services

Page 5: Visual Basic: An Object Oriented Approach 9 - Persistence.

Creating a file To create a computer file

Ask the O/S for a file handle Open the file for write access Send data item variables to the file in some

order Close the file

The order of the file is defined at the point of creation Care required to prevent file errors due to

upset sequence

Page 6: Visual Basic: An Object Oriented Approach 9 - Persistence.

Code to create a fileSub CreateFile( )Dim f As Integer ‘ File handleDim aName As StringDim aNumber As Integer f = FreeFile Open “MyFile.dat” For Output As #f aName = InputBox(“Name?”) Write #f, aName aNumber = InputBox(“Number?”) Write #f, aNumber Close #fEnd Sub

Sub CreateFile( )Dim f As Integer ‘ File handleDim aName As StringDim aNumber As Integer f = FreeFile Open “MyFile.dat” For Output As #f aName = InputBox(“Name?”) Write #f, aName aNumber = InputBox(“Number?”) Write #f, aNumber Close #fEnd Sub

Ask for handle

Open File

Send data

Close file

Page 7: Visual Basic: An Object Oriented Approach 9 - Persistence.

Retrieving data from a file To retrieve data from a file

Ask the O/S for a file handle Open the file for read access Read data items from the file - they will be

in the same order Close the file

File problems generally due to sequence problem e.g. program reads a string when expecting

a number

Page 8: Visual Basic: An Object Oriented Approach 9 - Persistence.

Code to retrieve data from a file

Sub ReadFile( )Dim f As Integer ‘ File handleDim name As StringDim number As Integer f = FreeFile Open “MyFile.dat” For Input As #f Input #f, name MsgBox name Input #f, number MsgBox number Close #fEnd Sub

Sub ReadFile( )Dim f As Integer ‘ File handleDim name As StringDim number As Integer f = FreeFile Open “MyFile.dat” For Input As #f Input #f, name MsgBox name Input #f, number MsgBox number Close #fEnd Sub

Ask for handle

Open File

Read data

Close file

Page 9: Visual Basic: An Object Oriented Approach 9 - Persistence.

Types of file Files can be organised in a number of ways

Tabular Character based text Text with line-breaks Binary (sequential) Binary (Random access)

File organisation is determined by... the program code that writes the file the variable types stored decisions made by the programmer in the

interests of efficiency and ease of use

Page 10: Visual Basic: An Object Oriented Approach 9 - Persistence.

File formats

“Fred Bloggs”, “1 High Street”, £200“Jane Smith”, “55 Glen Road”, £28.55

The key feature of this file is that it is composed of a continu ous sequence of text characters with no format beyond the la nguage used. Strictly, it should be seen as one long line of te xt characters.

This type of file is distinguished by the use of line breaks (special, non-printing characters) to preserve the format of the text.Each line ends with a line-break and so need not be a fixed length.

Tabular

Text with Line

breaks

Character

Page 11: Visual Basic: An Object Oriented Approach 9 - Persistence.

File formats IIE9 26 0D 01 00 33 DB B8 00 44 CD 21 F6 D2 F6 C2

80 75 03 F6 C2 02 C3 8B C4 40 40 A3 D4 13 5B 89

1E D6 13 F8 FF E3 8B 26 D4 13 8B 1E D6 13 F9 FF

E3 52 56 8B F0 AC 0A C0 74 1E 3C 0A 74 F7 3C 0D

74 08 8A D0 B4 02 CD 21 EB EB B2 0D B4 02 CD 21

B2 0A B4 02 CD 21 EB DD 5E 5A C3 53 51 52 BB D2

13 B9 0A 00 33 D2 F7 F1 80 C2 30 4B 88 17 0B C0

75 F2 8B C3 E8 BA FF 5A 59 5B C3 50 B8 D8 13 E8

AF FF 58 E8 AB FF B8 64 16 E8 A5 FF C3 E8 EB FF

B4 4C CD 21 80 3E CA 13 FF 74 49 B8 65 14 80 3E

F3 13 00 75 03 B8 6B 16 F7 06 7A 16 00 02 74 1E

E8 7E FF A1 7A 16 25 8E 00 3D 02 00 74 08 B2 09

B4 02 CD 21 EB 1E B8 64 16 E8 65 FF EB 16 50 B8

5D 16 E8 5C FF 58 E8 58 FF B8 63 16 E8 52 FF C6

06 CA 13 FF C3 0B ED 74 12 F7 06 7A 16 88 00 75

08 F7 06 7A 16 06 00 75 02 F8 C3 F9 C3 A0 C9 13

Binary (this is Hexadecimal

which is a representation of binary)

Random access is

similar, but each record is the same number of characters

long

Page 12: Visual Basic: An Object Oriented Approach 9 - Persistence.

Files and objects We send objects to a file by serializing

them The data in each object is written out as a

sequence of values, either in binary of character form

The class is responsible for sending and retrieving object data

Consider a serialised file of objects to be a stream

Can also use streams to transmit and receive objects over a number of different media

Page 13: Visual Basic: An Object Oriented Approach 9 - Persistence.

Streams Provide an object with Save and

Load methods By keeping the code within the class,

objects manage serialization with no help from client programs

Complex objects (e.g. structured hierarchies) can manage themselves hierarchically

Page 14: Visual Basic: An Object Oriented Approach 9 - Persistence.

Sending an object hierarchy to a stream

‘ Save to Stream pseudo code…Write each member variable to the streamWrite the number of child objects to the streamFor each child object Repeat the processNext

‘ Save to Stream pseudo code…Write each member variable to the streamWrite the number of child objects to the streamFor each child object Repeat the processNext

Recursive nature of this means a complex hierarchy does not require complex code

Only ever need to deal with this object and its direct children (which deals with itself and its children etc.)

The count of child objects is the key to being able to retrieve the while hierarchy from the stream

Page 15: Visual Basic: An Object Oriented Approach 9 - Persistence.

Retrieving an object hierarchy

‘ Load from Stream pseudo code…Read each member variable from the streamRead the number of child objects from the streamFor index From 1 To Number of children Create a new child object Get it to retrieve itself from the streamNext

‘ Load from Stream pseudo code…Read each member variable from the streamRead the number of child objects from the streamFor index From 1 To Number of children Create a new child object Get it to retrieve itself from the streamNext

More complex that sending hierarchy to stream Need to create new child objects before each

can read data from the stream Need to build up hierarchy

Page 16: Visual Basic: An Object Oriented Approach 9 - Persistence.

Objects and Databases Streams of objects have an inherent

problem Need to send and retrieve the whole

hierarchy No chance of reading one or two items

back OK for a few hundred or thousand

items Not possible for millions

In this case, a database provides the solution Structured random access Fast retrieval of objects and sets of

objects

Page 17: Visual Basic: An Object Oriented Approach 9 - Persistence.

Pure object databases Transparent in operation Linked tightly to the language and the

operating system Automatic retrieval of objects as needed

Try to access an object currently not in memory, and it will be fetched automatically

Tends to be domain specific CAD systems Large structured inventories

Page 18: Visual Basic: An Object Oriented Approach 9 - Persistence.

Relational object databases Make use of existing relational

database technology Common, cheap, well understood

Can fit into any domain that works with a database Legacy systems Common computer storage mechanisms

Data, Mail, Documents etc.

Page 19: Visual Basic: An Object Oriented Approach 9 - Persistence.

Relational object modelling One table per class Relationships between records map

relationships between objects Class methods can hide relational

database layer Automatically fetch objects as references to

them are made Automatically flush objects to database on

destruction Coding can become awkward and complex

Rewards in flexibility and legacy compatibility

Page 20: Visual Basic: An Object Oriented Approach 9 - Persistence.

Tiered system models

Three (or more) tiers Presentation Business Data access

RelationalDatabase

Business Logic LayerUser-Interface Layer

Data-AccessLayer

Page 21: Visual Basic: An Object Oriented Approach 9 - Persistence.

Tiers and responsibilities Presentation layer

Handles user-interactions Connects to business logic only

Business logic layer Enforces rules on use of data and access to it Models the way the business uses information

Data access layer Enforces data security and integrity Maintains connections to various data sources

and presents them in a homogenous way to the business layer

Page 22: Visual Basic: An Object Oriented Approach 9 - Persistence.

Visual Basic data access ActiveX Data Objects (ADO)

(Bizarrely, Microsoft says the X is silent) A simple, uniform layer of services for

accessing almost any type of structured data

Relational databases (various products) Mail servers Flat-file data from spreadsheets, old style

databases Filing system resources

Page 23: Visual Basic: An Object Oriented Approach 9 - Persistence.

ADO Object Model Connection class

Creates and maintains links to data sources Recordset class

Presents data as tables of records Using these two classes, possible to

work with any database Connection opens and closes links and

sends Commands to database engine Recordset allows new records to be created,

records manipulated, records deleted