The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives...

31
The Data Layer Database Design
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    232
  • download

    2

Transcript of The Data Layer Database Design. Persistent vs Transient Data Persistent data is data which survives...

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 is typically stored in a file or a database, depending on the quantity and nature of the data.

What types of object tend to be persistent?

What types of object tend to be transitory?

Give examples

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

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

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?

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

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.

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.

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

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?

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.

Entity- Relationship Modelling

• Identify entities and attributes

• Remove multivalued attributes• Resolve many-many relationships

• Should produce normalised data

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?

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.

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.

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.

Example <entity>student

StudentIDStudentquals[]

<entity>trainer

Trainings[]Garda Vetting

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

trainer table student table

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

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

ProductDB Class

ProductDBStatic class

Product GetProduct();

bool AddProduct (Product p);

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

Write a wrapper class to handle data access

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.

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

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

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

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.

// 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>();

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

// 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);

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

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