Top Banner
Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain
22

Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Jan 03, 2016

Download

Documents

Eustacia Ball
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: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Designing a Persistence Framework

With Patterns

Presented ByDr. Shazzad Hosain

Page 2: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

What is FrameworkAn extendable set of objects for related

function.Example: GUI Framework, such as Java’s AWT or

SwingCriteria of a framework: It provides an

implementation for the core and unvarying functions, and includes a mechanism to allow plug in or to extend the functions.Example: Developer can extend swing classes

Frameworks provide high degree of reuse.

Page 3: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Persistent ObjectsObjects that are stored in persistent storage

and brought into local memory during application use, e.g. ProductSpecification

Storage mechanismsObject DatabasesRelational DatabasesOther formats e.g. XML etc.

Page 4: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Persistence FrameworkPersistence Framework: A general purpose,

reusable and extendable set of types that provide functionality to support persistent objects.

Persistence Service (or Subsystem): Actually provides the service, and will be created with a persistence framework.

O-R Mapping Service: A persistence service that is written to work with RDBs.

Page 5: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Requirements for the Persistence Service and FrameworkThe framework should provide the following

functionsStore and retrieve objects in a persistent storage

mechanismCommit and rollback transactions.Design should be extendable to support different

storage mechanisms and formats, such as RDBs, records in flat files or XML in files.

Page 6: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Key Ideas for Persistence FrameworkMappingObject identityDatabase mapperMaterialization and dematerializationCachesTransaction state of objectTransaction operationsLazy materializationVirtual proxies

Page 7: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Pattern: Representing Objects as TablesHow to map an object to a record or relational

database schema?

Mapping objects and tables

Page 8: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Objects identifiers link objects and records

Pattern: Object IdentifierObject Identifier pattern proposes assigning an object

identifier (OID) to each record and object (or proxy of an object)

An OID is usually an alphanumeric value

Page 9: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

The PersistenceFacade

Accessing a Persistence Service with a Facade

Class Class

Page 10: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Pattern: Database Mapper / BrokerPersistenceFacade – as true of all facades –

does not do the work itself, but delegates requests to subsystem objects

Who should be responsible for materialization and dematerialization of objects from a persistent store?

Information Expert (or Expert) suggests the class itselfBut violates low coupling and high cohesion

Page 11: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Direct Mapping vs. Indirect MappingDirect Mapping: If the class saves data itself

Workable if the database related code is automatically generated and injected into the class by a post-processing compiler

Indirect Mapping: Uses other objects to do the maping for persistent objects.

Class Sale { total = … float getTotal () { } …….}

Class Sale { total = … float getTotal () { } ……. void saveObject () { SQL code goes here }}

Page 12: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.
Page 13: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

PersistenceFacade

HashMap <IMapper> mappers = new HashMap <IMapper> () ;mappers.put ( new ProducSpecification (), new ProductSpecificationRDBMapper () ) ;Mappers.put ( new Sale (), new SaleRDBMapper () ) ;

Class PersistenceFacade{ // …. public Object get (OID oid, Class persistenceClass) { // an Imapper is keyed by the Class of the persistence Object Imapper mapper = (IMapper) mappers.get (persistenceClass) // delegate return mapper.get ( oid ) ; }}

Page 14: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Class PersistenceFacade{ // …. public Object get (OID oid, Class persistenceClass) { // an Imapper is keyed by the Class of the persistence Object Imapper mapper = (IMapper) mappers.get (persistenceClass) // delegate return mapper.get ( oid ) ; }}Class ProductSpecificationRDBMapper {

// …. Object get (OID oid) { ProductSpecification ps = new ProductSpecification () ; SQL statements to get produc specification from tbl_product_specification ps.setProductName (pName) ; ps.setPrice (pPrice) ; return ps ; } // ….}

Hand Crafted mapper

Page 15: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Metadata-Based MapperDynamically generate the mapping from an

object schema to another schema (such as relational)

Mapping can be made also in external property files

Page 16: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Framework Design with the Template Method PatternTemplate Method pattern is at the heart of

framework designFamiliar to most OO programmers, though not

by nameThe idea is to define a method (the template

method) in a superclass that defines the skeleton of an algorithm

Template method invokes other methods some of which may be overridden in a subclass

Page 17: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Materialization with the Template Method patternIf we were to program two / three mapper classes

there will be some commonality in the code

If ( object in cache ) return itElse create the object from its representation in storage save object in cache return it

The point of variation is how the object is created from storage

Page 18: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Template Method for mapper objects

Page 19: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

Overriding the hook methodApply Template Again

Only table name

DB Record

Object from DB Record

Page 20: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

DB Record

Object from DB Record

Page 21: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.
Page 22: Designing a Persistence Framework With Patterns Presented By Dr. Shazzad Hosain.

ReferencesChapter 34 of “Applying UML and Patterns – An

Introduction to Object-Oriented Analysis and Design and the Unified Process” – by Craig Larman