DDD - 存储库和多对多关系

发布于 2024-11-29 07:13:07 字数 824 浏览 0 评论 0原文

我是 DDD 新手,我被困在这里: 我在两个实体之间存在多对多关系:用户和组。该关系不是聚合,因为用户可以在没有组的情况下存在,组也可以在没有用户的情况下存在。

这是我的 User 类的代码:

public class User {

    private List<Group> groups = new ArrayList<Group>();
    
    private UserRepository userRepository;

    public void create() throws Exception{
        userRepository.create(this);        
        
        // I have to update the groups with the user.
        for (Group group : groups) {
            group.update();
        }
    }
    
     public void addGroup(Group group){
        if (!groups.contains(group)) {
            groups.add(group);
            group.addUser(this);
         }
    }
}

问题是,当我创建一个具有组的 User 时,我不知道在哪里关联这些类(我无法使用 ORM)。我是在 User 的 create 方法中完成的,并且我还通过 Spring 管理其中的事务。这是正确的吗?或者我应该将该代码放在 userRepository 或服务中?

谢谢!

I am new to DDD and I am stuck here:
I have a many-to-many relationship between two entities: User and Group. The relationship is not an Aggregate because a User can exist without a Group and a Group can exist without a User.

This is my code for the User class:

public class User {

    private List<Group> groups = new ArrayList<Group>();
    
    private UserRepository userRepository;

    public void create() throws Exception{
        userRepository.create(this);        
        
        // I have to update the groups with the user.
        for (Group group : groups) {
            group.update();
        }
    }
    
     public void addGroup(Group group){
        if (!groups.contains(group)) {
            groups.add(group);
            group.addUser(this);
         }
    }
}

The problem is that I don't know where to associate those classes when I create a User who has groups in (I cannot use ORM). I made it in the create method of User, and I also manage transactions there through Spring. Is this correct? Or should I put that code in the userRepository or in a Service?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸩远一方 2024-12-06 07:13:07

根据埃里克·埃文斯 (Eric Evans) 的书,多对多关联会使模型过于复杂。尝试将它们替换为
2 多对一关联。多对多关联的一部分总是有更多
商业价值高于其他。

在你的情况下,我会保留 2 个聚合 - UserGroup,其中 User 聚合负责保存一堆 GroupReferences.

According Eric Evans' book, many to many associations make your model too complex. Try to replace them with
2 many-to-one associations. One part of many-to-many association always has more
business value than other.

In your case I would stay with 2 aggregates - User and Group, where User aggregate is responsible for holding bunch of GroupReferences.

烏雲後面有陽光 2024-12-06 07:13:07

我是在User的create方法中实现的,并且还通过Spring管理事务。这是正确的吗?

此逻辑的正确性取决于存储库的编写方式及其合同。如果存储库不会保留任何关系,则可能表明存储库设计不当。我希望存储库能够实现存储实体关联所需的逻辑,而不仅仅是实体的简单属性。

基于 ORM 解决方案的存储库可以通过级联持久性事件使这变得更容易,但在您的情况下,它可能会导致两个存储库中的代码重复(我假设您有一个 GroupRepositoryGroupRepository 以及)。如果您决定哪个实体拥有该关系,然后让相应的存储库保留该关联,可能会更好。因此,关联中的任何更改都只能由拥有关联的实体来完成(在您的情况下,它似乎是 User ,并且推断 UserRepository 应该管理关联)。

I made it in the create method of User, and also manage transactions there through Spring. Is this correct?

Correctness of this logic would depend on how the repository is written and it's contract. If the repository will not persist any relationships, then it could be a sign of a poorly designed repository. I would expect a repository to implement the necessary logic to store entity associations as well, and not just the simple properties of the entity.

A repository based on an ORM solution would have made this easier by through the cascading of persistence events, but in your case, it might lead to duplication of code in both your repositories (I am assuming that you have a GroupRepository as well). It might be better if you decide on which entity owns the relationship, and then have the association persisted by the corresponding repository. Any changes in the association should therefore be done only by the entity that owns the association (it appears to be User in your case, and by inference the UserRepository should manage the association).

¢好甜 2024-12-06 07:13:07

卢卡斯,我会:

1)创建一个用户工厂,它可以接收应与新用户关联的组。工厂可以使用 addGroup 方法来建立该关联。

2) 从 User 类中删除对 UserRepository 的引用。

Lucas, I would:

1) Create a User factory that can take in the Groups that should be associated with the new User. The factory can use the addGroup method to make that association.

2) Remove your reference to the UserRepository from the User class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文