Slide 1

Slide 1 text

Applying DDD when building Drupal modules using Symfony components Marek Matulka

Slide 2

Slide 2 text

Who am I?

Slide 3

Slide 3 text

Marek Matulka @super_marek mareg

Slide 4

Slide 4 text

Software Engineer at

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Background

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Requirement CMS - manage corporate website

Slide 11

Slide 11 text

Requirement CMS - manage corporate website - manage other brands’ websites

Slide 12

Slide 12 text

Requirement CMS - manage corporate website - manage other brands’ websites - build seasonal microsites

Slide 13

Slide 13 text

Requirement CMS - manage corporate website - manage other brands’ websites - build seasonal microsites - integrate with Magento store

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Why Drupal? - open source CMS

Slide 16

Slide 16 text

Why Drupal? - open source CMS - easy to install and manage

Slide 17

Slide 17 text

Why Drupal? - open source CMS - easy to install and manage - most required functionality available out of the box

Slide 18

Slide 18 text

Why Drupal? - open source CMS - easy to install and manage - most required functionality available out of the box - easy to extend

Slide 19

Slide 19 text

Why Drupal 7?

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

What is Symfony?

Slide 23

Slide 23 text

is a powerful framework

Slide 24

Slide 24 text

Doctrine ORM Twig

Slide 25

Slide 25 text

set of decoupled components

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Domain Driven Design

Slide 28

Slide 28 text

In order to create a good software, you have to know what the software is all about. – Eric Evans

Slide 29

Slide 29 text

How do we learn?

Slide 30

Slide 30 text

Stakeholders

Slide 31

Slide 31 text

Ask for examples... ...write stories!

Slide 32

Slide 32 text

Vocabulary

Slide 33

Slide 33 text

Ubiquitous Language Ubiquitous Language

Slide 34

Slide 34 text

Ubiquitous Language Ubiquitous Language Domain Expert Analyst Tester Developer Developer

Slide 35

Slide 35 text

Ubiquitous Language Ubiquitous Language Domain Expert Analyst Tester Developer Developer Application Code Acceptance Criteria Specs and documentation

Slide 36

Slide 36 text

Example?

Slide 37

Slide 37 text

Example Feature: In order to see correct prices As a visitor I need to be able to locate the nearest depot Scenario: Given I am visiting a product page for the first time And I got prompted to locate my nearest depot When I enter my post code Then I should see my local depot information

Slide 38

Slide 38 text

Example Feature: In order to see correct prices As a visitor I need to be able to locate the nearest depot Scenario: Given I am visiting a product page for the first time And I got prompted to locate my nearest depot When I enter my post code Then I should see my local depot information

Slide 39

Slide 39 text

Domain namespace Acme\Depot; interface DepotLocator { /** * @param string $postcode * * @return Depot */ public function locateDepot($postcode); }

Slide 40

Slide 40 text

Implementation namespace Acme\MagentoAdapter; use Acme\Depot\DepotLocator; class MagentoDepotLocator implements DepotLocator { public function locateDepot($postcode) { return $this->entityMapper->mapDepot( $this->clientAdapter->findDepotByPostcode($postcode) );

Slide 41

Slide 41 text

why interface?

Slide 42

Slide 42 text

Layered Architecture “High level modules should not depend on lower level implementation” – Robert C. Martin User Interface Application Domain Infrastructure

Slide 43

Slide 43 text

Dependency Inversion Policy Layer Policy Service Mechanism Layer Mechanism Service Utility Layer

Slide 44

Slide 44 text

Layered Architecture User Interface Application Domain Infrastructure

Slide 45

Slide 45 text

Layered Architecture User Interface Application Domain Infrastructure

Slide 46

Slide 46 text

Clean Architecture Entities Use Cases Controllers Web

Slide 47

Slide 47 text

Clean Architecture Entities Use Cases Controllers Web Framework Drivers Interface Adapters Application Business Rules Enterprise Business Rules

Slide 48

Slide 48 text

Hexagonal Architecture UI Adapter Log Adapter Data Storage Adapter External Data Adapter Application Domain

Slide 49

Slide 49 text

Hexagonal Architecture Acme\Depot\DepotLocator Acme\MagentoAdapter\MagentoDepotLocator

Slide 50

Slide 50 text

Hexagonal Architecture Acme\Depot\DepotLocator Acme\MagentoAdapter\MagentoDepotLocator port adapter

Slide 51

Slide 51 text

Why clean architecture?

Slide 52

Slide 52 text

Why clean architecture? - your code is clean(er)

Slide 53

Slide 53 text

Why clean architecture? - your code is clean - decoupled from framework

Slide 54

Slide 54 text

Why clean architecture? - your code is clean - decoupled from framework - reusable

Slide 55

Slide 55 text

Why clean architecture? - your code is clean - decoupled from framework - reusable - easy to test

Slide 56

Slide 56 text

Why clean architecture? - your code is clean - decoupled from framework - reusable - easy to test - easier to maintain

Slide 57

Slide 57 text

Why clean architecture? - your code is clean - decoupled from framework - re-usable - easy to test - easier to maintain - easier to change

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

When to use DDD?

Slide 60

Slide 60 text

When to use DDD? - complex domain

Slide 61

Slide 61 text

When to use DDD? - complex domain - access to domain experts

Slide 62

Slide 62 text

When to use DDD? - complex domain - access to domain experts - iterative, long term process

Slide 63

Slide 63 text

When to use DDD? - complex domain - access to domain experts - iterative, long term process - OO design

Slide 64

Slide 64 text

When not to use DDD?

Slide 65

Slide 65 text

When not to use DDD? - simple domain

Slide 66

Slide 66 text

When not to use DDD? - simple domain - data centric domains

Slide 67

Slide 67 text

When not to use DDD? - simple domain - data centric domains - prototyping

Slide 68

Slide 68 text

Symfony with Drupal 7

Slide 69

Slide 69 text

composer.json "require": { "php": ">=5.4.0", "symfony/console": "~2.5", "drush/drush": "7.*@dev", "symfony/finder": "~2.5", "symfony/dependency-injection": "~2.5", "symfony/config": "~2.5", "symfony/filesystem": "~2.6", "doctrine/dbal": "~2.5", "rhumsaa/uuid": "~2.8" },

Slide 70

Slide 70 text

AppKernel.php public function boot() { $this->container = new ContainerBuilder(); $this->container->registerExtension(new ApplicationExtension()); $loader = new YamlFileLoader($this->container, new FileLocator('app/config')); $loader->load('parameters.yml'); $loader = new XmlFileLoader($this->container, new FileLocator('/Resources/config')); $loader->load('services.xml'); $this->container->compile(); } public function getContainer() { return $this->container; }

Slide 71

Slide 71 text

settings.php require_once DRUPAL_ROOT . '/../vendor/autoload. php'; $kernel = new \Acme\Application\Kernel(); $kernel->boot(); $conf['di_container'] = $kernel->getContainer();

Slide 72

Slide 72 text

database.php $databases = [ 'default' => [ 'default' => [ 'database' => $conf['di_container']->getParameter('database.name'), 'username' => $conf['di_container']->getParameter('database.username'), 'password' => $conf['di_container']->getParameter('database.password'), 'host' => $conf['di_container']->getParameter('database.host'), 'port' => $conf['di_container']->getParameter('database.port'), 'driver' => 'mysql', 'prefix' => '', ], ], ];

Slide 73

Slide 73 text

depot_locator.module function get_depot_json($postcode) { global $conf; $depotEntity = $conf['di_container']->get('acme.depot_locator') ->locateDepot($postcode); $depot = [ 'code' => $depotEntity->getCode(), 'name' => $depotEntity->getName(), 'postcode' => $depotEntity->getPostcode(), ]; return ['depot' => $depot]; }

Slide 74

Slide 74 text

(to get Drupal 7 working with Symfony components) That’s it!

Slide 75

Slide 75 text

What I’ve learnt?

Slide 76

Slide 76 text

Drupal is a great CMS platform

Slide 77

Slide 77 text

Building your own framework is easy with Symfony Components

Slide 78

Slide 78 text

Extending Drupal 7 with Symfony components is easy

Slide 79

Slide 79 text

Questions?

Slide 80

Slide 80 text

Thank you! https://speakerdeck.com/super_marek @super_marek