ddd on example of symfony (sfcampua14)

59
DDD on example of Symfony Oleg ZInchenko [email protected]

Upload: oleg-zinchenko

Post on 23-Jun-2015

1.624 views

Category:

Documents


4 download

TRANSCRIPT

Page 2: DDD on example of Symfony (SfCampUA14)

cystbearSymfony expert

MongoDB adept

Erlang fun

OSS doer

KNPer

https://twitter.com/1cdecoderhttps://github.com/cystbearhttp://knplabs.com/

Page 3: DDD on example of Symfony (SfCampUA14)

What this talk about?

Page 4: DDD on example of Symfony (SfCampUA14)

About useful tool/lib?

Page 5: DDD on example of Symfony (SfCampUA14)

About success story?

Page 6: DDD on example of Symfony (SfCampUA14)

No! It’s about idea

Motivation!

Page 7: DDD on example of Symfony (SfCampUA14)

MVC

Page 8: DDD on example of Symfony (SfCampUA14)

Where to store business logic?

ModelViewController

Page 9: DDD on example of Symfony (SfCampUA14)

Where to store business logic?

ModelViewController

Page 10: DDD on example of Symfony (SfCampUA14)

Where to store business logic?

ModelViewController

Page 11: DDD on example of Symfony (SfCampUA14)

Where to store business logic?

ModelViewController -- YEAH!

Page 12: DDD on example of Symfony (SfCampUA14)

Welcome to

Fat Stupid Ugly ControllersFSUC/FUC

http://blog.astrumfutura.com/2008/12/the-m-in-mvc-why-models-are-misunderstood-and-unappreciated/http://zendframework.ru/anonses/model-with-mvc

http://habrahabr.ru/post/175465/

Page 13: DDD on example of Symfony (SfCampUA14)

Anemic (Domain) Model

http://www.martinfowler.com/bliki/AnemicDomainModel.htmlhttp://habrahabr.ru/post/224879/

“In essence the problem with anemic domain models is that they incur all of the costs of a domain model, without yielding any of the benefits.”

Martin Fowler

Page 14: DDD on example of Symfony (SfCampUA14)

Persistence Layer

Model

Page 15: DDD on example of Symfony (SfCampUA14)
Page 16: DDD on example of Symfony (SfCampUA14)

What is

Not MVC (phew!)

Request / Response FrameworkHTTP Framework

http://fabien.potencier.org/article/49/what-is-symfony2

Page 17: DDD on example of Symfony (SfCampUA14)

What about model,persistence layer?

Page 18: DDD on example of Symfony (SfCampUA14)

Meet Doctrine

http://www.doctrine-project.org/

SQL -- DBAL + ORMMongoDBCouchDBOrientDBPHPCR ODMOXM

Page 20: DDD on example of Symfony (SfCampUA14)

Services

http://groovy.codehaus.org/https://grails.org/

Single ClassWith its Deps (min) setEasy to ReplaceEasy to Test

MVC(S)!

Page 21: DDD on example of Symfony (SfCampUA14)

Controller’s pray

https://twitter.com/ornicar

Get RequestSubmit form if anyCall one Service methodReturn Response

Rendering HTML far away

Page 23: DDD on example of Symfony (SfCampUA14)

Real Painclass BackendUserProgramsPossessionFormHandler{ protected $dep1; // deps holder props

public function __construct(DepsClass $dep1 /*, ...*/) { $this->dep1 = $dep1; }

public function process(Form $form) { $this->dep1->makeHappy($form); // ... }

Page 24: DDD on example of Symfony (SfCampUA14)

How Kris writes Symfony apps#44

https://twitter.com/kriswallsmithhttp://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps

Page 25: DDD on example of Symfony (SfCampUA14)

https://twitter.com/kriswallsmithhttp://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps

How Kris writes Symfony apps#44

Page 26: DDD on example of Symfony (SfCampUA14)

https://twitter.com/mr_r_miller/status/522343384900718592

Just A Thought

Page 27: DDD on example of Symfony (SfCampUA14)

Domain Logic Patterns

http://martinfowler.com/books/eaa.html

Page 28: DDD on example of Symfony (SfCampUA14)

Domain Logic Patterns

http://martinfowler.com/books/eaa.html

Transaction ScriptDomain ModelTable ModuleService Layer

Page 29: DDD on example of Symfony (SfCampUA14)

Transaction Script

Page 30: DDD on example of Symfony (SfCampUA14)

Domain Model

Page 31: DDD on example of Symfony (SfCampUA14)

Table Module

Page 32: DDD on example of Symfony (SfCampUA14)

Domain Logic&

Application logic

Page 33: DDD on example of Symfony (SfCampUA14)

Service Layer

Page 34: DDD on example of Symfony (SfCampUA14)
Page 35: DDD on example of Symfony (SfCampUA14)

What is next?RADDDD PatternsExamplesLayersGoodies

Page 36: DDD on example of Symfony (SfCampUA14)

DDD != RADCode FirstDo not Care about persistence (yet)

Page 37: DDD on example of Symfony (SfCampUA14)

Domain ModelRepositoryValue ObjectDTOStrategyState

Patterns & Code

Page 38: DDD on example of Symfony (SfCampUA14)

Domain Model

Page 39: DDD on example of Symfony (SfCampUA14)

Domain Model<?php

namespace MegaCorp\Core\Product;

class Product{ private $id; private $name; private $recognitionStrategy;

public function __construct( ProductId $id, $name, $recognitionStrategy ) { $this->id = $id; $this->name = $name; $this->recognitionStrategy = $recognitionStrategy; }

Page 40: DDD on example of Symfony (SfCampUA14)

Repository<?php

namespace MegaCorp\Core\Product;

interface ProductRepository{ public function find(ProductId $productId);

public function findAll();

public function add(Product $product);

public function remove(Product $product);}

Page 41: DDD on example of Symfony (SfCampUA14)

Value Object<?php

namespace MegaCorp\Core;

class ProductId{ private $value;

public function __construct($value) { $this->value = (string) $value; }

public function getValue() { return $this->value; }}

Page 42: DDD on example of Symfony (SfCampUA14)

Value Object DateRange

Page 43: DDD on example of Symfony (SfCampUA14)

Value Object DateRange<?php

public function findByDateRange( \DateTime $from, \DateTime $to )

class DateRange{ private $from; private $to; public function __construct(\DateTime $from, \DateTime $to) { $this->from = $from; $this->to = $to; }}

public function findByDateRange(\DateRange $range)

Page 44: DDD on example of Symfony (SfCampUA14)

Value Object Money

Page 45: DDD on example of Symfony (SfCampUA14)

<?php

class Money{ private $amount; private $currency;

public function __construct( $amount, Currency $currency ) { // ... }}

Value Object Money

Page 46: DDD on example of Symfony (SfCampUA14)

<?php

class ProfileData{ public $firstName; public $lastName; public $birthday;}

DTO

Page 47: DDD on example of Symfony (SfCampUA14)

Strategy

Page 48: DDD on example of Symfony (SfCampUA14)

______ ______ _______ _______ / | / __ \ | \ | ____|| ,----'| | | | | .--. || |__ | | | | | | | | | || __| | `----.| `--' | | '--' || |____ \______| \______/ |_______/ |_______|

Page 49: DDD on example of Symfony (SfCampUA14)

src└── MegaCorp ├── ApiBundle │ ├── Controller │ │ └── ... │ └── MegaCorpApiBundle.php ├── Core │ └── Product │ ├── Product.php │ ├── ProductId.php │ └── ProductRepository.php └── CoreBundle ├── Controller │ └── ... ├── Repository │ ├── InMemoryProductRepository.php │ └── MongoDbProductRepository.php └── MegaCorpCoreBundle.php

Directory structure

Page 50: DDD on example of Symfony (SfCampUA14)

Layers

Page 51: DDD on example of Symfony (SfCampUA14)

LayersDomain Layer -- heart of your application, Entities and Repositories

Application Layer -- ControllersPresentation Layer -- Templates / DTOs for serializerInfrastructure Layer -- framework, persistence, concrete implementations of Domain Layer

Page 52: DDD on example of Symfony (SfCampUA14)

Useful goodies

Page 53: DDD on example of Symfony (SfCampUA14)

BBB DDD by Eric Evans

http://amzn.com/0321125215/

Page 54: DDD on example of Symfony (SfCampUA14)

DDD Quickly by InfoQ

http://www.infoq.com/minibooks/domain-driven-design-quickly

Page 55: DDD on example of Symfony (SfCampUA14)

PoEAA by Martin Fowler

http://amzn.com/B008OHVDFM/

Page 56: DDD on example of Symfony (SfCampUA14)

DDD and Patterns by Jimmy Nilsson

http://amzn.com/B0054KOKQQ/

Page 58: DDD on example of Symfony (SfCampUA14)
Page 59: DDD on example of Symfony (SfCampUA14)

Thanks!