通过聚合根更新聚合的最正确方法是什么?

时间:2017-12-12 18:20:59

标签: domain-driven-design aggregate aggregateroot

遵循DDD,Aggregate和Aggregate Root的良好实践。我有以下情况:

  • User(聚合根)
  • UserEmailUser内部)
  • 的集合

想象一下,我已经注册了一个包含10封电子邮件的用户,更新其中一封电子邮件的最正确和最正确的方法是什么?

方法1

static void UpdateEmailForExistingUserMethod1()
{
    var userId = new Guid("f0cd6e3e-b95b-4dab-bb0b-7e6c6e1b0855");
    var emailId = new Guid("804aff75-8e48-4f53-b55d-8d3ca76a2df9");

    using(var repository = new UserRepository())
    {
        // I'm going to return the user with all their emails?
        // I will not have performance problems for bringing all emails from this user?
        var user = repository.GetUserById(userId);

        if (user == null)
        {
            Console.WriteLine("User not found");
            return;
        }

        // Updating Email in Aggregate Root
        user.UpdateEmail(emailId, "updated1@email.com");

        // Commit in repository
        if (repository.Commit() > 0)
        {
            Console.WriteLine("E-mail updated with method 1!");
        };
    }
}

方法2:

static void UpdateEmailForExistingUserMethod2()
{
    var usuarioId = new Guid("f0cd6e3e-b95b-4dab-bb0b-7e6c6e1b0855");
    var emailId = new Guid("3b9c2f36-659e-41e8-a1c6-d879ab58352c");

    using(var usuarioRepository = new UserRepository())
    {
        if (!usuarioRepository.UserExists(usuarioId))
        {
            Console.WriteLine("User not found");
            return;
        }

        if (!usuarioRepository.EmailExists(emailId))
        {
            Console.WriteLine("E-mail not found");
            return;
        }

        // Grab only the email that I will update from the repository, 
        // optimizing performance
        var usuarioEmail = usuarioRepository.GetEmailById(emailId);

        // Updates the e-mail through a method of the e-mail entity itself
        usuarioEmail.Update("updated2@email.com");

        // Commit in repository
        if (usuarioRepository.Commit() > 0)
        {
            Console.WriteLine("E-mail updated with method 2!");
        };
    }
}

1 个答案:

答案 0 :(得分:0)

如果User是聚合的根,那么应该通过在根上调用方法来对聚合进行所有修改;所以你的“方法1”是正确的模式。

具体而言 - 通过在根上调用方法并允许root在必要时将工作委托给内部实体来实现对聚合中其他实体的访问。

关键是聚合根定义了域模型和应用程序之间的边界。

现在,在某些情况下,这种约束似乎没有多大意义。当这种情况发生时,挑战你的假设:你确定电子邮件是一个实体吗?你确定实体需要与用户实体在事务上保持一致吗?

对于像电子邮件地址这样的东西,我希望电子邮件地址将成为一个价值对象,可以添加到用户内部的集合中。所以我不希望将EmailId视为一种抽象。

user.FixTypoInEmailAddress("updated@email.com", "updated1@email.com")
  

不要超出必要的实体。