introduction to domain driven design (laravelba #5)

53
Domain Driven Design

Upload: guiwoda

Post on 19-Nov-2014

181 views

Category:

Technology


0 download

DESCRIPTION

Introduction to Domain driven design, given by @GuiWoda at #LaravelBA on sept 17, 2014

TRANSCRIPT

Page 1: Introduction to Domain driven design (LaravelBA #5)

Domain Driven Design

Page 2: Introduction to Domain driven design (LaravelBA #5)

Ubiquitous Language

UbiquitousLanguage@Fowler's blog

Page 3: Introduction to Domain driven design (LaravelBA #5)

Ubiquitous LanguageThat’s all there is to it

Page 4: Introduction to Domain driven design (LaravelBA #5)

Ubiquitous LanguageThat’s all there is to it

Questions?

Page 5: Introduction to Domain driven design (LaravelBA #5)

Client (Domain expert)

Project Manager Developer

We need to add a new security policy to our

accounts. Our customers can’t have more than two

active offers per billing term.

We need to restrict promotions. A user cannot

have more than two promotions per month.

public function validate(User $user){ // … other crazy validations

if (count ($user->getPromotions()) > 2) throw new ValidationException( “User can’t have more than 2 promotions per month” );

// … continue with crazy validations}

Page 6: Introduction to Domain driven design (LaravelBA #5)

Favor model language over translations

Page 7: Introduction to Domain driven design (LaravelBA #5)

Client (Domain expert)

Project Manager Developer

We need to add a new security policy to our

accounts. Our customers can’t have more than two

active offers per billing term.

public function validate(Account $account){ // … other crazy validations

if (! $account->satisfies( new ActiveOffersPerBillingTermSecurityPolicy )) throw new ValidationException( “Account did not satisfy the active offers security policy” );

// … continue with crazy validations}

Page 8: Introduction to Domain driven design (LaravelBA #5)

ActiveOffersPerBillingTermSecurityPolicy

… really?TOO MUCH CODE!

I WON’T TYPE THAT!!!11

Page 9: Introduction to Domain driven design (LaravelBA #5)

Semantics

Page 10: Introduction to Domain driven design (LaravelBA #5)

SemanticsActiveOffersPerBillingTermSecurityPolicy means something

in the MODEL LANGUAGE

count($user->getPromotions()) > 2 does not convey this knowledge

Page 11: Introduction to Domain driven design (LaravelBA #5)

SemanticsActiveOffersPerBillingTermSecurityPolicy means something

in the MODEL LANGUAGE

count($user->getPromotions()) > 2 does not convey this knowledge

=> MAKE IMPLICIT BUSINESS RULES EXPLICIT

Page 12: Introduction to Domain driven design (LaravelBA #5)

Favor model language over translations

When a domain expert (client) feels something’s wrong with the model language, you’ll know something’s wrong with the domain model.

Page 13: Introduction to Domain driven design (LaravelBA #5)

Client (Domain expert)

Project Manager Developer

When we talk about a billing term, we need to consider if the customer is billed on a

monthly or yearly basis.

| class ActiveOffersPerBillingTermSecurityPolicy | { | protected $maxOffers = 2; | | public function isSatisfiedBy(Account $account) | { | return count(- | $account->getMonthlyOffers()+ | $account->getOffersForBillingTerm() | ) < $this->maxOffers; | }

Page 14: Introduction to Domain driven design (LaravelBA #5)

Domain Knowledge

Page 15: Introduction to Domain driven design (LaravelBA #5)

Domain KnowledgeFocus your efforts in building a clear domain,

specific to your client’s needs

Page 16: Introduction to Domain driven design (LaravelBA #5)

Knowledge crunching

Page 17: Introduction to Domain driven design (LaravelBA #5)

Client (Domain expert)

Project Manager Developer

class LongOverdueCustomerContactCommand{ public function execute() { $customers = Customer::where( ‘account.balance’, ‘<’, 0 )->get();

foreach ($customers as $customer) $this->sendRefinanceEmail($customer); }}

We need to contact long overdue customers to refinance their debt.

Page 18: Introduction to Domain driven design (LaravelBA #5)

Client (Domain expert)

Project Manager Developer

class LongOverdueCustomerContactCommand{ public function execute() { $customers = Customer::where( ‘account.balance’, ‘<’, 0 )->get();

foreach ($customers as $customer) $this->sendRefinanceEmail($customer); }}

We need to contact long overdue customers to refinance their debt.

Page 19: Introduction to Domain driven design (LaravelBA #5)

Client (Domain expert)

Project Manager Developer

We need to contact long overdue customers to refinance their debt.

What is the difference between a regular customer, an overdue customer and a

long overdue customer?

Page 20: Introduction to Domain driven design (LaravelBA #5)

Client (Domain expert)

Project Manager Developer

class CustomerRepository{ public function findOverdue() { return Customer::where(‘account.balance’, ‘<’, 0) ->get(); }

public function findLongOverdue() { // Write more complex query logic for this one... }}

Overdue is straightforward, customers in debt right now.

Long overdue customers are the ones that have been overdue over the last 3

billing terms, at the end of each billing term.

Page 21: Introduction to Domain driven design (LaravelBA #5)

Knowledge crunchingKnowledge crunching deepens the domain

model, and brings better understanding

Page 22: Introduction to Domain driven design (LaravelBA #5)

But… How?

Page 23: Introduction to Domain driven design (LaravelBA #5)

Domain-orientedDesign Patterns

Page 24: Introduction to Domain driven design (LaravelBA #5)

class AccountsController{ public function index() { $accounts = Account::all();

return View::make(‘accounts.index’, [ ‘accounts’ => $accounts ]); }}

Page 25: Introduction to Domain driven design (LaravelBA #5)

class AccountsController{ public function index() { $accounts = Account::all();

return View::make(‘accounts.index’, [ ‘accounts’ => $accounts ]); }}

Page 26: Introduction to Domain driven design (LaravelBA #5)

EvansClassification@Fowler's blog

Page 27: Introduction to Domain driven design (LaravelBA #5)

Entities

Page 28: Introduction to Domain driven design (LaravelBA #5)

EntitiesObjects with an identity

LifespanUnique criteria

Page 29: Introduction to Domain driven design (LaravelBA #5)

ValueObjects

Page 30: Introduction to Domain driven design (LaravelBA #5)

ValueObjectsDefined by their attributes

Page 31: Introduction to Domain driven design (LaravelBA #5)

Aggregates

Page 32: Introduction to Domain driven design (LaravelBA #5)

AggregatesCluster of domain objectsTransactional coherence

Page 33: Introduction to Domain driven design (LaravelBA #5)

Services

Page 34: Introduction to Domain driven design (LaravelBA #5)

ServicesDomain coordinators

Define actions

Page 35: Introduction to Domain driven design (LaravelBA #5)

Repositories

Page 36: Introduction to Domain driven design (LaravelBA #5)

RepositoriesData access abstraction

Page 37: Introduction to Domain driven design (LaravelBA #5)

Factories

Page 38: Introduction to Domain driven design (LaravelBA #5)

FactoriesObject creation

Page 39: Introduction to Domain driven design (LaravelBA #5)

Domain Events

Page 40: Introduction to Domain driven design (LaravelBA #5)

Domain EventsObjects that represent significant domain

events

Page 41: Introduction to Domain driven design (LaravelBA #5)

Domain-orientedArchitecture

Page 42: Introduction to Domain driven design (LaravelBA #5)

Layered Architecture

Page 43: Introduction to Domain driven design (LaravelBA #5)

Layered Architecture

?

Page 44: Introduction to Domain driven design (LaravelBA #5)

Hexagonal Architecture

Page 45: Introduction to Domain driven design (LaravelBA #5)

Hexagonal Architecture

?

Page 46: Introduction to Domain driven design (LaravelBA #5)

Domain-orientedArchitecture

Isolate your domain model from specific application needs:

UI - Persistence - External Services - Application workflow - etc...

Page 47: Introduction to Domain driven design (LaravelBA #5)

So… What’s the point?

Page 48: Introduction to Domain driven design (LaravelBA #5)

It’s not about writing code

Page 49: Introduction to Domain driven design (LaravelBA #5)

It’s not about writing codeThat’s the easy part… right?

Page 50: Introduction to Domain driven design (LaravelBA #5)

It’s not about writing codeIt’s about understanding what needs to be

solved before starting to code

Page 51: Introduction to Domain driven design (LaravelBA #5)

Creating a language which makes conversations

between domain experts and developers possible without

misinterpretations

Page 52: Introduction to Domain driven design (LaravelBA #5)

Creating a language which makes conversations

between domain experts and developers possible without

(so many) misinterpretations

Page 53: Introduction to Domain driven design (LaravelBA #5)

● Eric Evans, “Domain-Driven Design: Tackling Complexity in the Heart of Software”

● Vaughn Vernon, “Implementing Domain-Driven Design”

● Martin Fowler, “Patterns of Enterprise Application Architecture”

● dddcommunity.org | Domain Driven Design Community

● -http://domainlanguage.com/ddd/reference/DDD_Reference_2011-01-31.pdf

● -http://www.lmgtfy.com/?q=Domain+Driven+Design