Top Banner
The Data Layer Database Design
31

The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

The Data Layer

Database Design

Page 2: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Persistent vs Transient Data

• Persistent data is data which survives after a piece of software is no longer running. It is typically stored in a file or a database, depending on the quantity and nature of the data.

Page 3: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

What types of object tend to be persistent?

What types of object tend to be transitory?

Give examples

Page 4: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Architecture for persistence

Business LogicLayer

Data AccessLayer

PresentationLayer •interfaces between the

software application and the database layer.

•wrapper layer that makes data appear as objects to the business logic layer

Database

Page 5: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Example : 4-layer architecture

• User interface classes

• Control classes• Entity classes

• Lower level data handling classes

Domain Layer

Application LogicLayer

Data Management

Layer

PresentationLayer

BusinessLogicLayer

Page 6: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Designing Persistent Storage

• Are there parts of the system where file storage is appropriate?

• Is the system a truly object oriented system or a front-end to a database (like many web applications)?

• What type of DBMS is used e.g. relational, OO• What is the logical layering of the system?

Page 7: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

FilesFile and Record Structures• Fixed – variable length records• Header/body structures of files e.g. daily transactions• Tagged data e.g. XML files

File types• Serial – update by adding at the end e.g. transaction

files • Sequential – ordered – need to partially rewrite file to

insert records.• Random access- can insert anywhere

Page 8: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

File Access Modes• Serial• Indexed Sequential• Direct access • Hashed addressing

Types of File• Master File• Transaction Files : record business transactions/events e.g.

sales transactions, banking transactions• Temporary or work files• Parameter files used for e.g. configuration, settings etc.

Page 9: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

When is it appropriate to use files?

• Files are appropriate for simple programs and data that does not need to be shared by many users.

Page 10: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Database Models• Relational databases – RDBMS – most commonly used

for record-based business applications.• Based on records or relations (rows), between

fields(columns), in tables• relational algebra is used to extract data • SQL is the standard query language e.g. SQL server, MySQL

• Object oriented Databases – used more for e.g. CAD systems. E.g. ObjectStore

• Object- Relational hybrids e.g. relational databases with OO extensions e.g. PostgresSQL

Page 11: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Relational Databases

• Data held in flat 2D tables with no embedding• Need to translate between this and a

structured store of objects1.Normalisation can be used to decompose

complex objects into tables.2.Classes can be translated using some general

“rules of thumb”Where is this done?

Page 12: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Normalisation

Aim : to reduce redundancyFunctional dependency between elements

should be 1-1• 1NF – atomic values• 2NF – every nonkey element is fully

dependent on the primary key• 3NF – attributes depend on the primary key

and nothing else.

Page 13: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Entity- Relationship Modelling

• Identify entities and attributes

• Remove multivalued attributes• Resolve many-many relationships

• Should produce normalised data

Page 14: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Example : Water Safety

student

Qualification

training

trainerstudentquals

classEnrolment

Can a student be enrolled in more than one class at a time? If so we need this, otherwise?

Page 15: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Mapping Classes to Tables

• Classes with a simple data structure become tables, object identifiers become primary keys.

• Classes containing objects – we make these into a separate table and the objects are referenced by a unique object ID.

• Collections become tables: 1-many • Many-many – we need separate tables• 1-1 Use foreign key.

Page 16: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Example<entity>student

StudentIDNameAddressPhoneMobileEmailStudentquals[]

<entity>studentquals

StudentIDQualIDDate of awardTrainerID

<entity>class

classIDStartdateEnddateDayTimeTrainerID

Object identifiers become primary keys

Classes containing objects –make these into a separate table and the objects are referenced by a unique object ID.

Page 17: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Mapping an Inheritance Hierarchy

• implement subclasses as tables with different structures or

• implement superclasses and subclasses as tables.

• Depends on what makes sense in the application.

Page 18: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Example <entity>student

StudentIDStudentquals[]

<entity>trainer

Trainings[]Garda Vetting

•subclasses -> tables with different structures •superclasses and subclasses ->tables.

trainer table student table

Page 19: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Designing Data Management Classes

• Layered architecture• Introduce new classes whose role is to deal

with the storage and retrieval of other classes.• This has the aim of separating the data

storage classes from the business classes.

-Database Broker Approach

Page 20: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Example : Product listpublic class Productlist{ private List<product> products;

public Productlist(){

products = new List<Product>();}public void Add(Product p){ products.Add(p);} ..............public void Fill(){Products = ProductDB.GetProducts()} ..................

}

Note the use of abstraction- here we just want to know that some object will fill our list for us. This will be implemented in the data access layer below

Page 21: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

ProductDB Class

ProductDBStatic class

Product GetProduct();

bool AddProduct (Product p);

bool UpdateProduct(Product oldprod, Product newprod);...........

Write a wrapper class to handle data access

Page 22: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Use of Existing Frameworks e.g. ADO.Net• Includes a number of namespaces ( including

System.Data, System.Data.Common etc.) provided by .Net framework for working with relational databases.

• Data provider objects (e.g. DataReader and dataAdapter) manage the database for your application -gets data from the database into your application.

• DataSet objects (DataTable etc.) manage the data model within your application, providing a memory-resident set of objects that you can load with data.

• For example, the SQLConnection object is used to connect into a database;

• SQLCommand can be used to send an SQL command to your database.

Page 23: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

Example : define data access class to deal with the database.

public static class BankaccDB {//……… public static BankAccount GetBankaccount(string accID) {// Set up SQL Connection SqlConnection connection = MyBankDB.GetConnection(); // Set up Query Stringstring selectStatement= " SELECT BankID, Balance" + "FROM Bankaccounts" + "WHERE BankID = @AccID";// .......................................

Page 24: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

// Open the connection and read in the record.....try { connection.Open(); SqlDataReader accReader =

selectCommand.ExecuteReader(commandBehaviour.SingleRow);

if (accReader.Read() ) { BankAccount b = new BankAccount(); b.Accountno = accReader["BankID"].ToString; b.Balance = (double) accReader["Balance"]; return b; } else { return null; }// ...... Etc.

Page 25: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

• In terms of demonstrating this on a package diagram, this would show as a dependency between your system and the ADO.net package

• Business logic layer of your application is linked directly to the implementation-specific ADO, and this still leaves the entity model as an independent layer.

Page 26: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

LINQ

• challenge in programming technology is to reduce the complexity of accessing and integrating information that is not natively defined using OO technology.

• The two most common sources of non-OO information are relational databases and XML

• LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations.

• It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.

Page 27: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

// establish a query context over ADO.NET sql connection

DataContext context = new DataContext( "Initial Catalog=petdb;Integrated Security=sspi");

// grab variables that represent the remote tables that correspond to the Person and Order CLR types

Table<Person> custs = context.GetTable<Person>();

Table<Order> orders = context.GetTable<Order>();

Page 28: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

LINQ to SQL: SQL Integration

• LINQ to SQL defines two core attributes, [Table] and [Column], which indicate which CLR types and properties correspond to external SQL data.

• [Table] attribute can be applied to a class and associates the CLR type with a named SQL table or view.

• [Column] attribute can be applied to any field or property and associates the member with a named SQL column

Page 29: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

// build the queryvar query = from c in custs from o in orders where o.Customer == c.Name select new { c.Name, o.OrderID, o.Amount, c.Age };

// execute the queryforeach (var item in query) Console.WriteLine("{0} {1} {2} {3}", item.Name, item.OrderID, item.Amount, item.Age);

Page 30: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

LINQ to SQL: SQL Integration[Table(Name="People")]public class Person { [Column(DbType="nvarchar(32) not null", Id=true)]

public string Name;

[Column] public int Age;

[Column] public bool CanCode;}

Page 31: The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives after a piece of software is no longer running. It.

[Table(Name="Orders")]public class Order { [Column(DbType="nvarchar(32) not null", Id=true)]

public string OrderID;

[Column(DbType="nvarchar(32) not null")]

public string Customer;

[Column] public int? Amount; }