Returning Values

After signing up, we might be thinking about redirecting the user to a profile page. The natural way of passing the required information back to the controller is to return the User Entity directly from the Service:

class SignUpUserService
{
// ...

public function execute(SignUpUserRequest $request)
{
$user = new User(
$this->userRepository->nextIdentity(),
$email,
$password
);

$this->userRepository->add($user);

return $user;
}
}

Then, from the controller, we would pick up the id field and redirect to some other place. However, think twice about what we've just done. We returned a full-featured Entity to the controller, which will allow the delivery mechanism to bypass the Application Layer and interact directly with the Domain.

Imagine the User Entity offers an updateEmailAddress method. You could try to prevent it, but at some point in the future, somebody might think about using it:

$app-> match( '/signup' , function (Request $request) use ($app) {
// ...
$user = $app['sign_up_user_application_service']->execute(
new SignUpUserRequest(
$request->get('email'),
$request->get('password'))
);
$user->updateEmailAddress('[email protected]');
// ...
});

Not only that, but the data that the presentation layer needs is not the same that the Domain manages. We don't want to evolve and couple the Domain layer around the presentation layer. Instead, we want them to evolve freely.

To do this, we need a flexible way of decoupling both layers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.137.240