Reputation: 2468
After introducing with domain driven design,I have realized that DDD focus on business model rather then any specific framework/language/ or technologies. As a data driven mindset holder(x), I am struggling to identify the steps to implement DDD in our real projects. I want to know what are the practical steps in real world DDD implementation. For example:
or something else ?
Upvotes: 2
Views: 2042
Reputation: 434
What I usually do at start is identify all the Entities of the domain.
For example let's take the typical blog approach.
We can have this entities, the user, the post, and the admin.
Sometimes it is not posible to identify all of them at first so instead of getting analysis paralisys I approach a code first.
So next natural step for me is identify how this entities collaborate between them. Is the user who writes the post? Then let's show it in the code:
$user->create(new Post($title, $body));
Then maybe the admin needs to review the post to accept it and show it in the page:
$admin->reviewPostFrom($user);
As you see we try to make the code as natural as we can, that's the idea, to be able to explain the code to the domain experts.
Next thing is by defining the use cases we can create the actions that our application will need.
We can use a command approach, for example:
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
}
}
And next we send the command to our command bus, who will be responsable of handling this command. It is in the command handler where all the use case happens:
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));
}
}
As you can see, we did not think a lot of things that while we are coding we realize we need. I hope it is of interest for you!!
Upvotes: 6
Reputation: 727
Domain-Driven Design encourages incremental development, not waterfall one. DDD is about the understanding of the complex domain and it simply cannot be fully discovered at one go. I would suggest to repeat often the steps you have given.
Another thing is that use cases and business requirements are highly coupled with the domain model. It's really hard to create them separately.
Upvotes: 1