Skip to content

Commit 5b72510

Browse files
committed
Merge pull request iluwatar#417 from inbravo/master
Data Mapper iluwatar#34
2 parents 534fb67 + af1db79 commit 5b72510

File tree

15 files changed

+740
-1
lines changed

15 files changed

+740
-1
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ target
1111
.idea
1212
*.iml
1313
*.swp
14-
datanucleus.log
14+
datanucleus.log
15+
/bin/
16+
/bin/
17+
/bin/
18+
19+
data-mapper/src/main/resources/log4j.xml

data-mapper/etc/data-mapper.png

39.2 KB
Loading

data-mapper/etc/data-mapper.ucls

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.datamapper.Student" project="java-design-patterns"
5+
file="/java-design-patterns/java/com/iluwatar/datamapper/Student.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="359" width="148" x="868" y="175"/>
7+
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<interface id="2" language="java" name="com.iluwatar.datamapper.StudentDataMapper" project="java-design-patterns"
14+
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentDataMapper.java" binary="false"
15+
corner="BOTTOM_RIGHT">
16+
<position height="-1" width="-1" x="733" y="241"/>
17+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
18+
sort-features="false" accessors="true" visibility="true">
19+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
20+
<operations public="true" package="true" protected="true" private="true" static="true"/>
21+
</display>
22+
</interface>
23+
<class id="3" language="java" name="com.iluwatar.datamapper.StudentDataMapperImpl" project="java-design-patterns"
24+
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentDataMapperImpl.java" binary="false"
25+
corner="BOTTOM_RIGHT">
26+
<position height="-1" width="-1" x="734" y="447"/>
27+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
28+
sort-features="false" accessors="true" visibility="true">
29+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
30+
<operations public="true" package="true" protected="true" private="true" static="true"/>
31+
</display>
32+
</class>
33+
<class id="4" language="java" name="com.iluwatar.datamapper.App" project="java-design-patterns"
34+
file="/java-design-patterns/java/com/iluwatar/datamapper/App.java" binary="false" corner="BOTTOM_RIGHT">
35+
<position height="-1" width="-1" x="501" y="341"/>
36+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
37+
sort-features="false" accessors="true" visibility="true">
38+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
39+
<operations public="true" package="true" protected="true" private="true" static="true"/>
40+
</display>
41+
</class>
42+
<dependency id="5">
43+
<end type="SOURCE" refId="4"/>
44+
<end type="TARGET" refId="3"/>
45+
</dependency>
46+
<dependency id="6">
47+
<end type="SOURCE" refId="4"/>
48+
<end type="TARGET" refId="1"/>
49+
</dependency>
50+
<association id="7">
51+
<end type="SOURCE" refId="3" navigable="false">
52+
<attribute id="8" name="students"/>
53+
<multiplicity id="9" minimum="0" maximum="2147483647"/>
54+
</end>
55+
<end type="TARGET" refId="1" navigable="true"/>
56+
<display labels="true" multiplicity="true"/>
57+
</association>
58+
<dependency id="10">
59+
<end type="SOURCE" refId="4"/>
60+
<end type="TARGET" refId="2"/>
61+
</dependency>
62+
<realization id="11">
63+
<end type="SOURCE" refId="3"/>
64+
<end type="TARGET" refId="2"/>
65+
</realization>
66+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
67+
sort-features="false" accessors="true" visibility="true">
68+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
69+
<operations public="true" package="true" protected="true" private="true" static="true"/>
70+
</classifier-display>
71+
<association-display labels="true" multiplicity="true"/>
72+
</class-diagram>

data-mapper/index.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
layout: pattern
3+
title: Data Mapper
4+
folder: data-mapper
5+
permalink: /patterns/dm/
6+
categories: Persistence Tier
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Intent
13+
A layer of mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself
14+
15+
![alt text](./etc/data-mapper.png "Data Mapper")
16+
17+
## Applicability
18+
Use the Data Mapper in any of the following situations
19+
20+
* when you want to decouple data objects from DB access layer
21+
* when you want to write multiple data retrieval/persistence implementations
22+
23+
## Credits
24+
25+
* [Data Mapper](http://richard.jp.leguen.ca/tutoring/soen343-f2010/tutorials/implementing-data-mapper/)

data-mapper/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2016 Amit Dixit
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
27+
<modelVersion>4.0.0</modelVersion>
28+
<parent>
29+
<groupId>com.iluwatar</groupId>
30+
<artifactId>java-design-patterns</artifactId>
31+
<version>1.12.0-SNAPSHOT</version>
32+
</parent>
33+
<artifactId>data-mapper</artifactId>
34+
<dependencies>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>log4j</groupId>
42+
<artifactId>log4j</artifactId>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* The MIT License Copyright (c) 2016 Amit Dixit
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
package com.iluwatar.datamapper;
20+
21+
import java.util.Optional;
22+
23+
import org.apache.log4j.Logger;
24+
25+
/**
26+
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
27+
* database. Its responsibility is to transfer data between the two and also to isolate them from
28+
* each other. With Data Mapper the in-memory objects needn't know even that there's a database
29+
* present; they need no SQL interface code, and certainly no knowledge of the database schema. (The
30+
* database schema is always ignorant of the objects that use it.) Since it's a form of Mapper ,
31+
* Data Mapper itself is even unknown to the domain layer.
32+
* <p>
33+
* The below example demonstrates basic CRUD operations: Create, Read, Update, and Delete.
34+
*
35+
*/
36+
public final class App {
37+
38+
private static Logger log = Logger.getLogger(App.class);
39+
40+
/**
41+
* Program entry point.
42+
*
43+
* @param args command line args.
44+
*/
45+
public static void main(final String... args) {
46+
47+
/* Create new data mapper for type 'first' */
48+
final StudentDataMapper mapper = new StudentDataMapperImpl();
49+
50+
/* Create new student */
51+
Student student = new Student(1, "Adam", 'A');
52+
53+
/* Add student in respectibe store */
54+
mapper.insert(student);
55+
56+
log.debug("App.main(), student : " + student + ", is inserted");
57+
58+
/* Find this student */
59+
final Optional<Student> studentToBeFound = mapper.find(student.getStudentId());
60+
61+
log.debug("App.main(), student : " + studentToBeFound + ", is searched");
62+
63+
/* Update existing student object */
64+
student = new Student(student.getStudentId(), "AdamUpdated", 'A');
65+
66+
/* Update student in respectibe db */
67+
mapper.update(student);
68+
69+
log.debug("App.main(), student : " + student + ", is updated");
70+
log.debug("App.main(), student : " + student + ", is going to be deleted");
71+
72+
/* Delete student in db */
73+
mapper.delete(student);
74+
}
75+
76+
private App() {}
77+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* The MIT License Copyright (c) 2016 Amit Dixit
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5+
* associated documentation files (the "Software"), to deal in the Software without restriction,
6+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
7+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14+
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
package com.iluwatar.datamapper;
20+
21+
/**
22+
* Using Runtime Exception for avoiding dependancy on implementation exceptions. This helps in
23+
* decoupling.
24+
*
25+
* @author amit.dixit
26+
*
27+
*/
28+
public final class DataMapperException extends RuntimeException {
29+
30+
private static final long serialVersionUID = 1L;
31+
32+
/**
33+
* Constructs a new runtime exception with the specified detail message. The cause is not
34+
* initialized, and may subsequently be initialized by a call to {@link #initCause}.
35+
*
36+
* @param message the detail message. The detail message is saved for later retrieval by the
37+
* {@link #getMessage()} method.
38+
*/
39+
public DataMapperException(final String message) {
40+
super(message);
41+
}
42+
}

0 commit comments

Comments
 (0)