Domain models

With ORM, there is a tendency to use the @OneToMany and @ManyToMany annotations to build the relationship between entities, and you can go from one entity to another easily. It does provide some convenience and reduces the amount of code.

Here, we are going to go with a different approach; we won't use these annotations to build the relationship. We will use a wrapper ID to build the relationship.

The following is how the com.taskagile.domain.model.team.Team model looks like. Some fields and methods are not listed here:

@Entity
@Table(name = "team")
public class Team extends AbstractBaseEntity {
...
@Column(name = "userId")
private long userId;
...
/**
* Create new team
*/
public static Team create(String name, UserId creatorId) {
Team team = new Team();
team.name = name;
team.archived = false;
team.userId = creatorId.value();
team.createdDate = new Date();
return team;
}

public TeamId getId() { return new TeamId(id); }

public UserId getUserId() { return new UserId(userId); }
...
}

As you can see, we use a simple userId to connect to the user who created this team. In the create() factory method, we need to pass in an instance of UserId, which is simply a wrapper of the long value.

The following is how the com.taskagile.domain.model.board.Board model looks like. Some fields and methods are not listed:

@Entity
@Table(name = "board")
public class Board extends AbstractBaseEntity {
...
@Column(name = "user_id")
private long userId;

@Column(name = "team_id")
private Long teamId;
...
/**
* Create new board
*/
public static Board create(UserId userId, String name, String
description, TeamId teamId) {
Board board = new Board();
board.userId = userId.value();
board.name = name;
board.description = description;
board.teamId = teamId.isValid() ? teamId.value() : null;
board.archived = false;
board.createdDate = new Date();
return board;
}

public BoardId getId() { return new BoardId(id); }

public UserId getUserId() { return new UserId(userId); }

public TeamId getTeamId() {
return teamId == null ? new TeamId(0) : new TeamId(teamId);
}
...
}

Because a personal board doesn't belong to any team, we need to define the teamId field as a Long value. Also, in the create() factory method, we pass in wrapped IDs of the user who created the board and the team that this board belongs to. For creating a personal board, the teamId parameter will be null. An improvement we can make here is to separate the creation of personal boards and team boards by using methods such as createPersonalBoard() and createTeamBoard()

The com.taskagile.domain.model.board.BoardMember model appears as follows:

@Entity
@Table(name = "board_member")
public class BoardMember extends AbstractBaseEntity {

@EmbeddedId
private BoardMemberId id;

public static BoardMember create(BoardId boardId, UserId userId) {
BoardMember boardMember = new BoardMember();
boardMember.id = new BoardMemberId(boardId, userId);
return boardMember;
}
...

@Embeddable
public static class BoardMemberId implements Serializable {

@Column(name = "board_id")
private long boardId;

@Column(name = "user_id")
private long userId;
...
}
}

As you can see, we create BoardMemberId with the @Embeddable annotation so that we can use the board ID and user ID as the composite id of the BoardMember entity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.151