1 of 35

Adopting Domain-Driven Design in your organization

@aleixyz

2 of 35

Thanks to DDD Barcelona to organize and marfeel for hosting this event :-)

2

3 of 35

DDD to Simplify,

Not to Complicate

3

4 of 35

When you try to explain DDD to your colleagues for first time

4

5 of 35

DDD is about

Discussion, listening, understanding, discovery, and business value, all in effort to centralize knowledge.

  • Vaughn Vernon. IDDD

Two main things to have in mind always

  • Bounded Context
  • Ubiquitous Language

5

6 of 35

From Strategic to Tactical patterns depending on your role

6

7 of 35

Strategic Patterns

7

8 of 35

Domain, Subdomain and Bounded Context

8

Image from Implementing Domain Driven Design - Vaughn Vernon

How you divide in subdomains should reflect your organization structure.

It helps you take better decisions; where to invest, what to outsource.

To apply it in your organization, you will need decision maker support or be a decision maker.

If you’re an Architect, it helps you to determine the amount and the size of the (micro)services.

9 of 35

Bounded contexts helps to organize in feature teams

9

10 of 35

Bounded contexts helps to organize in feature teams

10

11 of 35

Identify your domain expert

The domain expert can be:

  • The product owner(s)
  • The developer with more experience about the business
  • Comercial team
  • CEO
  • and more since nobody has the domain expert title in LinkedIn

Often is multiple people

11

12 of 35

Ubiquitous Language - Before DDD

12

Similar to an illustration of IDDD book

13 of 35

Ubiquitous Language - After DDD (with some luck)

13

14 of 35

Tools that help create a ubiquitous language

From Business side:

  • User Stories is the first step
  • Specification by Example, BDD.
  • Event Storming. Shows the current knowledge of the domain by the team. Helps in discover your domain (but it doesn’t define it).
  • Domain Storytelling. Be awake on the next talk! :-)

From Technical side:

  • Pair programming when you’re in the same office
  • Code review when you work remotely

14

Long run

“Quick win”

15 of 35

Tools that help create a ubiquitous language

In summary

Business People and Technical people have to talk often using the Business Language

and use that language in your code

Don’t be afraid to ask, we are here to learn about the domain

15

16 of 35

Situations that will/might be familiar

16

17 of 35

Reflection

Does the Business talk about nulls?

How are you modeling the absence of?

How would you model explicitly the absence of?

17

18 of 35

Book recommendations

18

19 of 35

Video recommendation

19

Readable Code - Laura Savino - DDD Europe 2018

Domain Language throughout Tests, combining DDD and BDD - Kenny Baas - DDD Europe 2018

20 of 35

Tactical Patterns

20

21 of 35

Do I need ...

  • To do Event Storming
  • Organized in Feature teams
  • Domain Storytelling
  • BDD, Specification by Example
  • Hexagonal Architecture

in order to adopt Domain-Driven Design in my organization?

NO! You need to adapt the tools DDD brings to you in your organization context.

Don’t throw the towel just because (for example) Event Storming didn’t work.

21

22 of 35

My recommendation based on my experience

Focus on:

  • Value Objects
  • Entities
  • Domain Services
  • Aggregates

Easy to apply with most frameworks

Use only with proof that these are necessary/helpful:

  • CQRS
  • ES

22

23 of 35

Fast example

23

24 of 35

Identify anemic models

24

You might have Controller Architecture in your project (when CRUD has domain logic).

  1. Know if you have an anemic model
  2. Verify if your controllers contain logic
  3. Check if that logic is in different places
  4. Check if the logic is easy to read

If your model has setters, it likely (it is…) to be an anemic model.

First step. Eradicate Setters

25 of 35

25

  1. Decouple from the UI using DTOs (Data Transfer Objects).
  2. Move the business logic as near as possible to Entities, your domain models.
  3. Leave the UI logic in the Controller

Be aware that you introduced technical debt, it’s OK as long as you are aware of it.

Next step. Identify Value Objects

❓❓❕❕

26 of 35

26

Value Objects add stability to your code base.

  1. They are immutable. If they are created, they have valid state.
  2. Add meaning to your classes using types
  3. Very reusable tactical pattern

We choose to be coupled to the DB through those (evil?) annotations until we manage to decouple from DB instead to the UI.

Is User finished? Probably not. Value Objects are a good candidate to be an EntityId for example.

Next step: Application Service

27 of 35

Application Service

27

Their work is:

  1. Fetch an entity via Repository
  2. Call an entity’s method (business logic)
  3. Save the entity
  4. Maybe send an email and other side-effect code
  5. Return a DTO to the upper level

Helps re-usability

28 of 35

28

Example of entity using ubiquitous language.

Given a User,

When he/she wants to see a video in a public playlist,

Then he/she is allowed to

Given a User,

When he/she request permission to see a video in a private playlist,

And he/she isn’t in the member list

Then he/she the permission is rejected

Given a User,

When he/she request permission to see a video in a private playlist,

And he/she is in the member list

Then he/she the permission is granted

29 of 35

Recommendations

  1. Eradicate Setters.Replace them with explicit, meaningful names and move the logic there. Tell don’t ask.
  2. Change methods names and logic over data. Data is persisted.
    1. It applies to the class fields that are mapped into DB
  3. Move the logic and invariants to your domain model
    • Simple controllers
    • Application services as a proxy between repository and your domain models
  4. If you cannot be isolated from DB and UI in your domain model, choose coupling to DB over UI.
    • Use DTOs to return data from your domain service to your controller

This needs to be iterative, choose a User Story and apply this steps. It will help you to know where to stop refactoring. Small steps.

29

30 of 35

Remember that

  • Not all logic is business logic
  • Try to not be influenced by persistence layer in your domain models (remove this technical debt as soon as possible)
  • Security. Don’t move security to your domain layer (only). It has to be on each layer.

30

31 of 35

Summary

31

32 of 35

If I recommend you a book for your team it would be

32

  • Value Objects
  • Entities
  • Immutability & Invariants in the constructor
  • Project robustness through Design
  • and more

33 of 35

  1. Know your organization
  2. Experiment how DDD helps you
  3. Learn and share your experience
  4. Iterate your domain

33

34 of 35

Fast introduction to DDD to your team mates

34

35 of 35

Q&A

Thank you for your time! :-)

35