【问题标题】:Optional parameters on request class请求类的可选参数
【发布时间】:2020-02-20 12:39:28
【问题描述】:

在服务请求中处理可选参数的正确方法是什么?

假设在这种情况下,我还想将$title 作为可选参数

<?php
namespace Lw\Application\Service\Wish;
class AddWishRequest
{
    private $userId;
    private $email;
    private $content;

    public function __construct($userId, $email, $content)
    {
        $this->userId = $userId;
        $this->email = $email;
        $this->content = $content;
    }

    public function userId()
    {
        return $this->userId;
    }

    public function email()
    {
        return $this->email;
    }

    public function content()
    {
        return $this->content;
    }
}

来自here的示例

【问题讨论】:

  • 请考虑帮助您标记为正确的答案。
  • 当然,我需要先花点时间。

标签: php domain-driven-design cqrs


【解决方案1】:

您可以在任何函数调用中使用可选参数,也可以在构造函数中使用。最佳做法是,在 getter 之前“获取”。

public function __construct($userId, $email, $content, $title = "")

表示,$title 是一个可选参数。如果未提供,则将其设置为空字符串。您还可以提供任何其他类型或值。

namespace Lw\Application\Service\Wish;
class AddWishRequest
{
    private $userId;
    private $email;
    private $content;
    private $title;

    public function __construct($userId, $email, $content, $title = "")
    {
        $this->userId = $userId;
        $this->email = $email;
        $this->content = $content;
        $this->title = $title;
    }

    public function getUserId()
    {
        return $this->userId;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function getTitle()
    {
        return $this->title;
    }

}

更新

如果你只是声明一个类似的属性

private $property

然后通过$this-&gt;property 访问它,始终为空(直到您设置一个值)。您应该让 getter 负责返回正确的值。

以下示例将始终使用 NULL-coalesce 运算符返回一个数组:

  • 如果 $something 为真(或具有数组内容)将返回 $something
  • 否则将返回空数组
public function getSomething() : array {
  return $this->something ?? [];
}

【讨论】:

  • 我有 5 个参数,我应该将其中一些留空或为空?
  • 构造函数应该接收尽可能少的参数。
【解决方案2】:

通常在 DDD 中也遵循干净代码的规则,如果你有可选参数,你有多个构造函数,在这种情况下是两个:

  • 一个仅用于强制参数。

  • 一个用于所有参数,包括可选参数,但在此构造函数中它也是强制性的。

如果你想构造没有可选参数的对象,你调用第一个参数。如果你想提供一个非空的可选参数,你可以使用第二个。

通常您应该使用具有有意义名称的工厂方法,并隐藏构造函数。

AddWishRequest.create(用户 ID、电子邮件、内容)

AddWishRequest.createWithTitle (userId, email, content, title)

【讨论】:

  • 但是如果我有 5 个可选参数,我应该进行所有组合吗?
  • 通常在这种情况下,您将只有一个具有强制属性的构造函数和一个为每个可选属性具有有意义名称的 setter,以便在创建对象后设置所需属性的值。在 setter 中,参数是强制性的。
  • 我说二传手要互相理解。我说的是有意义的名称,即不要命名方法 setProperty,给它一个有意义的名称,就像对对象的任何其他操作一样。 setter 就像任何其他操作一样,它是一个更改属性值的操作。应避免将方法命名为 setProperty,而不是方法本身。
  • 我的意思是你应该避免系统地使用setter,但是如果你的类有一个真正的操作,它包括设置一个属性的值,那就去做吧,但是用一个有趣的名字。除此之外,根据干净的代码,您应该避免使用超过 2 或 3 个参数,这样就不会出现可选参数过多的问题。如果你有更多,那是因为你可以将一些包裹在一个对象中。我不知道您的域,但似乎 userId 和 email 是用户的,以及愿望的标题和内容。所以我会有 2 个 args 而不是 4 个:userDto (id, email) 和 wishDto (title, content)。
  • 我想知道您的示例是否是来自 Carlos Buenosvinos 的“最后的愿望”,确实如此。我访问了链接并看到了它。好吧,该对象似乎是一个“命令”,一个执行“addWish”应用程序服务的dto,它是不可变的,没有setter。您构建请求,然后您无法修改它。所以我不会在对象请求中设置设置器。我会选择 UserDto 和 WishDto 对象作为请求的参数,这样你的参数就少于 4 个。无论如何,检查参数是否为空是在服务中完成的,而不是在请求中
猜你喜欢
  • 1970-01-01
  • 2021-12-29
  • 2020-07-16
  • 2018-02-27
  • 2019-06-28
  • 2013-12-21
  • 2020-04-12
  • 2021-12-03
  • 2022-01-10
相关资源
最近更新 更多