79
(println "Hello World! :)")

PHP Barcelona Monthly Talk Feb 2015

Embed Size (px)

Citation preview

Page 1: PHP Barcelona Monthly Talk Feb 2015

(println "Hello World! :)")

Page 2: PHP Barcelona Monthly Talk Feb 2015

DDD as an implementation detail

Eloi PochSergi González

Page 3: PHP Barcelona Monthly Talk Feb 2015

Who$are$we?$$

!es.linkedin.com/in/eloipoch/$

$

$@eloipoch$

[email protected]$

Eloi!Poch!!Technical!Lead!at!@Akamon!•  So8ware$Developer$•  Clojure$developer$wannabe$•  Want$to$code$in$Castefa$beach$while$drinking$wine$

Page 4: PHP Barcelona Monthly Talk Feb 2015

(conj '(1 2 3) 4)

Page 5: PHP Barcelona Monthly Talk Feb 2015

Who$are$we?$$

!es.linkedin.com/in/sergigp/$

$

$“SergiGP$

[email protected]$

Sergi!!Technical!Lead!at!@Akamon!•  So<ware$Developer$•  Chief$Trolling$Officer$•  Two$animals$remaining$to$have$an$urban$zoo$

Page 6: PHP Barcelona Monthly Talk Feb 2015
Page 7: PHP Barcelona Monthly Talk Feb 2015

Akamon

Page 8: PHP Barcelona Monthly Talk Feb 2015

A little bit on Akamon before we deal with the technical stuff

Page 9: PHP Barcelona Monthly Talk Feb 2015

We#develop#Social#Casino#Games#

Page 10: PHP Barcelona Monthly Talk Feb 2015

Por$olio'of'Facebook'games'

Page 11: PHP Barcelona Monthly Talk Feb 2015

Por$olio'of'Mobile'apps'

Page 12: PHP Barcelona Monthly Talk Feb 2015

Recently(launched(a(new(Casino(Suite(

Page 13: PHP Barcelona Monthly Talk Feb 2015

Technically …

Page 14: PHP Barcelona Monthly Talk Feb 2015

Flash Client Game Server Backend

AS3 Nova

Java JReactive & Vertx

PHP Horus

Page 15: PHP Barcelona Monthly Talk Feb 2015
Page 16: PHP Barcelona Monthly Talk Feb 2015

@eloipoch @jordillonch @SergiGP

@jorgeavila_ss @jdiezc @jvalduvieco @amassa5

Who we are? The backend team

Page 17: PHP Barcelona Monthly Talk Feb 2015

Development Problems

Page 18: PHP Barcelona Monthly Talk Feb 2015

Our expectations…

Page 19: PHP Barcelona Monthly Talk Feb 2015

PerformanceBig DataDifficult AlgorithmsConcurrencyReal time analyticsScalability

Page 20: PHP Barcelona Monthly Talk Feb 2015

The ugly truth…

Page 21: PHP Barcelona Monthly Talk Feb 2015

Technical Problems

Page 22: PHP Barcelona Monthly Talk Feb 2015

Code ComplexityBig ball of mudShared StateCoupling

Page 23: PHP Barcelona Monthly Talk Feb 2015
Page 24: PHP Barcelona Monthly Talk Feb 2015

& Non-Technical problems

Page 25: PHP Barcelona Monthly Talk Feb 2015

Different Cultures

Page 26: PHP Barcelona Monthly Talk Feb 2015

Tackling Complex CodeThe Philosophy

Page 27: PHP Barcelona Monthly Talk Feb 2015

Simple made Easy

Page 28: PHP Barcelona Monthly Talk Feb 2015

Complex((

One(role(

One(task(

not(a(single(opera2on(

(

OBJECTIVE

Hard(

Familiar,(near(your(

knowledge(

(

(

Simple(vs(Easy((

SUBJECTIVEOBJECTIVE

Page 29: PHP Barcelona Monthly Talk Feb 2015

Simple vs Easy

$mysqli = new mysqli("localhost", "horus", "olakease", “suite"); $mysqli->query("INSERT INTO users …”))

SIMPLE

EASY

/** * @ORM\Entity * @ORM\Table(name="product") */ class Product

Page 30: PHP Barcelona Monthly Talk Feb 2015

$sum = function ($carry, $item) { return $carry + $item; } array_reduce([1, 2, 3], $sum);

$acc = 0; foreach ([1, 2, 3] as $value) {

$acc += $value; }

SIMPLE

EASY

Simple vs EasySimple vs Easy

Page 31: PHP Barcelona Monthly Talk Feb 2015

$acc = 0; foreach ([1, 2, 3] as $value) {

$acc += $value; }

SIMPLE

EASY

(reduce + [1 2 3])

Simple vs EasySimple vs Easy

Page 32: PHP Barcelona Monthly Talk Feb 2015

Simplicity vs Easiness

• Easy to understand

• Easy to debug

• Easy to change, flexibility, maintenable

• Easy to use

• Hard to debug

• Hard to change

Page 33: PHP Barcelona Monthly Talk Feb 2015

Complected

• Something does more than one thing and you can’t split it easily

• Source of accidental complexity

• DON’T DO IT

Page 34: PHP Barcelona Monthly Talk Feb 2015

Essential ComplexityVS

Accidental Complexity

Page 35: PHP Barcelona Monthly Talk Feb 2015

Do not complect, compose

Page 36: PHP Barcelona Monthly Talk Feb 2015

Tackling Complex Code

The Theory

Page 37: PHP Barcelona Monthly Talk Feb 2015

Organisational level

Page 38: PHP Barcelona Monthly Talk Feb 2015

Pair Programming

Page 39: PHP Barcelona Monthly Talk Feb 2015

Pull Requests

Page 40: PHP Barcelona Monthly Talk Feb 2015

Code Reviews

Page 41: PHP Barcelona Monthly Talk Feb 2015

Continuous Integration

Page 42: PHP Barcelona Monthly Talk Feb 2015

Self Empowerment

Never ask for permission unless it

would be reckless not to

Page 43: PHP Barcelona Monthly Talk Feb 2015

Trust

Great teams are unafraid to air their dirty laundry, admit their mistakes, their weaknesses and their concerns

without fear. Patrick Lencioni

Page 44: PHP Barcelona Monthly Talk Feb 2015

Technical Level

Page 45: PHP Barcelona Monthly Talk Feb 2015

Lean Approach

Page 46: PHP Barcelona Monthly Talk Feb 2015

Any model/solution is provisional

Page 47: PHP Barcelona Monthly Talk Feb 2015

YAGNIYou Aren’t Gonna Need It

Page 48: PHP Barcelona Monthly Talk Feb 2015

DDD Concepts

Page 49: PHP Barcelona Monthly Talk Feb 2015

• DDD Tactical:

• Application Services

• Domain Services

• Domain Events

• Repositories

• AR & VO

• DDD Strategical:

• Modules

• Bounded Contexts

• SubDomains

• Context Maps

Page 50: PHP Barcelona Monthly Talk Feb 2015

CQRS Concepts

Page 51: PHP Barcelona Monthly Talk Feb 2015
Page 52: PHP Barcelona Monthly Talk Feb 2015

Hexagonal Architecture

Page 53: PHP Barcelona Monthly Talk Feb 2015
Page 54: PHP Barcelona Monthly Talk Feb 2015

TDD & BDD

Page 55: PHP Barcelona Monthly Talk Feb 2015
Page 56: PHP Barcelona Monthly Talk Feb 2015

Tackling Complex Code

The Practice

Page 57: PHP Barcelona Monthly Talk Feb 2015

Divide, Test & Conquer

Page 58: PHP Barcelona Monthly Talk Feb 2015

Product Entity Example

Page 59: PHP Barcelona Monthly Talk Feb 2015

ProductId

NameDescription

PriceCurrencyImage Url

CoinsStoreId

Page 60: PHP Barcelona Monthly Talk Feb 2015

Product

Product Descriptions

Product Prices

Stores

Product Benefits

Product Images

Page 61: PHP Barcelona Monthly Talk Feb 2015

ProductId

Product Description

Product Price

Stores

Product Benefit

Product Image

DescriptionName Currency

Amount

CoinsStoreId

Image Url

Page 62: PHP Barcelona Monthly Talk Feb 2015

ProductId

Product Description

Product Price

Stores

Product Benefit

Product Image

DescriptionName Currency

Amount

CoinsStoreId

Image Url

Product BC

Page 63: PHP Barcelona Monthly Talk Feb 2015

ProductId

Product Description

Product Price

Stores

Product Benefit

Product Image

DescriptionName Currency

Amount

CoinsStoreId

Image Url

Pricing/Sales BC

Product BC

Page 64: PHP Barcelona Monthly Talk Feb 2015

Product

Id

BenefitId

Product Price

ProductId

Amount

Currency

Product Description

ProductId

Name

Description

Product Benefit

Id

Amount

VirtualMoneyId

Product Store

ProductId

StoreId

Page 65: PHP Barcelona Monthly Talk Feb 2015
Page 66: PHP Barcelona Monthly Talk Feb 2015

HORUS

Page 67: PHP Barcelona Monthly Talk Feb 2015

Big Picture

• Directories: 773

• Files: 1501

• Lines of code: 113885

• Non-Comment Lines of Code: 88806 (80%)

• Average Class Length: 14 lines

• Average Method Length: 2 lines

• Cyclomatic Complexity / LLOC: 0.06

• Cyclomatic Complexity / Number of Methods: 1.21

HORUS

Page 68: PHP Barcelona Monthly Talk Feb 2015

Big Picture of our Code in Horus

• Directories: 773 / 558

• Files: 1501 / 1001

• Lines of code: 113885 / 41205

• Non-Comment Lines of Code: 88806 (80%) / 38045 (92%)

• Average Class Length: 14 lines / 5 lines

• Average Method Length: 2 lines / 2 lines

• Cyclomatic Complexity / LLOC: 0.06 / 0.03

• Cyclomatic Complexity / Number of Methods: 1.21 / 1.14

Page 69: PHP Barcelona Monthly Talk Feb 2015
Page 70: PHP Barcelona Monthly Talk Feb 2015

UserPostController Command Bus

UserRegistratorCommandHandler

UserRegistratorDomain Event

Publisher

UserRegisteredDomainEventSubscriber

UserAccountOpener

User UserRepository Event Subscribers

.

.

.

POST /users[

userId: 0db90987-5ce1-4683-bb05-7d1b5539cb0b]

Command Handlers

.

.

.

UserR

egisteredDom

ainEvent

User

HORUS

BASIC REQUEST

UserRegistrationCommand

UserRegistrationCommand

userIdanalyticsContextId

userIdanalyticsContextId

UserRegisteredDomainEvent

userId

analyticsContextId

id (Economy Account)

Page 71: PHP Barcelona Monthly Talk Feb 2015

UserPostController Command Bus

UserRegistratorCommandHandler

UserRegistratorDomain Event

Publisher

UserRegisteredDomainEventSubscriber

UserAccountOpener

User UserRepository Event Subscribers

.

.

.

POST /users[

userId: 0db90987-5ce1-4683-bb05-7d1b5539cb0b]

Command Handlers

.

.

.

UserR

egisteredDom

ainEvent

User

HORUS

BASIC REQUEST

UserRegistrationCommand

UserRegistrationCommand

userIdanalyticsContextId

userIdanalyticsContextId

UserRegisteredDomainEvent

userId

analyticsContextId

id (Economy Account)

UserModule

EconomyModule

Page 72: PHP Barcelona Monthly Talk Feb 2015

Operational

Analytics

Support

Marketing

DE

C

Q

Q

We are still COUPLED !!

Page 73: PHP Barcelona Monthly Talk Feb 2015

Operational

Analytics

Support

Marketing

HTTP

Page 74: PHP Barcelona Monthly Talk Feb 2015

Tackling Complex Code

The Conclusions

Page 75: PHP Barcelona Monthly Talk Feb 2015

Mistakes

• Do not store domain events

• Own libraries for CQRS & DDD

• Do not use Domain Services

• Domain events with wrong identifiers

• Couple App Services to Commands & Queries

• Not start earlier some Modules/Bounded Contexts

Page 76: PHP Barcelona Monthly Talk Feb 2015

Mistakes

• Let the DB guide us

• Let the framework guide us

Page 77: PHP Barcelona Monthly Talk Feb 2015

Problems

• Domain Events: Lost of order (async)

• Doctrine

• Unit Of Work + Integration Test = Fail

• Embedded: Identifier or not

• Behat: The Container

Page 78: PHP Barcelona Monthly Talk Feb 2015

Tips & Tricks

• Repositories:

• Just add, save and find

• Use of DQL implies the schema is too complex

• Controllers: 50 lines of code

• Application Services: Avoid business logic

• One step at time: Hex. Arch. -> DDD -> CQRS -> ES?

• Start it as a Module and promote it later to BC

Page 79: PHP Barcelona Monthly Talk Feb 2015

TROLLTIMEQUESTIONS?

Akamon Developers