applying domain-driven design to craft rich domain models
DESCRIPTION
The domain model is an abstraction of the problem domain that your system supports. It contains the objects and operations that are crucial to your system and its users. Therefore the design of the domain model deserves the utmost care and attention. In this session you will be introduced to Domain-Driven Design and you will learn how to put Domain-Driven Design (DDD) into practice. We will explore how to apply DDD on tactical level to design a rich domain model that encapsulates behaviour, protects its invariants and can be tested in isolation from its runtime environmentTRANSCRIPT
![Page 1: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/1.jpg)
Powerpoint Templates
Applying Domain-Driven Design to craft
Rich Domain Models
Alexander van Trijffel6-2-2014
![Page 2: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/2.jpg)
Agenda
• What is a domain model?• Hexagonal Architecture• Ubiquitous Language• When to use Domain-Driven Design?• Adding behavior to the domain model• Value objects
![Page 3: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/3.jpg)
The domain modelRepresents business objects, operations and events that reflect the business processes and are meaningful to the business representatives.
Business rules
‘An Asset Value Correlation multiplier must be applied for exposures with to Financial Sector Entities with assets of at least $100 bn’
‘In case no Country of Risk is registered in UCI, the C-rating should be ‘No Rating’
Behavior
‘When an assessment is activated, a Global Facility Id is requested from UFI and assigned to the assessment’
‘When an employee states that he has outside interests, the system accepts stock positions under certain conditions’
![Page 4: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/4.jpg)
Invariants
‘A confirmed assessment has been calculated’
‘An activated assessment has a GFID’
‘A counterparty has an Wwid’
‘A protection covers at least one debtor’
![Page 5: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/5.jpg)
Hexagonal Architecture
![Page 6: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/6.jpg)
The blue book
![Page 7: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/7.jpg)
DDD on strategic level
![Page 8: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/8.jpg)
Second Page :
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Do we speak the same language?
![Page 9: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/9.jpg)
Ubiquitous language
![Page 10: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/10.jpg)
DDD on tactical level
![Page 11: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/11.jpg)
Aren’t we doing DDD already?
We have entities…
![Page 12: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/12.jpg)
And our managers/services/* know all about the logic…
* enter your favorite name for the application service role here
![Page 13: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/13.jpg)
We have a separate business layer
![Page 14: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/14.jpg)
Hello Anemic Domain Model!
![Page 15: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/15.jpg)
System type Domain logic Complexity
Use cases DDD pays off?
CRUD, Master data, Lookup
Low Any No
Simple, Small, Utility Low < 30 NoDepartmental, Narrow scoped
Medium between 30 and 40
Could be
Innovative Unknown ? YesEnterprise System,Core system
High > 40 Yes
When to invest in developing an ubiquitous language and implementing a Rich Domain Model?
![Page 16: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/16.jpg)
Object Oriented Software:
• Encapsulates data and operations in objects that interact with each other via the object’s interface.
• Applies information hiding; Internal representation of objects is hidden to clients
Object oriented means that an object manages its state and guarantees that it is in a legal state at any time.
![Page 17: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/17.jpg)
By applying Object Orientation to the domain model, the model:
• can ensure that its state is correct at all times
• can hide complexity behind its public interface
![Page 18: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/18.jpg)
Anemic Domain Model example
![Page 19: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/19.jpg)
Applying business rules
![Page 20: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/20.jpg)
What if we have business rules on the client Status as well?
Things get messy quickly…
![Page 21: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/21.jpg)
Empowering the domain: Encapsulating data and operations in domain objects
![Page 22: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/22.jpg)
Encapsulating domain logic in value objects
![Page 23: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/23.jpg)
Value object – immutable and comparable by its attributes
![Page 24: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/24.jpg)
Entity Value object
Unique Can have many occurences
Distinguished by identity Distinguished by attributes
Can be changed Immutable
Model object as Entity or Value Object?
![Page 25: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/25.jpg)
Money: Entity or value object?
![Page 26: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/26.jpg)
Value objects examples
PercentageProbability of DefaultLoss Given DefaultWorld Wide IdGlobal Facility IdRating
AddressCountryDecisionPeriodStatus
![Page 27: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/27.jpg)
Other tactical DDD patterns to explore:
• Domain Services
• Domain Events
• Aggregates
![Page 28: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/28.jpg)
Summary
Domain-Driven Design is a methodology that provides patterns for designing rich domains that encapsulate data and behavior.
The investment in developing an ubiquitous language and implementing a rich domain model pays off for medium to large applications that implement business rules and processes.
By applying Object Oriented principles to the domain, business logic is easier to test, understand and extend.
![Page 29: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/29.jpg)
Thanks for your attention!
![Page 30: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/30.jpg)
Resources
• Domain-Driven Design: Tackling Complexity in the Heart of Software (Eric Evans)
• Implementing Domain-Driven DesignVaughn Vernon
• DDD/CQRS mailing list https://groups.google.com/forum/#!forum/dddcqrs
![Page 31: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/31.jpg)
Backup slides
![Page 32: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/32.jpg)
Empowering the domain: Constructing objects
What does it mean to be a client in our domain?
• Does a client without a wwid make sense?• Does our domain even support clients without ratings?
Communicate the intent of a client through the API:
![Page 33: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/33.jpg)
Empowering the domain: Encapsulated collections
Can the domain object guarantee that the Ratings collection is valid at all times?
Does it make sense to clear all ratings? Duplicate a rating?
![Page 34: Applying Domain-Driven Design to craft Rich Domain Models](https://reader033.vdocuments.us/reader033/viewer/2022042512/54b736de4a7959404e8b45e1/html5/thumbnails/34.jpg)
Empowering the domain: Encapsulated collections
We provide an explicit API to manipulate the collection.