Couple Architecture Components loosely

Waddlematch heeft een onderdeel match, waarin een match wordt gemaakt tussen twee gebruikers. In eerdere versies van het project werd er in de Match-DAO een UserDAO aangeroepen. Hierin is te zien dat de componenten aan elkaar worden gelinkt, wat niet de bedoeling is.


@Repository
public class JdbcMatchDAO implements MatchDAO {
    private final JdbcUserDAO jdbcUserDAO;
    JdbcTemplate jdbcTemplate;
    DataSource dataSource;

    @Autowired
    public JdbcMatchDAO(JdbcTemplate jdbcTemplate, DataSource dataSource, JdbcUserDAO jdbcUserDAO) {
        this.jdbcTemplate = jdbcTemplate;
        this.dataSource = dataSource;
        this.jdbcUserDAO = jdbcUserDAO;
    }

        private class MatchRowMapper implements RowMapper<Match> {
        @Override
        public Match mapRow(ResultSet resultSet, int rowNumber)
                throws SQLException {
            return new Match(
                    resultSet.getInt("match_id"),
                    resultSet.getInt("user_id"),
                    resultSet.getInt("status"),
                    resultSet.getInt("liked_profile"),
                    resultSet.getTimestamp("first_match_ts"),
                    resultSet.getTimestamp("full_match_ts"));

@Repository
public class MatchRepo {
    private final MatchDAO matchDAO;
    private final JdbcUserDAO userDAO;

    public MatchRepo(MatchDAO matchDAO, JdbcUserDAO userDAO)
{
        this.matchDAO = matchDAO;
        this.userDAO = userDAO;
    }

public class Match {
    private int match_id;
    private User currentUser;
    private int status;
    private User likedUser;
    private Timestamp first_match_ts;
    private Timestamp full_match_ts;
    private int currentUserId;
    private int likedUserId;

Dit is echter niet gewenst omdat op deze manier de componenten afhankelijk van elkaar worden. Dit is opgelost door de gebruiker in plaats van in de DAO aan te roepen, nu in de Match-Repository wordt toegevoegd en in plaats van de User-DAO wordt nu de User-Repo gebruikt. Hierdoor blijven de componenten los van elkaar.


@Repository
public class JdbcMatchDAO implements MatchDAO {
    JdbcTemplate jdbcTemplate;
    DataSource dataSource;

    @Autowired
    public JdbcMatchDAO(JdbcTemplate jdbcTemplate, DataSource dataSource) {
        this.jdbcTemplate = jdbcTemplate;
        this.dataSource = dataSource;
    }

    private class MatchRowMapper implements RowMapper<Match> {
        @Override
        public Match mapRow(ResultSet resultSet, int rowNumber)
                throws SQLException {
            return new Match(
                    resultSet.getInt("match_id"),
                    null, //will be completed in Repo
                    resultSet.getInt("status"),
                    null, //will be completed in Repo
                    resultSet.getTimestamp("first_match_ts"),
                    resultSet.getTimestamp("full_match_ts"));
        }


@Repository
public class MatchRepo {
    private final MatchDAO matchDAO;
    private final JdbcUserDAO userDAO;
    private final UserRepo userRepo;

        private Optional<Match> buildMatch(int id) {
        Optional<Match> matchOptional = matchDAO.getOneById(id);
        if (matchOptional.isPresent()) {
            Match match = matchOptional.get();
            Optional<User> currentUserOptional = userRepo.getUserById(matchDAO.getCurrentUserFromMatch(id));
            currentUserOptional.ifPresent(match::setCurrentUser);
            Optional<User> likedUserOptional = userRepo.getUserById(matchDAO.getLikedUserFromMatch(id));
            likedUserOptional.ifPresent(match::setLikedUser);
            return Optional.of(match);
        } else {
            return Optional.empty();
        }

public class Match {
    private int match_id;
    private User currentUser;
    private int status;
    private User likedUser;
    private Timestamp first_match_ts;
    private Timestamp full_match_ts;