mnh*_*ilu 0 domain-driven-design use-case
在引入域驱动设计之后,我意识到DDD专注于业务模型,而不是任何特定的框架/语言/或技术。作为数据驱动型思维定型者(x),我正在努力确定在实际项目中实施DDD的步骤。我想知道实际DDD实施中的实际步骤是什么。例如:
或者是其他东西 ?
小智 5
一开始我通常要做的是识别域的所有实体。
例如,让我们采用典型的博客方法。
我们可以拥有该实体,用户,帖子和管理员。
有时一开始不能一一识别所有代码,所以我没有采用分析方法,而是先编写了代码。
因此,对我而言,下一个自然的步骤是确定这些实体之间的协作方式。是写帖子的用户吗?然后让我们在代码中显示它:
$user->create(new Post($title, $body));
Run Code Online (Sandbox Code Playgroud)
然后,管理员可能需要查看帖子以接受并显示在页面中:
$admin->reviewPostFrom($user);
Run Code Online (Sandbox Code Playgroud)
如您所见,我们试图使代码尽可能自然,这就是能够向域专家解释代码的想法。
接下来的事情是通过定义用例,我们可以创建应用程序将需要的操作。
我们可以使用命令方法,例如:
class CreateNewPost
{
protected $userId;
protected $postTitle;
protected $postBody;
public function __construct(UserId $idUser, PostTitle $postTitle, PostBody $postBody)
{
// Here we can make some kind of validation of the data
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,我们将命令发送到命令总线,由其负责处理此命令。所有用例都在命令处理程序中发生:
class CreateNewPostHandler
{
// here we inject all dependencies we need to accomplish our use case
public function __construct(UserRepositoryInterface $userRepo, etc..)
{
$this->userRepository = $userRepo;
etc...
}
public function handle(CreateNewPost $command)
{
$user = $userRepo->getById($command->userId);
$user->create(new Post($command->getTitle(), $command->getBody()));
// Maybe we can launch an event here that launches a notification to admin, etc.
$this->eventDispatcher(new PostCreatedEvent($user));
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我们在编码时并没有想到很多事情,我们意识到我们需要。我希望这对您感兴趣!