Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic: An Object Oriented Approach 9 - Persistence.

Similar presentations


Presentation on theme: "Visual Basic: An Object Oriented Approach 9 - Persistence."— Presentation transcript:

1 Visual Basic: An Object Oriented Approach 9 - Persistence

2 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

3 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

4 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

5 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

6 Code to create a file Sub CreateFile( ) Dim f As Integer ‘ File handle Dim aName As String Dim 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 #f End Sub Sub CreateFile( ) Dim f As Integer ‘ File handle Dim aName As String Dim 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 #f End Sub Ask for handle Open File Send data Close file

7 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

8 Code to retrieve data from a file Sub ReadFile( ) Dim f As Integer ‘ File handle Dim name As String Dim number As Integer f = FreeFile Open “MyFile.dat” For Input As #f Input #f, name MsgBox name Input #f, number MsgBox number Close #f End Sub Sub ReadFile( ) Dim f As Integer ‘ File handle Dim name As String Dim number As Integer f = FreeFile Open “MyFile.dat” For Input As #f Input #f, name MsgBox name Input #f, number MsgBox number Close #f End Sub Ask for handle Open File Read data Close file

9 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

10 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

11 File formats II E9 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

12 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

13 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

14 Sending an object hierarchy to a stream ‘ Save to Stream pseudo code… Write each member variable to the stream Write the number of child objects to the stream For each child object Repeat the process Next ‘ Save to Stream pseudo code… Write each member variable to the stream Write the number of child objects to the stream For each child object Repeat the process Next 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

15 Retrieving an object hierarchy ‘ Load from Stream pseudo code… Read each member variable from the stream Read the number of child objects from the stream For index From 1 To Number of children Create a new child object Get it to retrieve itself from the stream Next ‘ Load from Stream pseudo code… Read each member variable from the stream Read the number of child objects from the stream For index From 1 To Number of children Create a new child object Get it to retrieve itself from the stream Next 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

16 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

17 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

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

19 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

20 Tiered system models Three (or more) tiers Presentation Business Data access

21 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

22 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

23 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


Download ppt "Visual Basic: An Object Oriented Approach 9 - Persistence."

Similar presentations


Ads by Google