Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Data Layer Database Design.

Similar presentations


Presentation on theme: "The Data Layer Database Design."— Presentation transcript:

1 The Data Layer Database Design

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

3 What types of object tend to be persistent?
What types of object tend to be transitory? Give examples

4 Architecture for persistence
Presentation Layer interfaces between the software application and the database layer. wrapper layer that makes data appear as objects to the business logic layer Business Logic Layer Data Access Layer Database

5 Example : 4-layer architecture
Presentation Layer User interface classes Control classes Entity classes Lower level data handling classes Application Logic Layer Business Logic Layer Domain Layer Data Management Layer

6 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?

7 Files File 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

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

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

10 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

11 Relational Databases Data held in flat 2D tables with no embedding
Need to translate between this and a structured store of objects Normalisation can be used to decompose complex objects into tables. Classes can be translated using some general “rules of thumb” Where is this done?

12 Normalisation Aim : to reduce redundancy
Functional 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.

13 Entity- Relationship Modelling
Identify entities and attributes Remove multivalued attributes Resolve many-many relationships Should produce normalised data

14 Example : Water Safety class Enrolment student trainer studentquals
training Can a student be enrolled in more than one class at a time? If so we need this, otherwise? Qualification

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

16 Example Object identifiers become primary keys <entity>
class classID Startdate Enddate Day Time TrainerID <entity> student StudentID Name Address Phone Mobile Studentquals[] <entity> studentquals StudentID QualID Date of award TrainerID Classes containing objects –make these into a separate table and the objects are referenced by a unique object ID. Object identifiers become primary keys

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

18 Example subclasses -> tables with different structures
<entity> student StudentID Studentquals[] subclasses -> tables with different structures superclasses and subclasses ->tables. trainer table student table <entity> trainer Trainings[] Garda Vetting

19 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

20 Example : Product list 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 public 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() }

21 ProductDB Class Write a wrapper class to handle data access ProductDB
Static class Product GetProduct(); bool AddProduct (Product p); bool UpdateProduct(Product oldprod, Product newprod);

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

23 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 String string selectStatement= " SELECT BankID, Balance" + "FROM Bankaccounts" + "WHERE BankID //

24 // Open the connection and read in the record. try { connection
// 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.

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

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

27 // establish a query context over ADO
// 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>();

28 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

29 // build the query var 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 query foreach (var item in query) Console.WriteLine("{0} {1} {2} {3}", item.Name, item.OrderID, item.Amount, item.Age);

30 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; public bool CanCode; }

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


Download ppt "The Data Layer Database Design."

Similar presentations


Ads by Google