Domain Driven Development applied

78
(println "Hello World! :)")

Transcript of Domain Driven Development applied

Page 1: Domain Driven Development applied

(println "Hello World! :)")

Page 2: Domain Driven Development applied

DDD as an implementation detail

Eloi PochSergi González

Page 3: Domain Driven Development applied

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: Domain Driven Development applied

(conj '(1 2 3) 4)

Page 5: Domain Driven Development applied

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: Domain Driven Development applied
Page 7: Domain Driven Development applied

Akamon

Page 8: Domain Driven Development applied

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

Page 9: Domain Driven Development applied

We#develop#Social#Casino#Games#

Page 10: Domain Driven Development applied

Por$olio'of'Facebook'games'

Page 11: Domain Driven Development applied

Por$olio'of'Mobile'apps'

Page 12: Domain Driven Development applied

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

Page 13: Domain Driven Development applied

Technically …

Page 14: Domain Driven Development applied

Flash Client Game Server Backend

AS3 Nova

Java JReactive & Vertx

PHP Horus

Page 15: Domain Driven Development applied
Page 16: Domain Driven Development applied

@eloipoch @jordillonch @SergiGP

@jorgeavila_ss @jdiezc @jvalduvieco @amassa5

Who we are? The backend team

Page 17: Domain Driven Development applied

Development Problems

Page 18: Domain Driven Development applied

Our expectations…

Page 19: Domain Driven Development applied

PerformanceBig DataDifficult AlgorithmsConcurrencyReal time analyticsScalability

Page 20: Domain Driven Development applied

The ugly truth…

Page 21: Domain Driven Development applied

Technical Problems

Page 22: Domain Driven Development applied

Code ComplexityBig ball of mudShared StateCoupling

Page 23: Domain Driven Development applied
Page 24: Domain Driven Development applied

& Non-Technical problems

Page 25: Domain Driven Development applied

Different Cultures

Page 26: Domain Driven Development applied

Tackling Complex CodeThe Philosophy

Page 27: Domain Driven Development applied

Simple made Easy

Page 28: Domain Driven Development applied

Complex((

One(role(

One(task(

not(a(single(opera2on(

(

OBJECTIVE

Hard(

Familiar,(near(your(

knowledge(

(

(

Simple(vs(Easy((

SUBJECTIVEOBJECTIVE

Page 29: Domain Driven Development applied

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: Domain Driven Development applied

$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

(reduce + [1 2 3])

Simple vs EasySimple vs Easy

Page 31: Domain Driven Development applied

Simplicity vs Easiness

• Easy to understand

• Easy to debug

• Easy to change, flexibility, maintenable

• Easy to use

• Hard to debug

• Hard to change

Page 32: Domain Driven Development applied

Complected

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

• Source of accidental complexity

• DON’T DO IT

Page 33: Domain Driven Development applied

Essential ComplexityVS

Accidental Complexity

Page 34: Domain Driven Development applied

Do not complect, compose

Page 35: Domain Driven Development applied

Tackling Complex Code

The Theory

Page 36: Domain Driven Development applied

Organisational level

Page 37: Domain Driven Development applied

Pair Programming

Page 38: Domain Driven Development applied

Pull Requests

Page 39: Domain Driven Development applied

Code Reviews

Page 40: Domain Driven Development applied

Continuous Integration

Page 41: Domain Driven Development applied

Self Empowerment

Never ask for permission unless it

would be reckless not to

Page 42: Domain Driven Development applied

Trust

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

without fear. Patrick Lencioni

Page 43: Domain Driven Development applied

Technical Level

Page 44: Domain Driven Development applied

Lean Approach

Page 45: Domain Driven Development applied

Any model/solution is provisional

Page 46: Domain Driven Development applied

YAGNIYou Aren’t Gonna Need It

Page 47: Domain Driven Development applied

DDD Concepts

Page 48: Domain Driven Development applied

• DDD Tactical:

• Application Services

• Domain Services

• Domain Events

• Repositories

• AR & VO

• DDD Strategical:

• Modules

• Bounded Contexts

• SubDomains

• Context Maps

Page 49: Domain Driven Development applied

CQRS Concepts

Page 50: Domain Driven Development applied
Page 51: Domain Driven Development applied

Hexagonal Architecture

Page 52: Domain Driven Development applied
Page 53: Domain Driven Development applied

TDD & BDD

Page 54: Domain Driven Development applied
Page 55: Domain Driven Development applied

Tackling Complex Code

The Practice

Page 56: Domain Driven Development applied

Divide, Test & Conquer

Page 57: Domain Driven Development applied

Product Entity Example

Page 58: Domain Driven Development applied

ProductId

NameDescription

PriceCurrencyImage Url

CoinsStoreId

Page 59: Domain Driven Development applied

Product

Product Descriptions

Product Prices

Stores

Product Benefits

Product Images

Page 60: Domain Driven Development applied

ProductId

Product Description

Product Price

Stores

Product Benefit

Product Image

DescriptionName Currency

Amount

CoinsStoreId

Image Url

Page 61: Domain Driven Development applied

ProductId

Product Description

Product Price

Stores

Product Benefit

Product Image

DescriptionName Currency

Amount

CoinsStoreId

Image Url

Product BC

Page 62: Domain Driven Development applied

ProductId

Product Description

Product Price

Stores

Product Benefit

Product Image

DescriptionName Currency

Amount

CoinsStoreId

Image Url

Pricing/Sales BC

Product BC

Page 63: Domain Driven Development applied

Product

Id

BenefitId

Product Price

ProductId

Amount

Currency

Product Description

ProductId

Name

Description

Product Benefit

Id

Amount

VirtualMoneyId

Product Store

ProductId

StoreId

Page 64: Domain Driven Development applied
Page 65: Domain Driven Development applied

HORUS

Page 66: Domain Driven Development applied

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 67: Domain Driven Development applied

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 68: Domain Driven Development applied
Page 69: Domain Driven Development applied

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 70: Domain Driven Development applied

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 71: Domain Driven Development applied

Operational

Analytics

Support

Marketing

DE

C

Q

Q

We are still COUPLED !!

Page 72: Domain Driven Development applied

Operational

Analytics

Support

Marketing

HTTP

Page 73: Domain Driven Development applied

Tackling Complex Code

The Conclusions

Page 74: Domain Driven Development applied

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 75: Domain Driven Development applied

Mistakes

• Let the DB guide us

• Let the framework guide us

Page 76: Domain Driven Development applied

Problems

• Domain Events: Lost of order (async)

• Doctrine

• Unit Of Work + Integration Test = Fail

• Embedded: Identifier or not

• Behat: The Container

Page 77: Domain Driven Development applied

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 78: Domain Driven Development applied

TROLLTIMEQUESTIONS?

Akamon Developers