class TodoController extends Symfony\Bundle\FrameworkBundle\Controller\Controller
{
public function addToDoAction(Request $request)
{
$todo = new ToDo();
$form = $this->createForm(new TodoType(), $todo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$list = $em->find(TodoList::class, $request->get('listId));
if(!$list) { throw new NotFoundException(); }
$todo->setTodoList($list);
$em->persist($todo);
$em->flush();
$this->mailer->sendMessage('A new ToDo has been added');
return $this->redirectToRoute('todo_show', ['id' => $todo->getId()]);
}
return $this->render('todo/new.html.twig', [
'todo' => $task,
'form' => $form->createView(),
]);
}
}
convenient code
maintainable code
class TodoController extends Symfony\Bundle\FrameworkBundle\Controller\Controller
{
public function addToDoAction(Request $request)
{
$todo = new ToDo();
$form = $this->createForm(new TodoType(), $todo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$list = $em->find(TodoList::class, $request->get('listId));
if(!$list) { throw new NotFoundException(); }
$todo->setTodoList($list);
$em->persist($todo);
$em->flush();
$this->mailer->sendMessage('A new ToDo has been added');
return $this->redirectToRoute('todo_show', ['id' => $todo->getId()]);
}
return $this->render('todo/new.html.twig', [
'todo' => $task,
'form' => $form->createView(),
]);
}
}
SLOW and Difficult
ToDoList
class TodoController extends Symfony\Bundle\FrameworkBundle\Controller\Controller
{
public function addToDoAction(Request $request)
{
$todo = new ToDo();
$form = $this->createForm(new TodoType(), $todo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$list = $em->find(TodoList::class, $request->get('listId));
if(!$list) { throw new NotFoundException(); }
$todo->setTodoList($list);
$em->persist($todo);
$em->flush();
$this->mailer->sendMessage('A new ToDo has been added');
return $this->redirectToRoute('todo_show', ['id' => $todo->getId()]);
}
return $this->render('todo/new.html.twig', [
'todo' => $task,
'form' => $form->createView(),
]);
}
}
Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.
Alistair Cockburn
The heart of software is its ability to solve domain-related problems for its users.
All other features, vital though they may be, support this basic purpose
Eric Evans, Domain Driven Design
Entities
Model
Repository
Form
Controller
Request
allow for communcation to happen
translate message from outside
?
Entity
Manager
Request
Form
Add ToDo Service
Add ToDo
Service
Repository
Entity Manager
High-level modules should not depend on low-level modules. Both should depend on abstractions
Abstractions should not depend on details. Details should depend on abstractions.
Service
RepositoryInterface
Doctrine Repository
Presentation
Domain
Persistence
Presentation
Domain
Persistence
CQRS
EVENT Sourcing
BDD
Modeling by
Example
TDD
class TodoController extends Symfony\Bundle\FrameworkBundle\Controller\Controller
{
public function addToDoAction(Request $request)
{
$todo = new ToDo();
$form = $this->createForm(new TodoType(), $todo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$list = $em->find(TodoList::class, $request->get('listId));
if(!$list) { throw new NotFoundException(); }
$todo->setTodoList($list);
$em->persist($todo);
$em->flush();
$this->mailer->sendMessage('A new ToDo has been added');
return $this->redirectToRoute('todo_show', ['id' => $todo->getId()]);
}
return $this->render('todo/new.html.twig', [
'todo' => $task,
'form' => $form->createView(),
]);
}
}
Model
class TodoController extends Symfony\Bundle\FrameworkBundle\Controller\Controller
{
public function addToDoAction(Request $request)
{
$todo = new ToDoDTO();
$form = $this->createForm(new TodoType(), $todo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$addTodoCommand = new AddTodoCommand($todo, $request->get('listId));
$this->commandHandler->handle($addTodoCommand);
return $this->redirectToRoute('todo_show', ['id' => $todo->getId()]);
}
return $this->render('todo/new.html.twig', [
'todo' => $task,
'form' => $form->createView(),
]);
}
}
DTO
Intention