the way from db-driven development to ddd

19
The way from DB-driven development to DDD Руслан Гатиятов Дроид Лабс http://droidlabs.pro // https://planiro.com

Upload: provectus

Post on 26-Jan-2017

165 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: The way from DB-driven development to DDD

The way from DB-driven development to DDD Руслан ГатиятовДроид Лабсhttp://droidlabs.pro // https://planiro.com

Page 2: The way from DB-driven development to DDD

1. Clarity2. Maintainability3. Extensibility4. Safety5. Stability6. Predictability7. Scaling8. Testability

What should we address?

Page 3: The way from DB-driven development to DDD

General Rails problems1. DB driven thinking instead of business actions - I should persist this entity and this entity and also this one - create migration before test and actual code

2. REST confusion - POST /users creates user + organization + settings + default entities + etc

3. Code does not protect you from doing errors Entity1.create(...) should I also call Entity2.create(...) ?

Page 4: The way from DB-driven development to DDD

General Rails problems4. Testing framework does not use business rules - hard to read tests (what is going on there?) - hard to isolate test - hard to maintain test - mocks and stubs usually violate encapsulation - very long tests with many checks per test - really slow tests that use whole framework (even zeus, spring, VCR) - TDD is being applied rarely

Page 5: The way from DB-driven development to DDD

General Rails problems 5. Violation of SOLID principles: all layers are mixed up - DB calls in the views - views helper methods in models - business rules in controllers - hard to isolate layers

Page 6: The way from DB-driven development to DDD

General Rails problems 6. Less code means == obscure code

7. World of opinionated conventions

8. Project == RAILS - Are you able to change Rails with any other web framework during 1 working day? 9. Write model is different from Read Model (Commands and Queries)

Page 7: The way from DB-driven development to DDD

MVC - passive model - active model

The beginning

Page 8: The way from DB-driven development to DDD

1. C = controller- interaction with user- calls to model layer- serialize data and return result

Controller

Page 9: The way from DB-driven development to DDD

View2. V = view

- representation of results- JSON, XML, HTML, TXT, CSV, PDF, ...

what does V consist of? - view data (view model) - serializer (html, xml, json, ...)

Page 10: The way from DB-driven development to DDD

Model3. M = business model (set of modules that implement business rules)

IMPORTANT:M != ActiveRecord::Base descendant (it was only a certain case)

It’s not enough to have only services layer.

Page 11: The way from DB-driven development to DDD

DDDwhat does DDD imply? - ubiquitous language - layers of abstraction - bounded context - conventions about domain entities

Page 12: The way from DB-driven development to DDD

DDD - bounded contextBounded context is a central point in DDD

Page 13: The way from DB-driven development to DDD

what does M consist of?

Layers of abstraction:- Application Layer- Domain Layer- Infrastructure Layer

Model

Page 14: The way from DB-driven development to DDD

what is Application Layer?

defines interaction interface to get access to different system actions (commands) and stored data (queries)

Commands + Queries => CQRS principle

Examples: command 'accounts.register_user' query 'pm.reporting.user_productivity_report'

Application Layer

Page 15: The way from DB-driven development to DDD

Implements business logic using ubiquitous language including constraints, permissions checks, etc

1) Entity2) Value Object3) Aggregate4) Service5) Repository6) Factory7) Domain events

Domain Layer

Page 16: The way from DB-driven development to DDD

provides access to all external services- repositories- http clients- file utils- configs- websocket clients- amqp clients- etc

Infrastructure Layer

Page 17: The way from DB-driven development to DDD

Why DDD?How much time do you need to

- understand an issue- find a problem place- think about all possible side effects

for a system that has one end point for all UPDATE operations VS system having 1 endpoint per business action?

DDD is intended to solve business problems not thinking about implementation from the beginning. Ubiquitous language helps customers and developers to talk using the same language.

Page 18: The way from DB-driven development to DDD

Questions to expertsWhy does it change?When does it change?How often?Who causes it?By which rules?What consequences?

Page 19: The way from DB-driven development to DDD

What does DDD provides?

1. DSL && Ubiquitous language2. Naming conventions for different layers3. Web-framework is a detail of implementation4. Database is a detail of implementation5. High Cohesion and Low Coupling of your code