Vaughn Vernon, Implementing Domain Driven Design (Perason Education, Inc: 2013), 172.
View
Controller
Model
Database
Data
Data
Data
<?php
App::uses('AppController', 'Controller');
class CustomersController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->Customer->create();
if ($this->Customer->save($this->request->data)) {
$this->Flash->success(__('The customer has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The customer could not be saved. Please, try again.'));
}
}
}
public function edit($id = null) {
if (!$this->Customer->exists($id)) {
throw new NotFoundException(__('Invalid customer'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Customer->save($this->request->data)) {
$this->Flash->success(__('The customer has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The customer could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Customer.' . $this->Customer->primaryKey => $id));
$this->request->data = $this->Customer->find('first', $options);
}
}
}
<?php
App::uses('AppController', 'Controller');
class CustomersController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->Customer->create();
if ($this->Customer->save($this->request->data)) {
$this->Flash->success(__('The customer has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The customer could not be saved. Please, try again.'));
}
}
}
public function edit($id = null) {
if (!$this->Customer->exists($id)) {
throw new NotFoundException(__('Invalid customer'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Customer->save($this->request->data)) {
$this->Flash->success(__('The customer has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The customer could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Customer.' . $this->Customer->primaryKey => $id));
$this->request->data = $this->Customer->find('first', $options);
}
}
}
"So what does the architecture of your application scream? When you look at the top level directory structure, and the source files in the highest level package; do they scream: Health Care System, or Accounting System, or Inventory Management System? Or do they scream: Rails, or Spring/Hibernate, or ASP?"
Uncle Bob
[Evans, Ref, p.16]
User Interface
Application Layer
Layered Architecture
Infrastructure
Domain Layer
Application Layer
Domain Layer
Layered Architecture
Infrastructure
Application Layer
Domain Layer
Layered Architecture
Infrastructure
Layered Architecture
Application
Domain
Infrastructure
Layered Architecture with DIP
Domain
Entity
Value Object
Application
Infrastructure
Alistair Cockburn.
Domain
Application
Infrastructure
Http
Persistence
AMPQ
Console
Http
Request
Command
Handler
ServiceBus
Entities
Value Objects
Http Land
Domain Land
<?php
class RegisterCustomer {
protected $name;
protected $email;
protected $username;
protected $password;
private function __construct(
$name, $email, $username, $password
) {
$this->name = $name,
$this->email = $email;
$this->username = $username;
$this->password = $password;
}
public static function fromRequest(
$name, $email, $username, $password
) {
return new self(
$name, $email, $username, $password
)
}
}
<?php
class RegisterCustomerHandler {
private $command;
private $repository;
public function __construct(
IRegisterUser $command,
ICustomerRepository $repository
)
{
$this->command = $name,
$this->repository = $repository;
}
public function handle()
{
$customer = new Customer(
$this->command->name,
UserName::fromString(
$this->command->username
),
Email::fromString(
$this->command->email
)
)
return $this->repository->add($customer);
}
}
IRepository
ORMRepository
MongoDB
MySQL
Domain Land
Persistence Land
ODMRepository
InMemoryRepository
CacheRepository
Redis
IRepository
<?php
interface ICostumerRepository {
public function add();
}
<?php
class CustomerRepository extends EntityRepository implements ICustomerRepository {
public function add(Customer $customer)
{
$this->em->persist($customer);
$this->em->flush();
}
}
http://geeknightrecife.github.io
Implementing Domain Driven Design - Vaughn Vernon.
Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans.
Principles of Package Design - Mathias Noback.
Clean Code Episode VII - Architecture, Use Cases, and High Level Design - Uncle Bob.
Writing Effective Use Cases - Alistair Cockburn.
http://alistair.cockburn.us/Hexagonal+architecture
@luciianoqueiroz
luciiano.queiroz@gmail.com
https://joind.in/talk/f142f