l15 data source layer
DESCRIPTION
Forritun gagnaaðgangs er líklega eitt algengasta viðfangsefni við gerð enterprice lausna. Einhvern vegin verðum við að geyma stöður og gögn. Til þess eru töflugagnagrunnar (relational databases) lílkega algengasta formið af geymslu. Gallinn er sá að hlutbundin forritun fellur ekkert sérlega vel að töflugrunnum. Í þessum fyrirlestri er yfir þau vandamál sem koma upp við hönnun gagnalagsins og hvernig best er að brúa bilið milli klasa í forriti og taflna í grunni.TRANSCRIPT
![Page 1: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/1.jpg)
Lecture 15Data Source Layer
![Page 2: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/2.jpg)
Reading Object-relational impedance mismatch Data Source Patterns
– Data Transfer Object– Row Data Gateway– Table Data Gateway– Active Record– Data Mapper– Record set
![Page 3: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/3.jpg)
Agenda Design Objectives Object-Relational Impedance Mismatch Problems with database programming Data Source Patterns Spring JDBC RU Data Framework Content Example
![Page 4: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/4.jpg)
Design Objectives
![Page 5: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/5.jpg)
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”– Tends to creep into presentation and data source
Data Source– Connection to the database– Also Persistence
![Page 6: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/6.jpg)
Relational Databases We are dealing with relational databases
– Wide-spread and well understood– SQL based– These are important in enterprise software
Alternatives– NoSQL databases– O/R Mappers
![Page 7: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/7.jpg)
Connecting to Data Sources 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
ProgramDatabaseclasses
Driver Database
![Page 8: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/8.jpg)
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
![Page 9: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/9.jpg)
Impedance Mismatch
![Page 10: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/10.jpg)
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
![Page 11: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/11.jpg)
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
![Page 12: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/12.jpg)
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
![Page 13: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/13.jpg)
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
![Page 14: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/14.jpg)
Database Code Database programming can be very
repetitive– Opportunities for reusability– JDBC is too low-level
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
![Page 15: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/15.jpg)
Which of these statements is false
A) Data tends to stick where it lendsB) Database programming tends to be low-levelC) Objects tend to map nicely to the databaseD) Database programming tends to be repetitive
QUIZ
![Page 16: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/16.jpg)
Which of these statements is false
A) Data tends to stick where it lendsB) Database programming tends to be low-levelC) Objects tend to map nicely to the databaseD) Database programming tends to be repetitive
QUIZ
✔
![Page 17: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/17.jpg)
Data Source Patterns
![Page 18: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/18.jpg)
Domain Layer Patterns Recap Transaction Script
– Organizes 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
![Page 19: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/19.jpg)
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
![Page 20: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/20.jpg)
Design Example Domain Model uses gateways
![Page 21: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/21.jpg)
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
![Page 22: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/22.jpg)
Data Transfer ObjectAn 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
![Page 23: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/23.jpg)
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
![Page 24: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/24.jpg)
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
![Page 25: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/25.jpg)
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
![Page 26: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/26.jpg)
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
![Page 27: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/27.jpg)
Pattern Overview
![Page 28: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/28.jpg)
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
![Page 29: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/29.jpg)
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
![Page 30: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/30.jpg)
Table Data GatewayAn 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
![Page 31: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/31.jpg)
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
![Page 32: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/32.jpg)
Row Data GatewayAn 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
![Page 33: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/33.jpg)
Row Data Gateway When to Use It
– Works well for simple domain layer for example Transaction Script
![Page 34: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/34.jpg)
Active RecordAn 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 andstore 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
![Page 35: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/35.jpg)
Data MapperA layer of Mappers that moves data between
objects and a database while keeping them independent of each other and the
mapper itself Sparates the in-memory objects from the
databse
![Page 36: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/36.jpg)
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
![Page 37: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/37.jpg)
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
![Page 38: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/38.jpg)
Data Mapper Simple example
– Loading from the database
![Page 39: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/39.jpg)
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
![Page 40: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/40.jpg)
Data Source class maps nicely to the rows in a table and contains some useful methods
A) Row Data GatewayB) Table Data GatewayC) Active RecordD) Data Mapper
QUIZ
![Page 41: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/41.jpg)
Data Source class maps nicely to the rows in a table and contains some useful methods
A) Row Data GatewayB) Table Data GatewayC) Active RecordD) Data Mapper
QUIZ
✔
![Page 42: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/42.jpg)
Spring JDBC
![Page 43: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/43.jpg)
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
![Page 44: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/44.jpg)
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>
![Page 45: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/45.jpg)
JdbcTemplate Main class of core package
– Simplifies queries Template Method pattern
– JdbcTemplate handles the processing and calls our code
– Dependency Injection
![Page 46: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/46.jpg)
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)
![Page 47: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/47.jpg)
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; }
![Page 48: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/48.jpg)
ContentRowMapperpublic 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; }}
![Page 49: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/49.jpg)
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); }
![Page 50: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/50.jpg)
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(); }
![Page 51: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/51.jpg)
RU Data FrameworkContent Example
![Page 52: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/52.jpg)
Content Example Table contents
– Contains content informationCREATE 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),)
![Page 53: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/53.jpg)
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
![Page 54: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/54.jpg)
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
![Page 55: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/55.jpg)
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 classfactory = RuDataAccessFactory.getInstance("data.xml");contentDataGateway = (ContentDataGateway) factory.getDataAccess("contentDataAccess");
![Page 56: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/56.jpg)
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>
![Page 57: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/57.jpg)
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 AbstractDataSourcewhich implementsDataSource
![Page 58: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/58.jpg)
DataSource in Ru Framework RU Framework uses Spring to get
DataSource
![Page 59: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/59.jpg)
Using RU Framework
![Page 60: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/60.jpg)
RuDataAccess RuDataAccess is base interface for Data
gateway interfaces– All gateway interfaces extend RuDataAccess– Has methods to set and get DataSource
![Page 61: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/61.jpg)
RuData RuData is a class implementing
RuDataAccess– Handles DataSource– Data classes extend this class
![Page 62: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/62.jpg)
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();}
![Page 63: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/63.jpg)
ContentDatapublic 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(); }
![Page 64: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/64.jpg)
ContentData public List getContents() { JdbcTemplate queryContent = new JdbcTemplate(getDataSource()); List<Content> contents = queryContent.query ("select * from contents", new ContentRowMapper()); return contents; }
![Page 65: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/65.jpg)
Usagepublic 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(); }}
![Page 66: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/66.jpg)
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>
![Page 67: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/67.jpg)
Contents Use ContentServiceData instead of a
Service Stub– Change one line in the app.xml file
![Page 68: L15 Data Source Layer](https://reader033.vdocument.in/reader033/viewer/2022061301/54d4de534a7959ff088b4580/html5/thumbnails/68.jpg)
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