Decoupling creation logic also comes in handy in our Services.
Using Specifications in our Services might be the best example to illustrate how to use Factories within our Services.
Consider the following Service example. Given a request from the outside world, we want to build a feed based on the latest Posts
added to the system:
namespace Application\Service; use Domain\Model\Post; use Domain\Model\PostRepository; class LatestPostsFeedService { private $postRepository; public function __construct(PostRepository $postRepository) { $this->postRepository = $postRepository; } /** * @param LatestPostsFeedRequest $request */ public function execute($request) { $posts = $this->postRepository->latestPosts($request->since); return array_map(function(Post $post) { return [ 'id' => $post->id()->id(), 'content' => $post->...