Tell me anybody, is it good practice to call service layer methods through domain object getters? Let me show you with an example:
public class User {
private long id;
private String name;
private String email;
private Address address;
// constructors ...
// getters and setters...
public Address getAddress() {
if (address == null) {
address = new UserService().getAddress(this);
}
return address;
}
}
public class UserService {
public Address getAddress(User user) throws SQLException {
return dao.getAddress(user.getId()); // execute query to user table for retrieving addr link and execute query to addr table for retrieving addr
}
// etc.
}
Some details of the database architecture: User and Address are different database tables. The address field in the user table contains a link to the corresponding row in the address table.
Users (most likelyUserServiceitself) should check ifaddressis null. In addition to tying aUserto a specificUserService, you also are coupling a specific type ofUserService.