Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Mapper #34 #417

Merged
merged 16 commits into from
Apr 23, 2016
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ target
.idea
*.iml
*.swp
datanucleus.log
datanucleus.log
/bin/
/bin/
/bin/

data-mapper/src/main/resources/log4j.xml
Binary file added data-mapper/etc/data-mapper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions data-mapper/etc/data-mapper.ucls
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
<class id="1" language="java" name="com.iluwatar.datamapper.Student" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/Student.java" binary="false" corner="BOTTOM_RIGHT">
<position height="359" width="148" x="868" y="175"/>
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="2" language="java" name="com.iluwatar.datamapper.StudentDataMapper" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentDataMapper.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="733" y="241"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="3" language="java" name="com.iluwatar.datamapper.StudentDataMapperImpl" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentDataMapperImpl.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="734" y="447"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.datamapper.App" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/App.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="501" y="341"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<dependency id="5">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="3"/>
</dependency>
<dependency id="6">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="1"/>
</dependency>
<association id="7">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="8" name="students"/>
<multiplicity id="9" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<dependency id="10">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="2"/>
</dependency>
<realization id="11">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="2"/>
</realization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>
25 changes: 25 additions & 0 deletions data-mapper/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
layout: pattern
title: Data Mapper
folder: data-mapper
permalink: /patterns/dm/
categories: Persistence Tier
tags:
- Java
- Difficulty-Beginner
---

## Intent
A layer of mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself

![alt text](./etc/data-mapper.png "Data Mapper")

## Applicability
Use the Data Mapper in any of the following situations

* when you want to decouple data objects from DB access layer
* when you want to write multiple data retrieval/persistence implementations

## Credits

* [Data Mapper](http://richard.jp.leguen.ca/tutoring/soen343-f2010/tutorials/implementing-data-mapper/)
45 changes: 45 additions & 0 deletions data-mapper/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0"?>
<!--

The MIT License
Copyright (c) 2016 Amit Dixit

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.12.0-SNAPSHOT</version>
</parent>
<artifactId>data-mapper</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
</project>
77 changes: 77 additions & 0 deletions data-mapper/src/main/java/com/iluwatar/datamapper/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* The MIT License Copyright (c) 2016 Amit Dixit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.iluwatar.datamapper;

import java.util.Optional;

import org.apache.log4j.Logger;

/**
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
* database. Its responsibility is to transfer data between the two and also to isolate them from
* each other. With Data Mapper the in-memory objects needn't know even that there's a database
* present; they need no SQL interface code, and certainly no knowledge of the database schema. (The
* database schema is always ignorant of the objects that use it.) Since it's a form of Mapper ,
* Data Mapper itself is even unknown to the domain layer.
* <p>
* The below example demonstrates basic CRUD operations: Create, Read, Update, and Delete.
*
*/
public final class App {

private static Logger log = Logger.getLogger(App.class);

/**
* Program entry point.
*
* @param args command line args.
*/
public static void main(final String... args) {

/* Create new data mapper for type 'first' */
final StudentDataMapper mapper = new StudentDataMapperImpl();

/* Create new student */
Student student = new Student(1, "Adam", 'A');

/* Add student in respectibe store */
mapper.insert(student);

log.debug("App.main(), student : " + student + ", is inserted");

/* Find this student */
final Optional<Student> studentToBeFound = mapper.find(student.getStudentId());

log.debug("App.main(), student : " + studentToBeFound + ", is searched");

/* Update existing student object */
student = new Student(student.getStudentId(), "AdamUpdated", 'A');

/* Update student in respectibe db */
mapper.update(student);

log.debug("App.main(), student : " + student + ", is updated");
log.debug("App.main(), student : " + student + ", is going to be deleted");

/* Delete student in db */
mapper.delete(student);
}

private App() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* The MIT License Copyright (c) 2016 Amit Dixit
Copy link
Owner

@iluwatar iluwatar Apr 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the license header consistent. Check the other files too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.iluwatar.datamapper;

/**
* Using Runtime Exception for avoiding dependancy on implementation exceptions. This helps in
* decoupling.
*
* @author amit.dixit
*
*/
public final class DataMapperException extends RuntimeException {

private static final long serialVersionUID = 1L;

/**
* Constructs a new runtime exception with the specified detail message. The cause is not
* initialized, and may subsequently be initialized by a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for later retrieval by the
* {@link #getMessage()} method.
*/
public DataMapperException(final String message) {
super(message);
}
}
Loading