This is my UserDAO bean defined in spring context
public class UserDAO{
public boolean isUserAlreadyExists(User user){
//some logic
}
public void createUser(User user){
//logic to add user to database
}
}
This is my spring service bean
@Component
@Transactional(readonly="true",propagation=Propation.SUPPORTD)
public class UserService{
@Autowired
UserDAO userDAO;
public void createUser(User){
if(!userDAO.isUserAlreadyExists(user)){
userDAO.createUser(user);
}
}
}
should i call isUserAlreadyExists from within UserDAO.createUser like this
//UserDAO.java
public void createUser(User user){
if(!isAlreadyUserExists(user)){
//user adding to database
}
}
OR
Above Service bean implementation is ok.
That logic seems like it should be in your service layer; as in the former example. Your
createUser()function in the DAO should only have to worry about creating, or adding the user. Let the service layer worry about what to do if theUseralready exists.Good read.