-
-
Notifications
You must be signed in to change notification settings - Fork 26.6k
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
Data Mapper #34 #417
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
28a5a43
basic implementation ++
inbravo deb15e2
JDBC removed...
inbravo eb72493
JDBC removed...
inbravo 59b6b81
Test/Doc added
inbravo 822ab8d
Second type mapper is updated to use java.util.vector
inbravo c53dcf1
Intent++
inbravo 06e0a15
Applicability++
inbravo 8529d6e
First review changes++
inbravo 32736fc
uml diagram++
inbravo 28c2714
review comments++
inbravo 8fa774d
mvn build file++
inbravo d5f7cb4
build check style error--
inbravo 93b0b37
build error fix++
inbravo 018a4e5
build error fix++
inbravo 59e9d02
version++
inbravo af1db79
version++
inbravo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
data-mapper/src/main/java/com/iluwatar/datamapper/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
42 changes: 42 additions & 0 deletions
42
data-mapper/src/main/java/com/iluwatar/datamapper/DataMapperException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* 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; | ||
|
||
/** | ||
* 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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!