SlideShare a Scribd company logo
Practical DDD
@Bauno
Disclaimer
...and so it begins...
- Libro di Evans del 2004
- Descrive (prescrive?) un insieme
di patterns e metodologie di
analisi e implementazione
“Talk is cheap, show me the code”
“DDD...show me the code”
@emadb, 2011
DDD Purpose
● To build a domain (business) model, NOT a data model (anaemic domain)
● To express domain concepts in the code
● To make the implicit explicit (including, but not limited to, errors)
Strategic tools
● Ubiquitous language (Domain experts)
● Bounded context
● Core domain (distillation)
Bounded context
● It’s impossible to build and
maintain a model for a large,
complex organization: divide et
impera
● The preferred way of
communication between
bounded contexts is using Domain
Events via pub/sub
Ubiquitous language
● Rigourous language, no ambiguity
● It’s the language we use in the code, to name our domain concepts
● Usually valid only inside a bounded context
Core domain distillation
● DDD is expensive both in analysis and implementation, so it’s important
to know where to use it
● Every large domain has various supporting subdomains for every core
domain
● Usually found following the money :-)
Tactical tools
● Entities
● Aggregates
● Value Objects
● Domain services
● Domain events
● Repositories
● Factories (anti corruption layer)
Entities and Aggregates
● An entity is just a PO(C|J)O object that
expresses a domain concept and
enforces the domain invariant(s)
● Persistence ignorance
● Holds the business logic of the domain
and its state
● Has an identity throughout the
bounded context (Identity equality)
● An aggregate is a transaction
boundary consisting in several entities
held together by an aggregate root
Value Objects
● Plain simple records
● Immutable
● Structural equality (no identity)
Entities vs Value Objects
● Should this be and Entity or a Value Object? The answer can be subtle or
obvious, and depends mostly on the context
● Usually in a 70/30 ratio
Domain Services
● “The operation relates to a domain concept that is not a natural part of an
Entity or Value Object”
● First class citizens of the domain
● Stateless
Domain Events
● “Nel dominio è successo qualcosa di interessante per un domain expert”
● Can (asynchronously) cross the boundaries of an aggregate
● Compensating events
Repositories
● A repository is a class whose sole purpose is to collaborate with an entity for
data persistence
● SRP is enforced
● Emphasys on the data access pattern, which is made explicit
Factories/Validators
● Responsibles for creating the entities
● Anti corruption layer
● The domain has to be always valid, creating entities in invalid state must
be impossible
● Usually there is a strong coupling between factories and the entities they
create, but that’s ok.
Resulting
architecture
Warning: wrong place for domain
services
Testing
● The core domain has to be
unit tested, ideally with a
100% coverage
● I usually test factories and
repositories in integration
CQS (Command-Query Separation)
CQRS (Command Query Responsibility Separation)
DDD + CQ(R)S
● Only way I’ve found to write a reliable, rock solid DOMAIN model
● Domain Entities
○ Enforces invariants
○ Express business model, relationships
○ Have only public methods
○ They’re the write part of the model
● Views
○ Used for queries:
○ Very dumb, no business logic (only a little, if any, of presentation logic)
○ They’re read-only for the domain (i.e.: they come out, never go in)
○ Tailored to the specific user view (there are A LOT of views, but that’s ok)
How I do it (with a single RDBMS)
public interface IDbDto<DTO> {
TDto ToDto();
void Load(TDto dto);
}
- Dtos are the ONLY data saved to and loaded from the database
- The interface is hidden until we do a cast (which can only be done in the repository)
- The Dtos are used to hydrate (fill the data in) the views and the entities alike
Every entity explicitly implements the interface IDbDto
● Very dumb class
● No constructor
● Everything is public
● Usually denormalized
● Read-only for the domain
● Constructor sets the identity
● Class enforces domain invariants
● No public properties (even read-only)
● Public methods only
● State is completely private
public class Cart : IDbDto<CartDto>
{
public Guid Id {get; }
public Cart (Customer
customer);
public Unit AddItem(Item item);
public Unit CheckOut();
}
public class CartView
{
public ItemView[] Items {get; set}
public string CustomerName {get;
set;}
public string CustomerAddress {get;
set;}
public double Amount {get; set;}
}
public class CartDto
{
public Guid Id {get; set}
public Guid CustomerId {get;
set}
public ItemDto[] Items;
}
● Another dumb class
● Usually Normalized
● No constructor, public
● This is where an ORM
may be handy
Functional DDD
● Really? It’s intrinsically anaemic (Record + Services)
● CQRS (State as application of a series of events) fits better
Questions?
?

More Related Content

PDF
Introduction to Domain driven design
PPTX
PDF
Sharable of qualities of clean code
PDF
Functional programming
PPTX
The Practice of Programming - Notation
PDF
A quick introduction to go
PDF
Ruby - a pure object oriented language
PPTX
A Practical Guide to Domain Driven Design: Presentation Slides
Introduction to Domain driven design
Sharable of qualities of clean code
Functional programming
The Practice of Programming - Notation
A quick introduction to go
Ruby - a pure object oriented language
A Practical Guide to Domain Driven Design: Presentation Slides

Similar to Practical domain driven design (20)

PPT
Domain Driven Design (DDD)
PPTX
Seminar - Scalable Enterprise Application Development Using DDD and CQRS
PDF
Domain driven design: a gentle introduction
PPTX
Domain Driven Design
PDF
Domain-Driven Design
PDF
Domain Driven Design
PPTX
Implementing DDD with C#
PPTX
Domain driven design
PPTX
Introduction to DDD
PPTX
Domain Driven Design Quickly
PPTX
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
PPTX
Domain Driven Design(DDD) Presentation
PPTX
how_does_ddd_work
PDF
Domain driven design and model driven development
PPTX
Domain driven design
PDF
D2 domain driven-design
PDF
Beyond MVC: from Model to Domain
PDF
Clean architecture with ddd layering in php
PDF
Idiomatic Domain Driven Design: implementing CQRS
PPTX
Domain Driven Design
Domain Driven Design (DDD)
Seminar - Scalable Enterprise Application Development Using DDD and CQRS
Domain driven design: a gentle introduction
Domain Driven Design
Domain-Driven Design
Domain Driven Design
Implementing DDD with C#
Domain driven design
Introduction to DDD
Domain Driven Design Quickly
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
Domain Driven Design(DDD) Presentation
how_does_ddd_work
Domain driven design and model driven development
Domain driven design
D2 domain driven-design
Beyond MVC: from Model to Domain
Clean architecture with ddd layering in php
Idiomatic Domain Driven Design: implementing CQRS
Domain Driven Design
Ad

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PDF
System and Network Administraation Chapter 3
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Introduction to Artificial Intelligence
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
medical staffing services at VALiNTRY
System and Network Administraation Chapter 3
How Creative Agencies Leverage Project Management Software.pdf
Online Work Permit System for Fast Permit Processing
VVF-Customer-Presentation2025-Ver1.9.pptx
Softaken Excel to vCard Converter Software.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
top salesforce developer skills in 2025.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Odoo POS Development Services by CandidRoot Solutions
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Operating system designcfffgfgggggggvggggggggg
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Introduction to Artificial Intelligence
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
How to Choose the Right IT Partner for Your Business in Malaysia
Navsoft: AI-Powered Business Solutions & Custom Software Development
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Ad

Practical domain driven design

  • 3. ...and so it begins... - Libro di Evans del 2004 - Descrive (prescrive?) un insieme di patterns e metodologie di analisi e implementazione
  • 4. “Talk is cheap, show me the code” “DDD...show me the code” @emadb, 2011
  • 5. DDD Purpose ● To build a domain (business) model, NOT a data model (anaemic domain) ● To express domain concepts in the code ● To make the implicit explicit (including, but not limited to, errors)
  • 6. Strategic tools ● Ubiquitous language (Domain experts) ● Bounded context ● Core domain (distillation)
  • 7. Bounded context ● It’s impossible to build and maintain a model for a large, complex organization: divide et impera ● The preferred way of communication between bounded contexts is using Domain Events via pub/sub
  • 8. Ubiquitous language ● Rigourous language, no ambiguity ● It’s the language we use in the code, to name our domain concepts ● Usually valid only inside a bounded context
  • 9. Core domain distillation ● DDD is expensive both in analysis and implementation, so it’s important to know where to use it ● Every large domain has various supporting subdomains for every core domain ● Usually found following the money :-)
  • 10. Tactical tools ● Entities ● Aggregates ● Value Objects ● Domain services ● Domain events ● Repositories ● Factories (anti corruption layer)
  • 11. Entities and Aggregates ● An entity is just a PO(C|J)O object that expresses a domain concept and enforces the domain invariant(s) ● Persistence ignorance ● Holds the business logic of the domain and its state ● Has an identity throughout the bounded context (Identity equality) ● An aggregate is a transaction boundary consisting in several entities held together by an aggregate root
  • 12. Value Objects ● Plain simple records ● Immutable ● Structural equality (no identity)
  • 13. Entities vs Value Objects ● Should this be and Entity or a Value Object? The answer can be subtle or obvious, and depends mostly on the context ● Usually in a 70/30 ratio
  • 14. Domain Services ● “The operation relates to a domain concept that is not a natural part of an Entity or Value Object” ● First class citizens of the domain ● Stateless
  • 15. Domain Events ● “Nel dominio è successo qualcosa di interessante per un domain expert” ● Can (asynchronously) cross the boundaries of an aggregate ● Compensating events
  • 16. Repositories ● A repository is a class whose sole purpose is to collaborate with an entity for data persistence ● SRP is enforced ● Emphasys on the data access pattern, which is made explicit
  • 17. Factories/Validators ● Responsibles for creating the entities ● Anti corruption layer ● The domain has to be always valid, creating entities in invalid state must be impossible ● Usually there is a strong coupling between factories and the entities they create, but that’s ok.
  • 19. Testing ● The core domain has to be unit tested, ideally with a 100% coverage ● I usually test factories and repositories in integration
  • 21. CQRS (Command Query Responsibility Separation)
  • 22. DDD + CQ(R)S ● Only way I’ve found to write a reliable, rock solid DOMAIN model ● Domain Entities ○ Enforces invariants ○ Express business model, relationships ○ Have only public methods ○ They’re the write part of the model ● Views ○ Used for queries: ○ Very dumb, no business logic (only a little, if any, of presentation logic) ○ They’re read-only for the domain (i.e.: they come out, never go in) ○ Tailored to the specific user view (there are A LOT of views, but that’s ok)
  • 23. How I do it (with a single RDBMS) public interface IDbDto<DTO> { TDto ToDto(); void Load(TDto dto); } - Dtos are the ONLY data saved to and loaded from the database - The interface is hidden until we do a cast (which can only be done in the repository) - The Dtos are used to hydrate (fill the data in) the views and the entities alike Every entity explicitly implements the interface IDbDto
  • 24. ● Very dumb class ● No constructor ● Everything is public ● Usually denormalized ● Read-only for the domain ● Constructor sets the identity ● Class enforces domain invariants ● No public properties (even read-only) ● Public methods only ● State is completely private public class Cart : IDbDto<CartDto> { public Guid Id {get; } public Cart (Customer customer); public Unit AddItem(Item item); public Unit CheckOut(); } public class CartView { public ItemView[] Items {get; set} public string CustomerName {get; set;} public string CustomerAddress {get; set;} public double Amount {get; set;} } public class CartDto { public Guid Id {get; set} public Guid CustomerId {get; set} public ItemDto[] Items; } ● Another dumb class ● Usually Normalized ● No constructor, public ● This is where an ORM may be handy
  • 25. Functional DDD ● Really? It’s intrinsically anaemic (Record + Services) ● CQRS (State as application of a series of events) fits better