【问题标题】:Do rules on value objects applied by my domain specification对我的域规范应用的值对象执行规则
【发布时间】:2015-04-09 08:19:56
【问题描述】:

一个简单的问题可以说我的域中有一个email 值对象,而在我的域中,电子邮件可以是(null,有效的电子邮件),所以在这种情况下,如果我想将电子邮件对象传递给我的contact_info 值对象哪个选项更有效? //从ddd的角度来看

选项 1:

class email{
    public function construct($email)
    {
        if($email !== null)
        {
            // Assert that the email is valid
        }

        $this->email = $email
    }
}

class contact_info{
    public function __construct(Email $email){

    }
}

选项 2:

class email{
    public function construct($email)
    {
        // Assert that the email is valid
        $this->email = $email
    }
}

class contact_info{
    public function __construct(Email $email = null){
        if($email !== null){
            $this->email = $email;
        }
    }
}

显然选项 1 更好,其抽象的误差范围更小。但我是 ddd 的新手,所以我不确定 :)

【问题讨论】:

  • 选项 1 似乎更符合您的要求。类 contact_info 永远不应该担心得到一个空依赖,检查它是否为空并不一定会受到伤害,但绝不应该是这种情况。
  • 感谢@Andrew,我同意这一点,但您认为这会以任何方式影响我的域可扩展性吗?
  • 我什至无法回答这个问题,我并不是 PHP 或可扩展性方面的专家。但是,由于contact_info 确实希望传入一个电子邮件对象,并且正如您所说,它可以为空,因此进行一些检查不会受到伤害。如果您使用工厂来创建新对象,则应该检查它是否正在创建有效对象。
  • 我明白了@Andrew,我明白了我的想法,谢谢 :)

标签: php entity domain-driven-design value-objects


【解决方案1】:

您的第一个选项是错误的,因为该类有两种可能的状态,并且一种无效,因为null 无法填充Email 接口。想象一下,使用该类的实例,您将不得不检查电子邮件是否有效,即使电子邮件不是可选的。这使得类型安全变得毫无意义:

class Consumer{
    // email is not optional at all
    function __construct(Email $email){
        if(!$email->isActuallyAnEmail()) // WTH?
            throw new Exception;
    }
}

您尝试使用的是 NullObject 模式。但是,如果 NullObjects 用于代替仅行为接口,则效果会更好;相反,在您的情况下,您正在围绕标量数据(即电子邮件字符串)进行建模:只要 它对调用者代码似乎可以正常工作并且做得很好,就可以使用 NullObject 模式。如果该接口应该在某处返回标量数据(在您的情况下为电子邮件字符串),则实现所述接口的 NullObject 只能制作该数据。所以这实际上不是 NullObject,而是我所说的 Fake/Example/Placeholder 对象。大致:

// behavioral-only, a perfectly fine NullObject
class NullLogger implements Logger{
    function log($message){ return true; }
}

// this sucks. you are just converting null to errors
// or moving errors to another place
// this NullObject implementation is invalid
// as it doesn't actually fullfill the Email interface
class NullEmail implements Email{
    function __toString(){ throw new Exception; }
}

// not behavioral-only, an ExampleObject in this case
class ExampleEmail implements Email{
    function __toString(){ return "example@example.org"; }
}

显然,这具有完全不同的含义。它不再是可选值。数据在那里,但它是编造的;它是一个示例值、默认值或只是一个占位符。

因此,选项 2 是正确的(除非您可以将 example@example.org 附加到您的“联系信息”对象)。

如果您想了解更多关于null 处理的信息,请查看Option pattern / Maybe monad,它的理解有点复杂;你可以看看它,但它不是严格要求的。

【讨论】:

    【解决方案2】:

    这里有两个 ValueObject:电子邮件和联系信息,两者都可以有不同的不变量。

    如果您的电子邮件 VO 具有 null 值,则它应该是无效的(Email(null) 应该引发异常)。然而,即使未设置电子邮件,您的 ContactInfo 仍然有效。

    在我看来,构造函数应该只传递对象有效所需的值,如果不需要电子邮件,则不要传递它。

    当然,默认参数使事情变得更容易,但它通常隐藏了设置值的真正含义。

    在这个例子中很难看到,但是看看这个:

    
    class Person:
        def __init__(self, id, type=USER):
            self._id = id
            self._type = type
    
    VS
    
    class Person:
        def __init__(self, id):
            self._id = id
            self._type = USER
    
        def give_admin_permissions(self):
            self._type = ADMIN
    

    我们可以将 ADMIN 类型直接传递给构造函数,但它真的说明了我们所做的吗?这不是太冗长。

    但在你的例子中它很好。

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 2013-07-15
      • 2016-11-12
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多