Merge pull request #9908 from SmartyAnsh/BAEL-4507-What_is_the_difference_between_DAO_and_Repository_patterns

BAEL-45087 - DAO vs Repository Pattern
This commit is contained in:
Eric Martin 2020-09-09 19:49:17 -05:00 committed by GitHub
commit 72430b8c62
9 changed files with 249 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.repositoryvsdaopattern;
import java.util.Date;
public class Tweet {
private String email;
private String tweetText;
private Date dateCreated;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTweetText() {
return tweetText;
}
public void setTweetText(String tweetText) {
this.tweetText = tweetText;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.repositoryvsdaopattern;
import java.util.List;
public interface TweetDao {
List<Tweet> fetchTweets(String email);
}

View File

@ -0,0 +1,17 @@
package com.baeldung.repositoryvsdaopattern;
import java.util.ArrayList;
import java.util.List;
public class TweetDaoImpl implements TweetDao {
@Override
public List<Tweet> fetchTweets(String email) {
List<Tweet> tweets = new ArrayList<Tweet>();
//call Twitter API and prepare Tweet object
return tweets;
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.repositoryvsdaopattern;
public class User {
private Long id;
private String userName;
private String firstName;
private String lastName;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.repositoryvsdaopattern;
public interface UserDao {
void create(User user);
User read(Long id);
void update(User user);
void delete(String userName);
}

View File

@ -0,0 +1,33 @@
package com.baeldung.repositoryvsdaopattern;
import javax.persistence.EntityManager;
public class UserDaoImpl implements UserDao {
private final EntityManager entityManager;
public UserDaoImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public void create(User user) {
entityManager.persist(user);
}
@Override
public User read(Long id) {
return entityManager.find(User.class, id);
}
@Override
public void update(User user) {
}
@Override
public void delete(String userName) {
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.repositoryvsdaopattern;
import java.util.List;
public interface UserRepository {
User get(Long id);
void add(User user);
void update(User user);
void remove(User user);
User findByUserName(String userName);
User findByEmail(String email);
List<Tweet> fetchTweets(User user);
}

View File

@ -0,0 +1,51 @@
package com.baeldung.repositoryvsdaopattern;
import java.util.List;
public class UserRepositoryImpl implements UserRepository {
private UserDaoImpl userDaoImpl;
private TweetDaoImpl tweetDaoImpl;
@Override
public User get(Long id) {
UserSocialMedia user = (UserSocialMedia) userDaoImpl.read(id);
List<Tweet> tweets = tweetDaoImpl.fetchTweets(user.getEmail());
user.setTweets(tweets);
return user;
}
@Override
public void add(User user) {
userDaoImpl.create(user);
}
@Override
public void remove(User user) {
}
@Override
public void update(User user) {
}
@Override
public List<Tweet> fetchTweets(User user) {
return tweetDaoImpl.fetchTweets(user.getEmail());
}
@Override
public User findByUserName(String userName) {
return null;
}
@Override
public User findByEmail(String email) {
return null;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.repositoryvsdaopattern;
import java.util.List;
public class UserSocialMedia extends User {
private List<Tweet> tweets;
public List<Tweet> getTweets() {
return tweets;
}
public void setTweets(List<Tweet> tweets) {
this.tweets = tweets;
}
}