DDD中使用存储库的工厂模式

时间:2019-02-22 07:25:08

标签: java domain-driven-design factory-pattern

我在下面的域中有对象Project

public class Project implements Entity<Project> {
    private ProjectId projectId;
    private ProjectTitle projectTitle;
    private ProjectDescription projectDescription;
    private ProjectStatus projectStatus;
    private ProjectNumberOfMember projectNumberOfMember;
    private ProjectDate startDate;
    private ProjectDate endDate;
    private Owner owner;
    private ProjectLgcDelFlag lgcDelFlag;
    private Admin admin;
}

所有者和管理对象从数据库中的表提供者和管理中获取。 这里是项目实体到映射表DB(我使用JPA)

    public class ProjectEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "first_name", length = 255)
    private String title;

    @Column(name = "description")
    private String description;

    @Column(name = "number_of_member")
    private int numberOfMember;

    @Column(name = "status")
    private int status;

    @Column(name = "start_date")
    private long startDate;

    @Column(name = "end_date")
    private long endDate;

    @Column(name = "owner_id")
    private long ownerId;

    @Column(name = "lgc_del_flag")
    private int lgcDelFlag;

    @Column(name = "admin_id")
    private long adminId;

    @Column(name = "created_by")
    private long createdBy;

    @Column(name = "created_at")
    private long createdAt;

    @Column(name = "updated_by")
    private long updatedBy;

    @Column(name = "updated_at")
    private long updatedAt;
}

我使用类ProjectFactory映射ProjectEntity,因此在DDD中允许注入存储库从DB获取Admin和Owner的信息?

1 个答案:

答案 0 :(得分:0)

在DDD中,经验法则是,一个聚合通过聚合根实体的ID引用另一个,因此在域的Project实体中,您不应具有OwnerId,而应具有OwnerId。管理员也一样。

因此在Project工厂中,您不需要从Owner或Admin获取信息,只需要它们的ID即可构造Project对象。