DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.

Slides:



Advertisements
Similar presentations
CSSE 374: Persistent Frameworks with GoF Design Patterns & Deployment Diagrams Steve Chenoweth Office: Moench Room F220 Phone: (812)
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Fall 2009ACS-3913 R McFadyen1 Persistent Objects Persistent objects are objects that continue to exist after the application program terminates Persistence.
Design Patterns CS is not simply about programming
Architectural Analysis & Design Architectural Layers The Logical Architecture is a large-scale organization of the software classes into packages (or namespaces),
Mar 20, R McFadyen1 Persistent Objects Persistent objects are objects that continue to exist after the application program terminates Persistence.
March 31, R McFadyen1 Persistent Objects Persistent objects are objects that continue to exist after the application program terminates Persistence.
Reuse Activities Selecting Design Patterns and Components
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Chapter 25 More Design Patterns.
Client/Server Software Architectures Yonglei Tao.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
CSSE 374: Even More Object Design with Gang of Four Design Patterns These slides derived from Shawn Bohner, Curt Clifton, and others involved in delivering.
Entity Framework Code First End to End
Maven for building Java applications By Nalin De Zoysa
What is Architecture  Architecture is a subjective thing, a shared understanding of a system’s design by the expert developers on a project  In the.
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
CSSE 374: More Object Design with Gang of Four Design Patterns Steve Chenoweth Office: Moench Room F220 Phone: (812)
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Database Management System Module 5 DeSiaMorewww.desiamore.com/ifm1.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
Object-to-Relational Mapping: The Crossing Chasms Pattern and Implementation Considerations Use of Meta Data in the Java Persistence Layer Presented by.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
Designing a Persistence Framework With Patterns
1 Mapping to Relational Databases Presented by Ramona Su.
GoF Sections Design Problems and Design Patterns.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Tallinna Tehnikaülikool Informaatikainstituut Javal baseeruv objektide püsivuse kiht Jakamar Bakalaureusetöö Üliõpilane:Erki Suurjaak Matrikli nr:
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Entity Framework Code First – Beyond the Basics Sergey Barskiy, Magenic Microsoft MVP – Data Platform Principal Consultant.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Nov 20, R McFadyen1 Persistent Objects Key ideas/definitions UML representations Mapping patterns Transactional states Example Some commercial.
Chapter 9 Logical Database Design : Mapping ER Model To Tables.
Chapter 36 More Object Design with GoF Patterns 1CS6359 Fall 2011 John Cole.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 16 Using Relational Databases.
Design Patterns Definition:
OO Methodology Elaboration Iteration 2 - Design Patterns -
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Elaboration Iteration 3 – Part 3 - Persistence Framework -
Advanced Object-oriented Design Patterns Creational Design Patterns.
Mapping Objects ↔Relational DB. The Problem with Databases Databases store data in rows in tables, which are not like objects. We can simulate object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
Chapter 3: Relational Databases
Neo.NET Entity Objects Architecture and Implementation Copyright © Erik Dörnenburg – Last updated: December 2004.
OO Methodology Elaboration Iteration 3 – Part 3 - More Design Patterns -
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
SQL Basics Review Reviewing what we’ve learned so far…….
Delete Data Database Administration Fundamentals LESSON 3.4.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
7.5 Using Stored-Procedure and Triggers NAME MATRIC NUM GROUP Muhammad Azwan Bin Khairul Anwar CS2305A Muhammad Faiz Bin Badrol Shah CS2305B.
Chapter 10 Design Patterns.
Behavioral Design Patterns
UML Deployment and Component Diagrams, Designing Persistence Framework with Patterns Software Design and Analysis CSCI 2040.
Entity Framework By: Casey Griffin.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Frameworks And Patterns
Updating Databases With Open SQL
Database SQL.
Updating Databases With Open SQL
Responding to Data Manipulation Via Triggers
Presentation transcript:

DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS

The Problem: Persistent Objects persistent object An object that can survive the process or thread that created it. A persistent object exists until it is explicitly deleted ProductDescription Storage Mechanisms and Persistent Objects Object databases Relational databases others

The Solution: A Persistence Service from a Persistence Framework The framework should provide functions such as: store and retrieve objects in a persistent storage mechanism commit and rollback transactions

persistence framework general-purpose, reusable, and extendable set of types that provides functionality to support persistent objects framework A set of collaborating abstract and concrete classes that may be used as a template to solve a related family of problems. It is usually extended via subclassing for application-specific behavior

Key Ideas Mapping Object identity Database mapper Materialization and dematerialization Caches Transaction state of object Transaction operations Lazy materialization Virtual proxies

The Representing Objects as Tables pattern How do you map an object to a record or relational database schema? The Representing Objects as Tables pattern

UML Data Modeling Profile «Table» ProductDescription «PK» OID:char(16) Description:varchar(100)... «FK» Manu_OID:char(16) «Table» Manufacturer «PK» OID:char(16) Name:varchar(100) City:varchar(50) * 1 aggregate signifies a referential constraint:a ProductDescription row can't exist without a related Manufacturer row PK-primary key FK-foreign key

对关系的存储设计

对泛化的存储设计

Method 1

Method 2a

Method 2b

object identifier (OID)pattern

Accessing a Persistence Service with a Facade

Mapping Objects: Database Mapper or Database Broker Pattern Who should be responsible for materialization and dematerialization of objects (for example, a ProductDescription) from a persistent store?

The PersistenceFacade — as true of all facades — does not do the work itself, but delegates requests to subsystem objects. direct mapping persistent object class itself indirect mapping Database Broker pattern Database Mapper pattern

Metadata-Based Mappers class PersistenceFacade { / /... public Object get( OID oid, Class persistenceClass ) { // an IMapper is keyed by the Class of the persistent object IMapper mapper = (IMapper) mappers.get( persistenceClass ); // delegate return mapper.get( oid ); } //... } usage: (Manufacturer) PersistenceFacade.getInstance(). get( manuOID, Manufacturer.class) );

Template Method Pattern

Framework Design with the Template Method Pattern if (object in cache) return it else create the object from its representation in storage save object in cache return it

How to use the Framework

Further factoring out the varying and unvarying parts of the algorithm.

Final Framework

Configuring Mappers class MapperFactory { public IMapper getProductSpecificationMapper(){...} public IMapper getSaleMapper() {...} }

class MapperFactory{ public Map getAllMappers( ) {...} } class PersistenceFacade{ private java.util.Map mappers = MapperFactory.getlnstance( ).getAllMappers( ); }

class ProductSpecificationRDBMapper extends … { // hook method override protected Object getObjectFromStorage( OID oid ) { String key = oid.toString(); dbRec = SQL execution result of: "Select * from PROD_SPEC where key =" + key ProductSpecification ps = new ProductSpecification(); ps.setOID( oid ); ps.setPrice( dbRec.getColumn("PRICE") ); ps.setItemID( dbRec.getColumn("ITEM_ID") ); ps.setDescrip( dbRec.getColumn("DESC") ); return ps; }

class RDBOperations { public ResultSet getProductDescriptionData( OID oid ) {...} public ResultSet getSaleData( OID oid ) {...}... } class ProductDescriptionRDBMapper extends AbstractPersistenceMapper{ protected Object getObjectFromStorage( OID oid ) { ResultSet rs = RDBOperations.getInstance().getProductDescriptionData( oid ); ProductDescription ps = new ProductDescription(); ps.setPrice( rs.getDouble( "PRICE" ) ); ps.setOID( oid ); return ps; }

Pattern: Cache Management to maintain materialized objects in a local cache to improve performance (materialization is relatively slow) and support transaction management operations such as a commit. When objects are materialized, they are placed in the cache, with their OID as the key. Subsequent requests to the mapper for an object will cause the mapper to first search the cache, thus avoiding unnecessary materialization

Transactional States and the State Pattern Persistent objects can be inserted, deleted, or modified. Operating on a persistent object (for example, modifying it) does not cause an immediate database update; rather, an explicit commit operation must be performed.

OldCleanOldDirty OldDelete commit/delete New [from DB] [new(not from DB)] save commit/update delete rollback/reload rollback/reloadcommit/insert State chart:PersistentObject Legend: New--newly created;not in DB Old--retrieved from DB Clean--unmodified Dirty--modified Deleted

GoF State pattern Context/Problem An object's behavior is dependent on its state, and its methods contain case logic reflecting conditional state-dependent actions. Is there an alternative to conditional logic? Solution Create state classes for each state, implementing a common interface. Delegate state-dependent operations from the context object to its current state object. Ensure the context object always points to a state object reflecting its current state.

Designing a Transaction with the Command Pattern

Ordering the database tasks Table A: caseNo StudentNo Health Table B: StudentNo StudentName Inseart a record ( “ ”, ” wang ” ) to B update A ("001","05001"),

Command Context/Problem How to handle requests or tasks that need functions such as sorting (prioritizing), queueing, delaying, logging, or undoing? Solution Make each task a class that implements a common interface

actions become objects, and thus can be sorted, logged, queued, and so forth.

Lazy Materialization with a Virtual Proxy

// EAGER MATERIALIZATION OF MANUFACTURER class ProductSpecificationRDBMapper extends AbstractPersistenceMapper{ protected Object getObjectFromStorage( OID oid ){ ResultSet rs = RDBOperations.getlnstance().getProductSpecificationData( oid ); ProductSpecification ps = new ProductSpecification(); ps.setPrice( rs.getDouble( "PRICE" ) ); // here's the essence of it String manufacturerForeignKey = rs.getString( "MANU_OID" ); OID manuOID = new OID( manufacturerForeignKey ); ps.setManufacturer( (Manufacturer) PersistenceFacade.getInstance(). get(manuOID, Manufacturer.class) ); // or LAZY MATERIALIZATION OF MANUFACTURER ps.setManufacturer( new ManufacturerProxy( manuOID ) );

the Representing Object Relationships as Tables one-to-one associations Place an OID foreign key in one or both tables representing the objects in relationship. Or, create an associative table that records the OIDs of each object in relationship.

one-to-many associations, such as a collection many-to-many associations Create an associative table that records the OIDs of each object in relationship.

Unresolved Issues dematerializing objects Briefly, the mappers must define putObjectToStorage. methods. Dematerializing composition hierarchies requires collaboration between multiple mappers and the maintenance of associative tables (if an RDB is used). materialization and dematerialization of collections queries for groups of objects thorough transaction handling error handling when a database operation fails multiuser access and locking strategies security — controlling access to the database