SlideShare a Scribd company logo
1 of 67
Download to read offline
HÖNNUN OG SMÍÐI HUGBÚNAÐAR 2015
L17 Data Source Layer
Agenda
Design Objectives
Object-Relational Impedance Mismatch
Problems with database programming
Data Source Patterns
Spring JDBC
RU Data Framework Content Example
Reading
Object-relational
impedance mismatch
Data Source Pattern
• Data Transfer Object
• Row Data Gateway
• Table Data Gateway
• Active Record
• Data Mapper
• Record set
Design Objectives
The Three Layers
▪ Presentation
– User’s interface to the system
– User can be another system
– Accepts input, displays views
▪ Domain
– The Application of the system
– The “Business logic”
– Has the tendency to creep into presentation and data source
▪ Data Source
– Connection to the database
We are dealing with relational databases
• Wide-spread and well understood
• SQL based, transactionally safe
• These are important in enterprise software
Drawbacks
• Bottleneck in a modern Internet system
• Impedance Mismatch
Alternatives
• NoSQL databases
• Object-Relational Mappers (ORM)
Relational Databases
Programs need to interface the Data Source
• Usually this means relational databases
Database vendors usually supply drivers for database
Rational databases use SQL language
• Fairly standard
Connecting to Data Sources
Database
classes
Driver DatabsaseProgram
Objectives
▪ Hide SQL from the Domain Layer
▪ Access to database needs to ensure
– Speed and data integrity
– Concurrent access of many clients
▪ Database independence
– It can be an objective to keep the system independent of particular
database technology
▪ Data Source Layer needs to be maintainable
– Database will change
Impedance Mismatch
Object Oriented
world
classes,
interfaces,
encapsulation,
inheritance,
polymorphism
Relational
Database world
Tables, view,
primary keys,
foreign keys,
stored
procedures
Object-Relational 

impedance mismatch
Mapping Objects to Tables
▪ Object Relational Impedance Mismatch
▪ Objects are different than tables
– Encapsulation, Accessibility, Interfaces, classes, inheritance
▪ Data types are different
– Booleans, dates, string etc.
▪ Structural differences
– Classes contain other classes
The Legacy Problem
▪ In most cases the data model exists
– The schema already exists
– We cannot assume that we create the schema
▪ Data tends to stick where it lends
– Cannot assume that our application controls the schema
– The schema will likely outlive the application
The Usability Problem
▪ The Database API determines the usability of the data access
– Should be easy to use
▪ The programming model is important
– Does matter how efficient and good a persistence framework is, if it
is complex and cumbersome to use
▪ Tools may help, but should not be used to conceal excessive
complexity
– If tools are required to generate data access the programming
model is likely to be complex
Using Databases
▪ Programmers tend to want to solve all problems in their domain
– Should we solve all problems in our object domain?
– Should we write everything in Java or C#?
▪ Databases are good at what they do
– But it’s necessary to let them do it in a natural way
Database Code
▪ Database programming can be very repetitive
– Opportunities for reusability
– JDBC is pretty low-level
▪ Too much similar code is bad!
– Don’t write code unless you have to
– Try to write code for the business layer
▪ Persistence Frameworks are difficult to build
– Use the frameworks that exist
– Examples: Spring JBDC, Hibernate, JPA, NHibernate, iBatis
https://en.wikipedia.org/wiki/List_of_object-relational_mapping_software
Which	of	these	statements	is	false
A) Data	tends	to	stick	where	it	lends
B) Database	programming	tends	to	be	low-level
C) Objects	tend	to	map	nicely	to	the	database
D) Database	programming	tends	to	be	repetitive	
QUIZ
Data Source Patterns
Domain Layer Patterns Recap
▪ Transaction Script
– Organises business logic by procedures where each procedure
handles a single request from the presentation
▪ Domain Model
– An object model of the domain that incorporates both behaviour
and data
▪ Table Module
– A single instance that handles the business logic for all rows in a
database table or view
Good Design
▪ Separate database code from other code
– Provide database classes to access the database
– All SQL code in the same place
– Factories for each database
– Use of Connection Pools
▪ Error handling
– SQLException is isolated in the Data Source Layer
– Wrap in domain specific exceptions – use of runtime exceptions
Design Example
▪ Domain Model uses gateways
Useful Patterns
▪ Data Transfer Object
– An object that carries data between processes in order to deduce
the number of method calls
▪ Record Set
– An in-memory representation of tabular data
Data Transfer Object
An object that carries data between processes in order to deduce the
number of method calls
▪ Object that is used to transfer data between layers
– Data Source returns data objects to web layer
Data Transfer Object
▪ How it Works
– Similar to Value Object but is constructed to carry data between
layers
– Data source layer creates DTO for transfer
– DTOs holds data – get/set method
– Can be mutable or immutable
– Could have methods to transform data – for example serialize the
data or convert to XML
– Simple Domain Objects can be used as DTO
• Creates dependencies
Data Transfer Object
▪ Assembling DTO from domain objects
– Assembler reduces dependencies
▪ When To Use It
– Whenever you need to transfer multiple items of data between two
processes in a single method call
Record Set
▪ An in-memory representation of tabular data
▪ How It Works
– Contains the result of a database query
– Common in ADO.NET and JDBC
– One record is current, clients can traverse the set
– Usually provided by the database code
▪ When to Use It
– When returning data from a query
Data Source Patterns
▪ Table Data Gateway
– Acts as a Gateway to a database table
▪ Row Data Gateway
– Acts as a Gateway to a single record in a data source
▪ Active Record
– Wraps a row in a database table or view, encapsulates the database
access, and adds domain logic on that data
▪ Data Mapper
– A layer of Mappers that moves data between objects and database
while keeping them independent
Pattern Overview
Data Source Layer
▪ Domain layer has influence on the patterns
▪ Transaction Script
– Table Data Gateway for single access to a table
– Row Data Gateway for single access to a row of a table
▪ Domain Model
– Active Record or Row Data Gateway
– Data Mapper
▪ Table Module
– Table Data Gateway with Record Set
Data Source Patterns
▪ Table Data Gateway
– Acts as a Gateway to a database table
▪ Row Data Gateway
– Acts as a Gateway to a single record in a data source
▪ Active Record
– Wraps a row in a database table or view, encapsulates the database
access, and adds domain logic on that data
▪ Data Mapper
– A layer of Mappers that moves data between objects and database
while keeping them independent
Table Data Gateway
An object that acts as a Gateway to a database table. One
instance handles all the rows in the table.
▪ Also called Data Access Objects – DAO
▪ How It Works
– Simple interface to a table with several find methods and methods
for maintaining data
– CRUD methods (Create, Read, Update, Delete)
– Acts as a gateway to a table
– One gateway for each table
– Finders return Collection of DTOs or Record Set
Table Data Gateway
▪ When to Use It
– Works for Table Module since it is based on Record Set
– Useful for web application where Domain Model is used
Row Data Gateway
An object that acts as a Gateway to a single record in a data
source.There is only one instance per row.
▪ How It Works
– Object that is exactly one 

single record
– Each table column is a 

field in the object
– Do not have logic
– Finder object
– Can be generated
Row Data Gateway
▪ When to Use It
– Works well for simple domain layer for example Transaction Script
Active Record
An object that wraps a row in a database table or view,
encapsulates the database access, and adds domain logic on that
data
▪ How It Works
– Each object can read and

store itself
– Contain domain logic
▪ When to Use It
– When Domain Logic is not 

too complex
– When using Transaction Script
+insert()
+update()
+delete()
+getExemption()
+isFlaggedForAudit(in CompanyID)
+getTaxableEarnings()
-lastname
-firstname
-NumerOfDependents
Person
Data Mapper
A layer of Mappers that moves data between objects and a
database while keeping them independent of each other and the
mapper itself
▪ Separates the in-memory objects from the database
Data Mapper
▪ How It works
– Simple Data Mappers map in-memory object to table on a field-to-
field basis
– Others need to map more complicated object hierarchies to multiple
tables
– Mapper uses Identity Map to see if object is already loaded
▪ For insert and updates
– The mapper must know what objects have changed, which are new,
and which must be destroyed
– Unit of Work pattern
Data Mapper
▪ When to Use It
– Database and object model must be independent
– Data Mappers are useful with Domain Model
– For simple Domain Model an Active Record could be used, but as
it becomes more complicated some mapping is needed
▪ O/R mapping solutions can provide the mappers
– For example Hibernate
Data Mapper
▪ Simple example
– Loading from the database
Data Mapper
▪ Simple example
– Updating data
– Client asks the mapper to save a domain object
– The mapper pulls the data out of the domain object and saves to the
database
Data	Source	class	maps	nicely	to	the	rows	in	a	table	and	contains	
some	useful	methods
A) Row	Data	Gateway
B) Table	Data	Gateway
C) Active	Record
D) Data	Mapper
QUIZ
Spring JDBC
Spring JDBC
▪ Spring JDBC packages
– org.springframework.jdbc
▪ datasource
– Classes for connecting to the database
▪ core
– Base classes for accessing the database – JdbcTemplate
▪ object
– Classes that support updating, inserting and deleting data from the
database
▪ support
– Utility classes
Loading the DataSource
▪ Code
▪ Configuration – data.xml
Resource res = new FileSystemResource("data.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
DataSource ds = (DataSource)factory.getBean("dataSource");
<beans>
<bean id="dataSource“ class=
"org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>net.sourceforge.jtds.jdbc.Driver</value>
</property>
<property name="url”>
<value>jdbc:jtds:sqlserver://honn.ru.is:1433</value>
</property>
<property name="username"><value>andri</value></property>
<property name="password"><value>abc123</value></property>
</bean>
</beans>
JdbcTemplate
▪ Main class of core package
– Simplifies queries
▪ Template Method pattern
– JdbcTemplate handles the 

processing and calls our code
– Dependency Injection
JdbcTemplate Example
ParameterizedRowMapper<String> rm = new ParameterizedRowMapper<String>()
{
public String mapRow(ResultSet rs, int rowNum)
throws SQLException
{
return rs.getString("title");
}
};
JdbcTemplate tpl = new JdbcTemplate(getDataSource());
Collection<String> col = tpl.query("select * from contents", rm);
for(String s : col)
{
System.out.println(s);
}
Kenya's elephants send text messages to rangers
(AP)
Flexible OLEDs could be part of lighting's future
(AP)
Collecting Data
▪ Spring Interface RowMapper
– An interface used by JdbcTemplate for mapping returned result
sets
– Class that implements this interface can be used to collect data
public interface ParameterizedRowMapper<T> extends RowMapper<T>
{
Object mapRow(ResultSet rs, int rowNum) throws SQLException;
}
ContentRowMapper
public class ContentRowMapper implements
ParameterizedRowMapper<Content>
{
public Content mapRow(ResultSet rs, int rowNum) throws SQLException
{
Content content = new Content (rs.getInt (1), // id
rs.getString (2), // title
rs.getString (3), // link
rs.getString (4), // description
rs.getDate (5), // pubdate
rs.getString(6)); // author
return content;
}
}
Using ContentRowMapper
▪ JdbcTemplate method query takes RowMapper interface as
parameter
ContentRowMapper crm = new ContentRowMapper();
JdbcTemplate tpl = new JdbcTemplate(ds);
List l = tpl.query("select * from contents", crm);
Iterator i = l.iterator();
Content cont;
while (i.hasNext())
{
cont = (Content) i.next();
System.out.println(cont);
}
Insert
▪ SimpleJdbcInsert
– Class for inserts
public int add(Content content)
{
SimpleJdbcInsert insertContent =
new SimpleJdbcInsert(getDataSource())
.withTableName("contents")
.usingGeneratedKeyColumns("id");
Map<String, Object> parameters = new HashMap<String, Object>(5);
parameters.put("title", content.getTitle());
parameters.put("link", content.getLink());
parameters.put("description", content.getDescription());
parameters.put("pubdate", content.getPubDate());
parameters.put("author", content.getAuthor());
return insertContent.executeAndReturnKey(parameters).intValue();
}
RU Data Framework Content Example
Content Example
▪ Table contents
– Contains content information
CREATE TABLE contents
(
id int Identity (1, 1) primary key NOT NULL,
title varchar(128),
link varchar(512) unique,
description text,
pubDate datetime,
author varchar(128),
)
Content Example
▪ Gateway class for the contents table
– ContentDataGateway interface contains the CRUD operations
– Class ContentData implements the gateway and provides the
JDBC code
– Content is simple 

JavaBean – acts as 

Data Transfer 

Object
RU Data Framework
▪ Classes and interfaces for accessing the database
– Implementation of the Data Gateway
▪ For Table Data Gateway
– Each table has an Gateway interface
– Implementation in Data classes
– Factory pattern returns the implementation for each Date Gateway
RuDataAccessFactory
▪ Factory for creating Gateway interfaces
– Reads information about the DataSource
– Spring Bean Definition file: data.xml
– Uses Spring Bean Factory
– RuDataAccessFactory reads information on each gateway
interface and which classes to use as implementation
– Code using the gateway interface calls getDataAccess in the
factory class
factory = RuDataAccessFactory.getInstance("data.xml");
contentDataGateway = (ContentDataGateway)

factory.getDataAccess("contentDataAccess");
Data Definition
▪ File data.xml
<beans>
<!-- Data Source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driv erClassName">
<value>net.sourceforge.jtds.jdbc.Driver</value></property>
<property name="url">
<value>jdbc:jtds:sqlserver://honn.ru.is:1433</value></property>
<property name="username"><value>andri</value></property>
<property name="password"><value>abc123</value></property>
</bean>
<!– Content Gateway Interface -->
<bean id="contentGateway"
class="is.ru.honn.tube.data.content.ContentData"/>
</bean>
</beans>
DataSource
▪ DataSource is a connection to a database
– Driver Class Name (net.sourceforge.jtds.jdbc.Driver)
– URL (jdbc:jtds:sqlserver://honn.ru.is:1433)
– username (andri)
– password (abc123)
▪ RU framework uses Spring
– Load the DataSource information
– DriverManagerDataSource

extends 

AbstractDataSource

which implements

DataSource
DataSource in Ru Framework
▪ RU Framework uses Spring to get DataSource
Using RU Framework
RuDataAccess
▪ RuDataAccess is base interface for Data gateway interfaces
– All gateway interfaces extend RuDataAccess
– Has methods to set and get DataSource
RuData
▪ RuData is a class implementing RuDataAccess
– Handles DataSource
– Data classes extend this class
ContentDataGateway
▪ Contains all the method that are needed to manage contents
– Gateway to the contents table
– Pattern Table Data Gateway
public interface ContentDataGateway extends RuDataAccess
{
public int add(Content content);
public List<Content> getContents();
}
ContentData
public class ContentData extends RuData implements ContentDataGateway
{
public int add(Content content)
{
SimpleJdbcInsert insertContent =
new SimpleJdbcInsert(getDataSource())
.withTableName("contents")
.usingGeneratedKeyColumns("id");
Map<String, Object> parameters = new HashMap<String, Object>(5);
parameters.put("title", content.getTitle());
parameters.put("link", content.getLink());
parameters.put("description", content.getDescription());
parameters.put("pubdate", content.getPubDate());
parameters.put("author", content.getAuthor());
return insertContent.executeAndReturnKey(parameters).intValue();
}
ContentData
public List getContents()
{
JdbcTemplate queryContent = new JdbcTemplate(getDataSource());
List<Content> contents = queryContent.query

("select * from contents", new ContentRowMapper());
return contents;
}
Usage
public class ContentServiceData implements ContentService {
private ContentDataGateway contentDataGateway = null;
public ContentServiceData()
{
RuDataAccessFactory factory = null;
try {
factory = RuDataAccessFactory.getInstance("data.xml");
} catch (RuException e) { ... }
contentDataGateway = (ContentDataGateway)

factory.getDataAccess("contentDataGateway");
}
public void addContent(Content content)
{
contentDataGateway.add(content);
}
public List<Content> getContents()
{
return contentDataGateway.getContents();
}
}
Contents
▪ Use ContentServiceData instead of a Service Stub
– Change one line in the app.xml file
<bean id="contentService"
class="is.ru.honn.tube.service.ContentServiceData">
</bean>
Contents
▪ Use ContentServiceData instead of a Service Stub
– Change one line in the app.xml file
RU Database
▪ hrnem.ru.is
▪ Each student has their own database
▪ Username and passwords sent out
▪ Check the access
Summary
▪ Design Objectives
– Object-relational impedance mismatch
– The Usability Problem
– The Legacy Problem
▪ Patterns
– Table Data Gateway
– Row Data Gateway
– Active Record
– Data Mapper
– Data Transfer Object and Record set

More Related Content

What's hot

KnowIT, semantic informatics knowledge base
KnowIT, semantic informatics knowledge baseKnowIT, semantic informatics knowledge base
KnowIT, semantic informatics knowledge baseLaurent Alquier
 
1. introduction to no sql
1. introduction to no sql1. introduction to no sql
1. introduction to no sqlAnuja Gunale
 
Under The Hood of Pluggable Databases by Alex Gorbachev, Pythian, Oracle OpeW...
Under The Hood of Pluggable Databases by Alex Gorbachev, Pythian, Oracle OpeW...Under The Hood of Pluggable Databases by Alex Gorbachev, Pythian, Oracle OpeW...
Under The Hood of Pluggable Databases by Alex Gorbachev, Pythian, Oracle OpeW...Alex Gorbachev
 
HBase and Drill: How loosley typed SQL is ideal for NoSQL
HBase and Drill: How loosley typed SQL is ideal for NoSQLHBase and Drill: How loosley typed SQL is ideal for NoSQL
HBase and Drill: How loosley typed SQL is ideal for NoSQLDataWorks Summit
 
Introduction to NOSQL databases
Introduction to NOSQL databasesIntroduction to NOSQL databases
Introduction to NOSQL databasesAshwani Kumar
 
From Raw Data to Analytics with No ETL
From Raw Data to Analytics with No ETLFrom Raw Data to Analytics with No ETL
From Raw Data to Analytics with No ETLCloudera, Inc.
 
Introduction to Machine Learning for Oracle Database Professionals
Introduction to Machine Learning for Oracle Database ProfessionalsIntroduction to Machine Learning for Oracle Database Professionals
Introduction to Machine Learning for Oracle Database ProfessionalsAlex Gorbachev
 
Introduction To HBase
Introduction To HBaseIntroduction To HBase
Introduction To HBaseAnil Gupta
 
Appache Cassandra
Appache Cassandra  Appache Cassandra
Appache Cassandra nehabsairam
 
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...Fabio Fumarola
 
Big Challenges in Data Modeling: NoSQL and Data Modeling
Big Challenges in Data Modeling: NoSQL and Data ModelingBig Challenges in Data Modeling: NoSQL and Data Modeling
Big Challenges in Data Modeling: NoSQL and Data ModelingDATAVERSITY
 
HBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
HBaseCon 2012 | HBase Schema Design - Ian Varley, SalesforceHBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
HBaseCon 2012 | HBase Schema Design - Ian Varley, SalesforceCloudera, Inc.
 
Semantika Introduction
Semantika IntroductionSemantika Introduction
Semantika IntroductionJosef Hardi
 
data stage-material
data stage-materialdata stage-material
data stage-materialRajesh Kv
 

What's hot (20)

KnowIT, semantic informatics knowledge base
KnowIT, semantic informatics knowledge baseKnowIT, semantic informatics knowledge base
KnowIT, semantic informatics knowledge base
 
1. introduction to no sql
1. introduction to no sql1. introduction to no sql
1. introduction to no sql
 
Under The Hood of Pluggable Databases by Alex Gorbachev, Pythian, Oracle OpeW...
Under The Hood of Pluggable Databases by Alex Gorbachev, Pythian, Oracle OpeW...Under The Hood of Pluggable Databases by Alex Gorbachev, Pythian, Oracle OpeW...
Under The Hood of Pluggable Databases by Alex Gorbachev, Pythian, Oracle OpeW...
 
HBase and Drill: How loosley typed SQL is ideal for NoSQL
HBase and Drill: How loosley typed SQL is ideal for NoSQLHBase and Drill: How loosley typed SQL is ideal for NoSQL
HBase and Drill: How loosley typed SQL is ideal for NoSQL
 
10. Graph Databases
10. Graph Databases10. Graph Databases
10. Graph Databases
 
Introduction to NOSQL databases
Introduction to NOSQL databasesIntroduction to NOSQL databases
Introduction to NOSQL databases
 
From Raw Data to Analytics with No ETL
From Raw Data to Analytics with No ETLFrom Raw Data to Analytics with No ETL
From Raw Data to Analytics with No ETL
 
Introduction to Machine Learning for Oracle Database Professionals
Introduction to Machine Learning for Oracle Database ProfessionalsIntroduction to Machine Learning for Oracle Database Professionals
Introduction to Machine Learning for Oracle Database Professionals
 
Introduction To HBase
Introduction To HBaseIntroduction To HBase
Introduction To HBase
 
Datastage ppt
Datastage pptDatastage ppt
Datastage ppt
 
Apache HBase™
Apache HBase™Apache HBase™
Apache HBase™
 
Nosql data models
Nosql data modelsNosql data models
Nosql data models
 
Appache Cassandra
Appache Cassandra  Appache Cassandra
Appache Cassandra
 
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
1. Introduction to the Course "Designing Data Bases with Advanced Data Models...
 
Database Technologies
Database TechnologiesDatabase Technologies
Database Technologies
 
Big Challenges in Data Modeling: NoSQL and Data Modeling
Big Challenges in Data Modeling: NoSQL and Data ModelingBig Challenges in Data Modeling: NoSQL and Data Modeling
Big Challenges in Data Modeling: NoSQL and Data Modeling
 
L08 Data Source Layer
L08 Data Source LayerL08 Data Source Layer
L08 Data Source Layer
 
HBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
HBaseCon 2012 | HBase Schema Design - Ian Varley, SalesforceHBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
HBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
 
Semantika Introduction
Semantika IntroductionSemantika Introduction
Semantika Introduction
 
data stage-material
data stage-materialdata stage-material
data stage-material
 

Similar to OBJECT-RELATIONAL IMPEDANCE MISMATCH AND DATA SOURCE PATTERNS

Database and Java Database Connectivity
Database and Java Database ConnectivityDatabase and Java Database Connectivity
Database and Java Database ConnectivityGary Yeh
 
Object Relational Mapping with Dapper (Micro ORM)
Object Relational Mapping with Dapper (Micro ORM)Object Relational Mapping with Dapper (Micro ORM)
Object Relational Mapping with Dapper (Micro ORM)Muhammad Umar
 
Day 9 - PostgreSQL Application Architecture
Day 9 - PostgreSQL Application ArchitectureDay 9 - PostgreSQL Application Architecture
Day 9 - PostgreSQL Application ArchitectureBarry Jones
 
Democratizing Data
Democratizing DataDemocratizing Data
Democratizing DataDatabricks
 
The Evolution of the Oracle Database - Then, Now and Later (Fontys Hogeschool...
The Evolution of the Oracle Database - Then, Now and Later (Fontys Hogeschool...The Evolution of the Oracle Database - Then, Now and Later (Fontys Hogeschool...
The Evolution of the Oracle Database - Then, Now and Later (Fontys Hogeschool...Lucas Jellema
 
Using Familiar BI Tools and Hadoop to Analyze Enterprise Networks
Using Familiar BI Tools and Hadoop to Analyze Enterprise NetworksUsing Familiar BI Tools and Hadoop to Analyze Enterprise Networks
Using Familiar BI Tools and Hadoop to Analyze Enterprise NetworksMapR Technologies
 
Data processing with spark in r &amp; python
Data processing with spark in r &amp; pythonData processing with spark in r &amp; python
Data processing with spark in r &amp; pythonMaloy Manna, PMP®
 
Using Familiar BI Tools and Hadoop to Analyze Enterprise Networks
Using Familiar BI Tools and Hadoop to Analyze Enterprise NetworksUsing Familiar BI Tools and Hadoop to Analyze Enterprise Networks
Using Familiar BI Tools and Hadoop to Analyze Enterprise NetworksDataWorks Summit
 
Hive Evolution: ApacheCon NA 2010
Hive Evolution:  ApacheCon NA 2010Hive Evolution:  ApacheCon NA 2010
Hive Evolution: ApacheCon NA 2010John Sichi
 
A Scalable Data Transformation Framework using Hadoop Ecosystem
A Scalable Data Transformation Framework using Hadoop EcosystemA Scalable Data Transformation Framework using Hadoop Ecosystem
A Scalable Data Transformation Framework using Hadoop EcosystemDataWorks Summit
 

Similar to OBJECT-RELATIONAL IMPEDANCE MISMATCH AND DATA SOURCE PATTERNS (20)

L13 Oranizing Domain Logic
L13 Oranizing Domain LogicL13 Oranizing Domain Logic
L13 Oranizing Domain Logic
 
L15 Organising Domain Layer
L15 Organising Domain LayerL15 Organising Domain Layer
L15 Organising Domain Layer
 
L19 Application Architecture
L19 Application ArchitectureL19 Application Architecture
L19 Application Architecture
 
L07 Oranizing Domain Logic
L07 Oranizing Domain LogicL07 Oranizing Domain Logic
L07 Oranizing Domain Logic
 
Database and Java Database Connectivity
Database and Java Database ConnectivityDatabase and Java Database Connectivity
Database and Java Database Connectivity
 
Object Relational Mapping with Dapper (Micro ORM)
Object Relational Mapping with Dapper (Micro ORM)Object Relational Mapping with Dapper (Micro ORM)
Object Relational Mapping with Dapper (Micro ORM)
 
Day 9 - PostgreSQL Application Architecture
Day 9 - PostgreSQL Application ArchitectureDay 9 - PostgreSQL Application Architecture
Day 9 - PostgreSQL Application Architecture
 
Democratizing Data
Democratizing DataDemocratizing Data
Democratizing Data
 
The Evolution of the Oracle Database - Then, Now and Later (Fontys Hogeschool...
The Evolution of the Oracle Database - Then, Now and Later (Fontys Hogeschool...The Evolution of the Oracle Database - Then, Now and Later (Fontys Hogeschool...
The Evolution of the Oracle Database - Then, Now and Later (Fontys Hogeschool...
 
Hadoop
HadoopHadoop
Hadoop
 
Using Familiar BI Tools and Hadoop to Analyze Enterprise Networks
Using Familiar BI Tools and Hadoop to Analyze Enterprise NetworksUsing Familiar BI Tools and Hadoop to Analyze Enterprise Networks
Using Familiar BI Tools and Hadoop to Analyze Enterprise Networks
 
L11 Application Architecture
L11 Application ArchitectureL11 Application Architecture
L11 Application Architecture
 
Data processing with spark in r &amp; python
Data processing with spark in r &amp; pythonData processing with spark in r &amp; python
Data processing with spark in r &amp; python
 
Using Familiar BI Tools and Hadoop to Analyze Enterprise Networks
Using Familiar BI Tools and Hadoop to Analyze Enterprise NetworksUsing Familiar BI Tools and Hadoop to Analyze Enterprise Networks
Using Familiar BI Tools and Hadoop to Analyze Enterprise Networks
 
L20 Scalability
L20 ScalabilityL20 Scalability
L20 Scalability
 
Hive Evolution: ApacheCon NA 2010
Hive Evolution:  ApacheCon NA 2010Hive Evolution:  ApacheCon NA 2010
Hive Evolution: ApacheCon NA 2010
 
Sql Server2008
Sql Server2008Sql Server2008
Sql Server2008
 
Cheetah:Data Warehouse on Top of MapReduce
Cheetah:Data Warehouse on Top of MapReduceCheetah:Data Warehouse on Top of MapReduce
Cheetah:Data Warehouse on Top of MapReduce
 
A Scalable Data Transformation Framework using Hadoop Ecosystem
A Scalable Data Transformation Framework using Hadoop EcosystemA Scalable Data Transformation Framework using Hadoop Ecosystem
A Scalable Data Transformation Framework using Hadoop Ecosystem
 
Map reducecloudtech
Map reducecloudtechMap reducecloudtech
Map reducecloudtech
 

More from Ólafur Andri Ragnarsson

New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionÓlafur Andri Ragnarsson
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine Ólafur Andri Ragnarsson
 

More from Ólafur Andri Ragnarsson (20)

Nýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfaraNýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfara
 
Nýjast tækni og framtíðin
Nýjast tækni og framtíðinNýjast tækni og framtíðin
Nýjast tækni og framtíðin
 
New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course Introduction
 
L01 Introduction
L01 IntroductionL01 Introduction
L01 Introduction
 
L23 Robotics and Drones
L23 Robotics and Drones L23 Robotics and Drones
L23 Robotics and Drones
 
L22 Augmented and Virtual Reality
L22 Augmented and Virtual RealityL22 Augmented and Virtual Reality
L22 Augmented and Virtual Reality
 
L20 Personalised World
L20 Personalised WorldL20 Personalised World
L20 Personalised World
 
L19 Network Platforms
L19 Network PlatformsL19 Network Platforms
L19 Network Platforms
 
L18 Big Data and Analytics
L18 Big Data and AnalyticsL18 Big Data and Analytics
L18 Big Data and Analytics
 
L17 Algorithms and AI
L17 Algorithms and AIL17 Algorithms and AI
L17 Algorithms and AI
 
L16 Internet of Things
L16 Internet of ThingsL16 Internet of Things
L16 Internet of Things
 
L14 From the Internet to Blockchain
L14 From the Internet to BlockchainL14 From the Internet to Blockchain
L14 From the Internet to Blockchain
 
L14 The Mobile Revolution
L14 The Mobile RevolutionL14 The Mobile Revolution
L14 The Mobile Revolution
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine
 
L12 digital transformation
L12 digital transformationL12 digital transformation
L12 digital transformation
 
L10 The Innovator's Dilemma
L10 The Innovator's DilemmaL10 The Innovator's Dilemma
L10 The Innovator's Dilemma
 
L09 Disruptive Technology
L09 Disruptive TechnologyL09 Disruptive Technology
L09 Disruptive Technology
 
L09 Technological Revolutions
L09 Technological RevolutionsL09 Technological Revolutions
L09 Technological Revolutions
 
L07 Becoming Invisible
L07 Becoming InvisibleL07 Becoming Invisible
L07 Becoming Invisible
 
L06 Diffusion of Innovation
L06 Diffusion of InnovationL06 Diffusion of Innovation
L06 Diffusion of Innovation
 

Recently uploaded

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 

Recently uploaded (20)

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 

OBJECT-RELATIONAL IMPEDANCE MISMATCH AND DATA SOURCE PATTERNS

  • 1. HÖNNUN OG SMÍÐI HUGBÚNAÐAR 2015 L17 Data Source Layer
  • 2. Agenda Design Objectives Object-Relational Impedance Mismatch Problems with database programming Data Source Patterns Spring JDBC RU Data Framework Content Example
  • 3. Reading Object-relational impedance mismatch Data Source Pattern • Data Transfer Object • Row Data Gateway • Table Data Gateway • Active Record • Data Mapper • Record set
  • 5. The Three Layers ▪ Presentation – User’s interface to the system – User can be another system – Accepts input, displays views ▪ Domain – The Application of the system – The “Business logic” – Has the tendency to creep into presentation and data source ▪ Data Source – Connection to the database
  • 6. We are dealing with relational databases • Wide-spread and well understood • SQL based, transactionally safe • These are important in enterprise software Drawbacks • Bottleneck in a modern Internet system • Impedance Mismatch Alternatives • NoSQL databases • Object-Relational Mappers (ORM) Relational Databases
  • 7. Programs need to interface the Data Source • Usually this means relational databases Database vendors usually supply drivers for database Rational databases use SQL language • Fairly standard Connecting to Data Sources Database classes Driver DatabsaseProgram
  • 8. Objectives ▪ Hide SQL from the Domain Layer ▪ Access to database needs to ensure – Speed and data integrity – Concurrent access of many clients ▪ Database independence – It can be an objective to keep the system independent of particular database technology ▪ Data Source Layer needs to be maintainable – Database will change
  • 9. Impedance Mismatch Object Oriented world classes, interfaces, encapsulation, inheritance, polymorphism Relational Database world Tables, view, primary keys, foreign keys, stored procedures Object-Relational 
 impedance mismatch
  • 10. Mapping Objects to Tables ▪ Object Relational Impedance Mismatch ▪ Objects are different than tables – Encapsulation, Accessibility, Interfaces, classes, inheritance ▪ Data types are different – Booleans, dates, string etc. ▪ Structural differences – Classes contain other classes
  • 11. The Legacy Problem ▪ In most cases the data model exists – The schema already exists – We cannot assume that we create the schema ▪ Data tends to stick where it lends – Cannot assume that our application controls the schema – The schema will likely outlive the application
  • 12. The Usability Problem ▪ The Database API determines the usability of the data access – Should be easy to use ▪ The programming model is important – Does matter how efficient and good a persistence framework is, if it is complex and cumbersome to use ▪ Tools may help, but should not be used to conceal excessive complexity – If tools are required to generate data access the programming model is likely to be complex
  • 13. Using Databases ▪ Programmers tend to want to solve all problems in their domain – Should we solve all problems in our object domain? – Should we write everything in Java or C#? ▪ Databases are good at what they do – But it’s necessary to let them do it in a natural way
  • 14. Database Code ▪ Database programming can be very repetitive – Opportunities for reusability – JDBC is pretty low-level ▪ Too much similar code is bad! – Don’t write code unless you have to – Try to write code for the business layer ▪ Persistence Frameworks are difficult to build – Use the frameworks that exist – Examples: Spring JBDC, Hibernate, JPA, NHibernate, iBatis https://en.wikipedia.org/wiki/List_of_object-relational_mapping_software
  • 15. Which of these statements is false A) Data tends to stick where it lends B) Database programming tends to be low-level C) Objects tend to map nicely to the database D) Database programming tends to be repetitive QUIZ
  • 17. Domain Layer Patterns Recap ▪ Transaction Script – Organises business logic by procedures where each procedure handles a single request from the presentation ▪ Domain Model – An object model of the domain that incorporates both behaviour and data ▪ Table Module – A single instance that handles the business logic for all rows in a database table or view
  • 18. Good Design ▪ Separate database code from other code – Provide database classes to access the database – All SQL code in the same place – Factories for each database – Use of Connection Pools ▪ Error handling – SQLException is isolated in the Data Source Layer – Wrap in domain specific exceptions – use of runtime exceptions
  • 19. Design Example ▪ Domain Model uses gateways
  • 20. Useful Patterns ▪ Data Transfer Object – An object that carries data between processes in order to deduce the number of method calls ▪ Record Set – An in-memory representation of tabular data
  • 21. Data Transfer Object An object that carries data between processes in order to deduce the number of method calls ▪ Object that is used to transfer data between layers – Data Source returns data objects to web layer
  • 22. Data Transfer Object ▪ How it Works – Similar to Value Object but is constructed to carry data between layers – Data source layer creates DTO for transfer – DTOs holds data – get/set method – Can be mutable or immutable – Could have methods to transform data – for example serialize the data or convert to XML – Simple Domain Objects can be used as DTO • Creates dependencies
  • 23. Data Transfer Object ▪ Assembling DTO from domain objects – Assembler reduces dependencies ▪ When To Use It – Whenever you need to transfer multiple items of data between two processes in a single method call
  • 24. Record Set ▪ An in-memory representation of tabular data ▪ How It Works – Contains the result of a database query – Common in ADO.NET and JDBC – One record is current, clients can traverse the set – Usually provided by the database code ▪ When to Use It – When returning data from a query
  • 25. Data Source Patterns ▪ Table Data Gateway – Acts as a Gateway to a database table ▪ Row Data Gateway – Acts as a Gateway to a single record in a data source ▪ Active Record – Wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data ▪ Data Mapper – A layer of Mappers that moves data between objects and database while keeping them independent
  • 27. Data Source Layer ▪ Domain layer has influence on the patterns ▪ Transaction Script – Table Data Gateway for single access to a table – Row Data Gateway for single access to a row of a table ▪ Domain Model – Active Record or Row Data Gateway – Data Mapper ▪ Table Module – Table Data Gateway with Record Set
  • 28. Data Source Patterns ▪ Table Data Gateway – Acts as a Gateway to a database table ▪ Row Data Gateway – Acts as a Gateway to a single record in a data source ▪ Active Record – Wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data ▪ Data Mapper – A layer of Mappers that moves data between objects and database while keeping them independent
  • 29. Table Data Gateway An object that acts as a Gateway to a database table. One instance handles all the rows in the table. ▪ Also called Data Access Objects – DAO ▪ How It Works – Simple interface to a table with several find methods and methods for maintaining data – CRUD methods (Create, Read, Update, Delete) – Acts as a gateway to a table – One gateway for each table – Finders return Collection of DTOs or Record Set
  • 30. Table Data Gateway ▪ When to Use It – Works for Table Module since it is based on Record Set – Useful for web application where Domain Model is used
  • 31. Row Data Gateway An object that acts as a Gateway to a single record in a data source.There is only one instance per row. ▪ How It Works – Object that is exactly one 
 single record – Each table column is a 
 field in the object – Do not have logic – Finder object – Can be generated
  • 32. Row Data Gateway ▪ When to Use It – Works well for simple domain layer for example Transaction Script
  • 33. Active Record An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data ▪ How It Works – Each object can read and
 store itself – Contain domain logic ▪ When to Use It – When Domain Logic is not 
 too complex – When using Transaction Script +insert() +update() +delete() +getExemption() +isFlaggedForAudit(in CompanyID) +getTaxableEarnings() -lastname -firstname -NumerOfDependents Person
  • 34. Data Mapper A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself ▪ Separates the in-memory objects from the database
  • 35. Data Mapper ▪ How It works – Simple Data Mappers map in-memory object to table on a field-to- field basis – Others need to map more complicated object hierarchies to multiple tables – Mapper uses Identity Map to see if object is already loaded ▪ For insert and updates – The mapper must know what objects have changed, which are new, and which must be destroyed – Unit of Work pattern
  • 36. Data Mapper ▪ When to Use It – Database and object model must be independent – Data Mappers are useful with Domain Model – For simple Domain Model an Active Record could be used, but as it becomes more complicated some mapping is needed ▪ O/R mapping solutions can provide the mappers – For example Hibernate
  • 37. Data Mapper ▪ Simple example – Loading from the database
  • 38. Data Mapper ▪ Simple example – Updating data – Client asks the mapper to save a domain object – The mapper pulls the data out of the domain object and saves to the database
  • 41. Spring JDBC ▪ Spring JDBC packages – org.springframework.jdbc ▪ datasource – Classes for connecting to the database ▪ core – Base classes for accessing the database – JdbcTemplate ▪ object – Classes that support updating, inserting and deleting data from the database ▪ support – Utility classes
  • 42. Loading the DataSource ▪ Code ▪ Configuration – data.xml Resource res = new FileSystemResource("data.xml"); XmlBeanFactory factory = new XmlBeanFactory(res); DataSource ds = (DataSource)factory.getBean("dataSource"); <beans> <bean id="dataSource“ class= "org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>net.sourceforge.jtds.jdbc.Driver</value> </property> <property name="url”> <value>jdbc:jtds:sqlserver://honn.ru.is:1433</value> </property> <property name="username"><value>andri</value></property> <property name="password"><value>abc123</value></property> </bean> </beans>
  • 43. JdbcTemplate ▪ Main class of core package – Simplifies queries ▪ Template Method pattern – JdbcTemplate handles the 
 processing and calls our code – Dependency Injection
  • 44. JdbcTemplate Example ParameterizedRowMapper<String> rm = new ParameterizedRowMapper<String>() { public String mapRow(ResultSet rs, int rowNum) throws SQLException { return rs.getString("title"); } }; JdbcTemplate tpl = new JdbcTemplate(getDataSource()); Collection<String> col = tpl.query("select * from contents", rm); for(String s : col) { System.out.println(s); } Kenya's elephants send text messages to rangers (AP) Flexible OLEDs could be part of lighting's future (AP)
  • 45. Collecting Data ▪ Spring Interface RowMapper – An interface used by JdbcTemplate for mapping returned result sets – Class that implements this interface can be used to collect data public interface ParameterizedRowMapper<T> extends RowMapper<T> { Object mapRow(ResultSet rs, int rowNum) throws SQLException; }
  • 46. ContentRowMapper public class ContentRowMapper implements ParameterizedRowMapper<Content> { public Content mapRow(ResultSet rs, int rowNum) throws SQLException { Content content = new Content (rs.getInt (1), // id rs.getString (2), // title rs.getString (3), // link rs.getString (4), // description rs.getDate (5), // pubdate rs.getString(6)); // author return content; } }
  • 47. Using ContentRowMapper ▪ JdbcTemplate method query takes RowMapper interface as parameter ContentRowMapper crm = new ContentRowMapper(); JdbcTemplate tpl = new JdbcTemplate(ds); List l = tpl.query("select * from contents", crm); Iterator i = l.iterator(); Content cont; while (i.hasNext()) { cont = (Content) i.next(); System.out.println(cont); }
  • 48. Insert ▪ SimpleJdbcInsert – Class for inserts public int add(Content content) { SimpleJdbcInsert insertContent = new SimpleJdbcInsert(getDataSource()) .withTableName("contents") .usingGeneratedKeyColumns("id"); Map<String, Object> parameters = new HashMap<String, Object>(5); parameters.put("title", content.getTitle()); parameters.put("link", content.getLink()); parameters.put("description", content.getDescription()); parameters.put("pubdate", content.getPubDate()); parameters.put("author", content.getAuthor()); return insertContent.executeAndReturnKey(parameters).intValue(); }
  • 49. RU Data Framework Content Example
  • 50. Content Example ▪ Table contents – Contains content information CREATE TABLE contents ( id int Identity (1, 1) primary key NOT NULL, title varchar(128), link varchar(512) unique, description text, pubDate datetime, author varchar(128), )
  • 51. Content Example ▪ Gateway class for the contents table – ContentDataGateway interface contains the CRUD operations – Class ContentData implements the gateway and provides the JDBC code – Content is simple 
 JavaBean – acts as 
 Data Transfer 
 Object
  • 52. RU Data Framework ▪ Classes and interfaces for accessing the database – Implementation of the Data Gateway ▪ For Table Data Gateway – Each table has an Gateway interface – Implementation in Data classes – Factory pattern returns the implementation for each Date Gateway
  • 53. RuDataAccessFactory ▪ Factory for creating Gateway interfaces – Reads information about the DataSource – Spring Bean Definition file: data.xml – Uses Spring Bean Factory – RuDataAccessFactory reads information on each gateway interface and which classes to use as implementation – Code using the gateway interface calls getDataAccess in the factory class factory = RuDataAccessFactory.getInstance("data.xml"); contentDataGateway = (ContentDataGateway)
 factory.getDataAccess("contentDataAccess");
  • 54. Data Definition ▪ File data.xml <beans> <!-- Data Source --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driv erClassName"> <value>net.sourceforge.jtds.jdbc.Driver</value></property> <property name="url"> <value>jdbc:jtds:sqlserver://honn.ru.is:1433</value></property> <property name="username"><value>andri</value></property> <property name="password"><value>abc123</value></property> </bean> <!– Content Gateway Interface --> <bean id="contentGateway" class="is.ru.honn.tube.data.content.ContentData"/> </bean> </beans>
  • 55. DataSource ▪ DataSource is a connection to a database – Driver Class Name (net.sourceforge.jtds.jdbc.Driver) – URL (jdbc:jtds:sqlserver://honn.ru.is:1433) – username (andri) – password (abc123) ▪ RU framework uses Spring – Load the DataSource information – DriverManagerDataSource
 extends 
 AbstractDataSource
 which implements
 DataSource
  • 56. DataSource in Ru Framework ▪ RU Framework uses Spring to get DataSource
  • 58. RuDataAccess ▪ RuDataAccess is base interface for Data gateway interfaces – All gateway interfaces extend RuDataAccess – Has methods to set and get DataSource
  • 59. RuData ▪ RuData is a class implementing RuDataAccess – Handles DataSource – Data classes extend this class
  • 60. ContentDataGateway ▪ Contains all the method that are needed to manage contents – Gateway to the contents table – Pattern Table Data Gateway public interface ContentDataGateway extends RuDataAccess { public int add(Content content); public List<Content> getContents(); }
  • 61. ContentData public class ContentData extends RuData implements ContentDataGateway { public int add(Content content) { SimpleJdbcInsert insertContent = new SimpleJdbcInsert(getDataSource()) .withTableName("contents") .usingGeneratedKeyColumns("id"); Map<String, Object> parameters = new HashMap<String, Object>(5); parameters.put("title", content.getTitle()); parameters.put("link", content.getLink()); parameters.put("description", content.getDescription()); parameters.put("pubdate", content.getPubDate()); parameters.put("author", content.getAuthor()); return insertContent.executeAndReturnKey(parameters).intValue(); }
  • 62. ContentData public List getContents() { JdbcTemplate queryContent = new JdbcTemplate(getDataSource()); List<Content> contents = queryContent.query
 ("select * from contents", new ContentRowMapper()); return contents; }
  • 63. Usage public class ContentServiceData implements ContentService { private ContentDataGateway contentDataGateway = null; public ContentServiceData() { RuDataAccessFactory factory = null; try { factory = RuDataAccessFactory.getInstance("data.xml"); } catch (RuException e) { ... } contentDataGateway = (ContentDataGateway)
 factory.getDataAccess("contentDataGateway"); } public void addContent(Content content) { contentDataGateway.add(content); } public List<Content> getContents() { return contentDataGateway.getContents(); } }
  • 64. Contents ▪ Use ContentServiceData instead of a Service Stub – Change one line in the app.xml file <bean id="contentService" class="is.ru.honn.tube.service.ContentServiceData"> </bean>
  • 65. Contents ▪ Use ContentServiceData instead of a Service Stub – Change one line in the app.xml file
  • 66. RU Database ▪ hrnem.ru.is ▪ Each student has their own database ▪ Username and passwords sent out ▪ Check the access
  • 67. Summary ▪ Design Objectives – Object-relational impedance mismatch – The Usability Problem – The Legacy Problem ▪ Patterns – Table Data Gateway – Row Data Gateway – Active Record – Data Mapper – Data Transfer Object and Record set