Domain Driven Design
BY AMIT MUKHERJEE
TECHNICAL ARCHITECT - TECH MAHINDRA
AMIT.MUKHERJEE@TECHMAHINDRA.COM
Agenda
Why
Building blocks
◦ Repositories, entities, specifications etc.
Putting it to practice
◦ Dependency injection
◦ Persistence
◦ Validation
◦ Architecture
Challenges
When not to use DDD
Software is complicated
Traditional Architecture
Onion Architecture
-Domain model != view model
(The M in MVC)
-Instead of depending
downwards, dependencies goes
inwards
public bool CanBook(Cargo cargo, Voyage voyage)
{
if (!overbookingPolicy.IsAllowed(cargo, voyage))
return false;
...
}
public bool CanBook(Cargo cargo, Voyage voyage)
{
double maxBooking = voyage.Capacity * 1.1;
if (voyage.BookedCargoSize + cargo.Size > maxBooking)
return false;
...
}
Domain Model
The domain model requirements and
phases
Solution Structure
Essentials of DDD - Entities
-Like a person, you can change everything
about them: name, address, sex
-But over their whole lifetime they’re still the
same person
-Same with object, entities are the same
object for the duration of their lifetime
-Implemented with an identifier or ID
Essentials of DDD – Value Objects
Value Objects
-Value types on the other hand
-Are identified by their value
-E.g. color, addresses, shopping lists
- Define by logic and data
- Any two value objects with same data are
the same
- They are immutable
Essentials of DDD – Aggregates
Value Objects
-Cluster of objects
-Consistency boundary
-Simplifies things massively
-Master object called aggregate root
Essentials of DDD – Aggregate Root
Value Objects
-Cannot hold reference to anything except the root
-Can’t access any entity except from the root
-Consistency boundary
-Save the whole thing
-Cascades
-Versioning
-Locking – coarse grain lock
Essentials of DDD – Repository
Value Objects
-Common pattern
-When you need something
-Where you put things when your application isn’t
using them
- Abstracts away persistence
- One repository is specific to one type of
aggregate root
- It contains collection of instance of aggregate
- And provide operations of loading and saving
aggregate instance
Essentials of DDD – Bounded Context
Value Objects
-Different definitions in different contexts
-E.g. different departments may reuse
language differently
Data Model applies or lives inside Bounded
Context
In an enterprise with many domain, many
Bounded Context exist together
Bounded Context refers to different aspect of
the domain
Essentials of DDD – Dependency Injection
Value Objects
-All about interfaces
-Decouple our code
-Depend on the model, not the
implementation
-Users of INotificationService have no idea
-Instead of calling out
-Inversion of control
public class LeaveService
{
private readonly INotificationService notifications;
public LeaveService(INotificationService notifications)
{
this.notifications = notifications;
}
public void TakeLeave(Employee employee, DateTime start, DateTime end)
{
this.notifications.Notify(employee, "Leave approved.");
}
}
Essentials of DDD – Validation
Is the first name filled in?
Is the e-mail address format valid?
Is the first name less than 255 characters long?
Is the chosen username available?
Is the password strong enough?
Is the requested book available, or already out
on loan?
Is the customer eligible for this policy?
public class PersonRepository : IPersonRepository
{
public void Save(Person customer)
{
if (!customer.IsValid())
throw new Exception(...)
}
}
Validating only before it writes to the DB
- self-validating entities (doesn’t work for
different contexts)
When DDD isn’t appropriate
 -DDD is for dealing with complex domains
 -If you don’t have a complex domain
 -If you have no behavior or logic
 -If your ’model’ maps perfectly to a relational DB schema
 -Clues that you don’t need DDD
 -If you don’t have access to a domain expert

Domain driven design

  • 1.
    Domain Driven Design BYAMIT MUKHERJEE TECHNICAL ARCHITECT - TECH MAHINDRA AMIT.MUKHERJEE@TECHMAHINDRA.COM
  • 2.
    Agenda Why Building blocks ◦ Repositories,entities, specifications etc. Putting it to practice ◦ Dependency injection ◦ Persistence ◦ Validation ◦ Architecture Challenges When not to use DDD
  • 3.
  • 4.
  • 5.
    Onion Architecture -Domain model!= view model (The M in MVC) -Instead of depending downwards, dependencies goes inwards
  • 6.
    public bool CanBook(Cargocargo, Voyage voyage) { if (!overbookingPolicy.IsAllowed(cargo, voyage)) return false; ... } public bool CanBook(Cargo cargo, Voyage voyage) { double maxBooking = voyage.Capacity * 1.1; if (voyage.BookedCargoSize + cargo.Size > maxBooking) return false; ... }
  • 7.
  • 8.
    The domain modelrequirements and phases
  • 9.
  • 10.
    Essentials of DDD- Entities -Like a person, you can change everything about them: name, address, sex -But over their whole lifetime they’re still the same person -Same with object, entities are the same object for the duration of their lifetime -Implemented with an identifier or ID
  • 11.
    Essentials of DDD– Value Objects Value Objects -Value types on the other hand -Are identified by their value -E.g. color, addresses, shopping lists - Define by logic and data - Any two value objects with same data are the same - They are immutable
  • 12.
    Essentials of DDD– Aggregates Value Objects -Cluster of objects -Consistency boundary -Simplifies things massively -Master object called aggregate root
  • 13.
    Essentials of DDD– Aggregate Root Value Objects -Cannot hold reference to anything except the root -Can’t access any entity except from the root -Consistency boundary -Save the whole thing -Cascades -Versioning -Locking – coarse grain lock
  • 14.
    Essentials of DDD– Repository Value Objects -Common pattern -When you need something -Where you put things when your application isn’t using them - Abstracts away persistence - One repository is specific to one type of aggregate root - It contains collection of instance of aggregate - And provide operations of loading and saving aggregate instance
  • 15.
    Essentials of DDD– Bounded Context Value Objects -Different definitions in different contexts -E.g. different departments may reuse language differently Data Model applies or lives inside Bounded Context In an enterprise with many domain, many Bounded Context exist together Bounded Context refers to different aspect of the domain
  • 16.
    Essentials of DDD– Dependency Injection Value Objects -All about interfaces -Decouple our code -Depend on the model, not the implementation -Users of INotificationService have no idea -Instead of calling out -Inversion of control public class LeaveService { private readonly INotificationService notifications; public LeaveService(INotificationService notifications) { this.notifications = notifications; } public void TakeLeave(Employee employee, DateTime start, DateTime end) { this.notifications.Notify(employee, "Leave approved."); } }
  • 17.
    Essentials of DDD– Validation Is the first name filled in? Is the e-mail address format valid? Is the first name less than 255 characters long? Is the chosen username available? Is the password strong enough? Is the requested book available, or already out on loan? Is the customer eligible for this policy? public class PersonRepository : IPersonRepository { public void Save(Person customer) { if (!customer.IsValid()) throw new Exception(...) } } Validating only before it writes to the DB - self-validating entities (doesn’t work for different contexts)
  • 18.
    When DDD isn’tappropriate  -DDD is for dealing with complex domains  -If you don’t have a complex domain  -If you have no behavior or logic  -If your ’model’ maps perfectly to a relational DB schema  -Clues that you don’t need DDD  -If you don’t have access to a domain expert

Editor's Notes

  • #2 Welcome About domain dd Myself
  • #4 Challenge development habits constraints -ve std approaches, prevents creativity comfort zone just in case
  • #5 big code, code not in control, dev struggle to keep code afloat client replace old system -------------------------------------- SAN - DAS - RAM - STORAGE - NICS -------------------------------------- UI -WCF-> Server -ORM-> DB lots of model transformation going on ORM converts data into object mapper, properties convert data into UI datasets database driven system fat service layer non model centric
  • #11 An object that is not defined by its attributes, but rather by a thread of continuity and its identity.
  • #12 An object that contains attributes but has no conceptual identity. It is defined by logic and data. 2 vO with same data are the same them immutable
  • #13  If you follow a database-first approach, you aggregate root is usually the table on the 1 side of a 1-many relationship. The most common example being a Person. Each person has many addresses, one or more pay slips, invoices, CRM entries, etc.
  • #14 A collection of objects that are bound together by a root entity, otherwise known as an aggregate root. The aggregate root guarantees the consistency of changes being made within the aggregate by forbidding external objects from holding references to its members.
  • #15 One repository is specific to one type of aggregate root contains collection of instance of aggregate provide operations of loading and saving aggregate instance
  • #16 DM applies or lives inside BC in enterprise with many domain, many BC exist BC to refer different aspect of the domain Exterme domain model focus