SlideShare une entreprise Scribd logo
1  sur  47
Domain-Driven Design

     Dennis Traub
           @dtraub
     #devspace 2012-10-19
Domain
  A sphere of knowledge, influence, or activity.

  What an organization does and the world it does it in.
Model
  A system of abstractions that describes selected
  aspects of a Domain and can be used to solve
  problems related to that Domain.
don‘t model reality
we model useful abstractions of
reality
Ubiquitous Language
  A Language structured around the Domain Model
  and used by all team mebers.
Bounded Context
  An explicit Boundary within which a Domain
  Model exists.

  Inside the boundary all terms have specific meaning.
  They are part of the context‘s Ubiquitous Language.
BC Example: Online-Shop
  E-Commerce-System
  Inventory System
  Accounting
Subdomains
  The whole Domain of the organization is comprised
  of Subdomains.

  They focus on only one specific area of the whole
  business Domain.
Subdomain Example:
  Product Catalog
  Customer Management
  Orders
  Invoicing
  Inventory
  Shipping
don‘t build a model that
works for everyone
Core Domain
  The distinctive part of the Model, central to the user‘s
  goals, that differentiates the application and makes it
  valuable.

  The most important Subdomain.
Question # 1:

Which subdomain creates the greatest
competitive advantage?
Question # 2:

Which subdomain has the highest complexity?
we don‘t     use DDD when
there‘s no value in formalizing
the problem
we only use DDD in parts where
we get a competitive advantage
Aggregate
  A cluster of associated objects that are treated as a
  unit for the purpose of data changes.

  A set of consistency rules applies within the
  Aggregate‘s boundaries.
Exercise # 1: A simple Model
  We have Students, Courses, and Teachers
  A Student can take multiple Courses
  A Course can have multiple Students
  A Course is hosted by a Teacher
  A Teacher can host multiple Courses
public class Teacher
{
    public List<Course> Courses { get; private set; }
}


public class Course
{
    public Teacher Teacher { get; private set; }
    public List<Student> Students { get; private set; }
}

public class Student
{
    public List<Course> Courses { get; private set; }
}
Exercise # 2: Assign Teacher
  A Course ist hosted by a Teacher

  Constraint: A Teacher has a maximum number of
  students
public class Teacher
{
    public List<Course> Courses { get; private set; }
    public int MaximumNumberOfStudents { get; private set; }

    public void Assign(Course course) {
        if (course.Students.Count >= MaximumNumberOfStudents)
            throw ...
        this.Courses.Add(course);
        course.AssignTo(this);
    }
}

public class Course
{
    public Teacher Teacher { get; private set; }
    public void AssignTo(Teacher teacher) {
        this.Teachers.Add(teacher);
    }
}
public class Teacher
{
    public List<Course> Courses { get; private set; }
    public int MaximumNumberOfStudents { get; private set; }

    public void Assign(Course course) {
        if (course.Students.Count >= MaximumNumberOfStudents)
            throw ...
        this.Courses.Add(course);
        course.AssignTo(this);
    }
}

public class Course
{
    public Teacher Teacher { get; private set; }
    public void AssignTo(Teacher teacher) {
        this.Teachers.Add(teacher);
    }
}
Pattern # 1: Responsibility

  We don‘t know where to put things.

  Responsibilities must be clear.

  Have one way of doing something, not many.
public class Teacher
{
    public List<Course> Courses { get; private set; }
    public int MaximumNumberOfStudents { get; private set; }

    public void Assign(Course course) {
        if (course.Students.Count >= MaximumNumberOfStudents)
            throw ...
        this.Courses.Add(course);
        // course.AssignTo(this);
        course.Teacher = this;
    }
}

public class Course
{
    public Teacher Teacher { get; private set; }
    /* public void AssignTo(Teacher teacher) {
         this.Teachers.Add(teacher);
    } */
}
public class Teacher
{
    public List<Course> Courses { get; private set; }
    public int MaximumNumberOfStudents { get; private set; }

    public void Assign(Course course) {
        if (course.Students.Count >= MaximumNumberOfStudents)
            throw ...
        this.Courses.Add(course);
        // course.AssignTo(this);
        course.Teacher = this;
    }
}

public class Course
{
    public Teacher Teacher { get; private set; }
    /* public void AssignTo(Teacher teacher) {
         this.Teachers.Add(teacher);
    } */
}
Pattern # 2: Don‘t be stupid
  No rules on Teacher.Courses or Course.Teacher.

  We don‘t need the relationship.
don‘t model reality
public class Teacher
{
    public List<Course> Courses { get; private set; }
    public int MaximumNumberOfStudents { get; private set; }

    /* public void Assign(Course course) {
        if (course.Students.Count >= MaximumNumberOfStudents) throw ...
        this.Courses.Add(course);
        course.AssignTo(this);
    } */
}

public class Course
{
    // public Teacher Teacher { get; private set; }
    public int MaximumNumberOfStudents { get; private set; }

    public void AssignTo(Teacher teacher) {
        if (Students.Count >= teacher.MaximumNumberOfStudents)
            throw ...

        MaximumNumberOfStudents = teacher.MaximumNumberOfStudents;
    }
}
But what if ...
   What happens if the Teacher changes his/her
   maximum number of students?
New Use Case.
Ask the Domain Expert
Exercise # 3: Enroll Student
  A Student can enroll in a Course

  Constraint: A Student can only enroll in up to five
  Courses

  Constraint: A Student‘s Courses may not overlap
public class Course
{
    public int MaximumNumberOfStudents { get; private set; }
    public void Students { get; private set; }

    private void TryEnroll(Student student) {
        if (Students.Count() >= MaximumNumberOfStudents) throw ...
    }

    public void Enroll(Student student) {
        this.TryEnroll(student);
        student.TryEnroll(this);
        this.Students.Add(student);
        student.EnrollIn(this);
    }
}

public class Student
{
    public List<Course> Courses { get; private set; }

    public void TryEnrollIn(Course course) {
        foreach (var c in Courses) {
            if (c.OverlapsWith(course)) throw ...
        }
    }

    public void EnrollIn(Course course) {
        this.Courses.Add(course);
    }
}
public class Course
{
    public int MaximumNumberOfStudents { get; private set; }
    public void Students { get; private set; }

    private void TryEnroll(Student student) {
        if (Students.Count() >= MaximumNumberOfStudents) throw ...
    }

    public void Enroll(Student student) {
        this.TryEnroll(student);
        student.TryEnroll(this);
        this.Students.Add(student);
        student.EnrollIn(this);
    }
}

public class Student
{
    public List<Course> Courses { get; private set; }

    public void TryEnrollIn(Course course) {
        foreach (var c in Courses) {
            if (c.OverlapsWith(course)) throw ...
        }
    }

    public void EnrollIn(Course course) {
        this.Courses.Add(course);
    }
}
Pattern # 3: Capture Concepts
  We‘re missing an explicit concept:
  A Student has a Schedule
  The Schedule consists of Enrollments
  Constraint: Enrollments may not overlap
  Constraint: A Schedule may contain up to 5
  Enrollments
public class Course
{
    public int MaximumNumberOfStudents { get; private set; }
    public void Students { get; private set; }

    private void TryEnroll(Student student) {
        if (Students.Count() >= MaximumNumberOfStudents) throw ...
    }

    internal void Enroll(Student student) {
        // this.TryEnroll(student);
        // student.TryEnroll(this);
        this.Students.Add(student);
        // student.EnrollIn(this);
    }
}

public class Student
{
    public List<Course> Courses { get; private set; }
    public Schedule Schedule { get; private set; }

    public void EnrollIn(Course course) {
        var enrollment = course.CreateEnrollment(course);
        Schedule.TryAddEnrollment(enrollment);
        course.TryEnroll(this);
        Schedule.AddEnrollment(enrollment);
        course.Enroll(this);
    }
}
public class Course
{
    public int MaximumNumberOfStudents { get; private set; }
    // public void Students { get; private set; }
    public int NumberOfStudents { get; private set; }

    private void TryEnroll(Student student) {
        // if (Students.Count() >= MaximumNumberOfStudents) throw ...
        if (NumberOfStudents >= MaximumNumberOfStudents) throw ...
    }

    internal void Enroll(Student student) {
        this.NumberOfStudents++;
    }
}

public class Student
{
    public List<Course> Courses { get; private set; }
    public Schedule Schedule { get; private set; }

    public void EnrollIn(Course course) {
        var enrollment = course.CreateEnrollment(course);
        Schedule.TryAddEnrollment(enrollment);
        course.TryEnroll(this);
        Schedule.AddEnrollment(enrollment);
        course.Enroll(this);
    }
}
Aggregate
  A cluster of associated objects that are treated as a
  unit for the purpose of data changes.

  A set of consistency rules applies within the
  Aggregate‘s boundaries.
In other words:
  A Transaction should only affect one Aggregate.
What if ...
   ... they don‘t need to be done in one Transaction?

   ... we TryEnroll() but not Enroll()?
How big is the business impact?
Domain Events
  A Domain Event is a full-fledged part of the Domain
  Model, a representation of something that happened
  in the Domain.

  Something happened that Domain Experts care
  about, e.g. a Student enrolled in a Course.
public class Course
{
    public int MaximumNumberOfStudents { get; private set; }
    public int NumberOfStudents { get; private set; }

    private void TryEnroll(Student student) {
        if (NumberOfStudents >= MaximumNumberOfStudents) throw ...
    }

    /* public void Enroll(Student student) {
         this.NumberOfStudents++;
    } */

    public void Handle(StudentEnrolledInCourse event) {
        this.NumberOfStudents++;
    }
}

public class Student
{
    public List<Course> Courses { get; private set; }
    public Schedule Schedule { get; private set; }

    public void EnrollIn(Course course) {
        var enrollment = course.CreateEnrollment(course);
        Schedule.TryAddEnrollment(enrollment);
        course.TryEnroll(this);
        Schedule.AddEnrollment(enrollment);
        // course.Enroll(this);
        Bus.Publish(new StudentEnrolledInCourse( ... ));
    }
}
But, once again, what if ...
   ... the Student drops out of a Course?
New Use Case.
Ask the Domain Expert
1. don‘t be stupid

2. denormalize

3. break up consistencies
References & Further Reading:
  Inspiration for the exercises: The works of Greg Young

  Domain-Driven Design – Tackling Complexity in the Heart of Software,
  by Eric Evans, Addison-Wesley, 2004
  Implementing Domain-Driven Design by Vaughn Vernon, Addison-Wesley, 2012
  http://domaindrivendesign.org - DDD-Forum maintained by Eric Evans, et al.
  Effective Aggregate Design: bit.ly/ncCOTH
  Google Group: groups.google.com/forum/?fromgroups#!forum/dddcqrs
Dennis Traub

  @dtraub

 #devspace

Contenu connexe

Tendances

ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
habib_786
 
PostgreSQL_ Up and Running_ A Practical Guide to the Advanced Open Source Dat...
PostgreSQL_ Up and Running_ A Practical Guide to the Advanced Open Source Dat...PostgreSQL_ Up and Running_ A Practical Guide to the Advanced Open Source Dat...
PostgreSQL_ Up and Running_ A Practical Guide to the Advanced Open Source Dat...
MinhLeNguyenAnh2
 
Introduction to Sharding
Introduction to ShardingIntroduction to Sharding
Introduction to Sharding
MongoDB
 

Tendances (20)

MongoDB
MongoDBMongoDB
MongoDB
 
Introdução ao MongoDB em 30 slides
Introdução ao MongoDB em 30 slidesIntrodução ao MongoDB em 30 slides
Introdução ao MongoDB em 30 slides
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Solr vs. Elasticsearch - Case by Case
Solr vs. Elasticsearch - Case by CaseSolr vs. Elasticsearch - Case by Case
Solr vs. Elasticsearch - Case by Case
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
 
Spark + Cassandra = Real Time Analytics on Operational Data
Spark + Cassandra = Real Time Analytics on Operational DataSpark + Cassandra = Real Time Analytics on Operational Data
Spark + Cassandra = Real Time Analytics on Operational Data
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizer
 
Mongo DB
Mongo DBMongo DB
Mongo DB
 
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
PostgreSQL_ Up and Running_ A Practical Guide to the Advanced Open Source Dat...
PostgreSQL_ Up and Running_ A Practical Guide to the Advanced Open Source Dat...PostgreSQL_ Up and Running_ A Practical Guide to the Advanced Open Source Dat...
PostgreSQL_ Up and Running_ A Practical Guide to the Advanced Open Source Dat...
 
Content Management with MongoDB by Mark Helmstetter
 Content Management with MongoDB by Mark Helmstetter Content Management with MongoDB by Mark Helmstetter
Content Management with MongoDB by Mark Helmstetter
 
Mongo db intro.pptx
Mongo db intro.pptxMongo db intro.pptx
Mongo db intro.pptx
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Introduction to Sharding
Introduction to ShardingIntroduction to Sharding
Introduction to Sharding
 
MongoDB presentation
MongoDB presentationMongoDB presentation
MongoDB presentation
 
Criação de log de ações através do banco - PostgreSQL
Criação de log de ações através do banco - PostgreSQLCriação de log de ações através do banco - PostgreSQL
Criação de log de ações através do banco - PostgreSQL
 
PostgreSQL Tutorial For Beginners | Edureka
PostgreSQL Tutorial For Beginners | EdurekaPostgreSQL Tutorial For Beginners | Edureka
PostgreSQL Tutorial For Beginners | Edureka
 

Similaire à DDD Modeling Workshop

Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdf
aptind
 
import school.; import school.courses.;public class Main { p.pdf
import school.; import school.courses.;public class Main { p.pdfimport school.; import school.courses.;public class Main { p.pdf
import school.; import school.courses.;public class Main { p.pdf
annaiwatertreatment
 
C# Delegates, Events, Lambda
C# Delegates, Events, LambdaC# Delegates, Events, Lambda
C# Delegates, Events, Lambda
Jussi Pohjolainen
 
Cs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionCs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solution
Fahadaio
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
irshadkumar3
 

Similaire à DDD Modeling Workshop (20)

Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdf
 
import school.; import school.courses.;public class Main { p.pdf
import school.; import school.courses.;public class Main { p.pdfimport school.; import school.courses.;public class Main { p.pdf
import school.; import school.courses.;public class Main { p.pdf
 
C# Delegates, Events, Lambda
C# Delegates, Events, LambdaC# Delegates, Events, Lambda
C# Delegates, Events, Lambda
 
Mapeamento uml java
Mapeamento uml javaMapeamento uml java
Mapeamento uml java
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
 
Jar chapter 5_part_i
Jar chapter 5_part_iJar chapter 5_part_i
Jar chapter 5_part_i
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
6 class design
6 class design6 class design
6 class design
 
Refactoring - Mejorando el diseño del código existente
Refactoring - Mejorando el diseño del código existenteRefactoring - Mejorando el diseño del código existente
Refactoring - Mejorando el diseño del código existente
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
Cs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solutionCs141 mid termexam2_fall2017_v1.1_solution
Cs141 mid termexam2_fall2017_v1.1_solution
 
Core java oop
Core java oopCore java oop
Core java oop
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Lecture 4 part 1.pdf
Lecture 4 part 1.pdfLecture 4 part 1.pdf
Lecture 4 part 1.pdf
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4
 

Plus de Dennis Traub

Strategic Appplication Development with Domain-Driven Design (DDD)
Strategic Appplication Development with Domain-Driven Design (DDD)Strategic Appplication Development with Domain-Driven Design (DDD)
Strategic Appplication Development with Domain-Driven Design (DDD)
Dennis Traub
 
CQRS-Einführung - Teil 2
CQRS-Einführung - Teil 2CQRS-Einführung - Teil 2
CQRS-Einführung - Teil 2
Dennis Traub
 
CQRS - Eine Einführung - NOUG 2011
CQRS - Eine Einführung - NOUG 2011CQRS - Eine Einführung - NOUG 2011
CQRS - Eine Einführung - NOUG 2011
Dennis Traub
 

Plus de Dennis Traub (14)

Application Integration Patterns (not only) for Microservices
Application Integration Patterns (not only) for MicroservicesApplication Integration Patterns (not only) for Microservices
Application Integration Patterns (not only) for Microservices
 
Serverless SecOps Automation on AWS at AWS UG Krakow, Poland
Serverless SecOps Automation on AWS at AWS UG Krakow, PolandServerless SecOps Automation on AWS at AWS UG Krakow, Poland
Serverless SecOps Automation on AWS at AWS UG Krakow, Poland
 
Serverless Security Automation on AWS - Hamburg AWS User Group
Serverless Security Automation on AWS - Hamburg AWS User GroupServerless Security Automation on AWS - Hamburg AWS User Group
Serverless Security Automation on AWS - Hamburg AWS User Group
 
Cloud ist keine Strategie - Keynote des AWS Cloud Day, Solingen
Cloud ist keine Strategie - Keynote des AWS Cloud Day, SolingenCloud ist keine Strategie - Keynote des AWS Cloud Day, Solingen
Cloud ist keine Strategie - Keynote des AWS Cloud Day, Solingen
 
Cloud ist keine Strategie - AWS Tech Community Summit Cologne, 2017
Cloud ist keine Strategie - AWS Tech Community Summit Cologne, 2017Cloud ist keine Strategie - AWS Tech Community Summit Cologne, 2017
Cloud ist keine Strategie - AWS Tech Community Summit Cologne, 2017
 
Taming the Monolith - Microservices Meetup Hamburg
Taming the Monolith - Microservices Meetup HamburgTaming the Monolith - Microservices Meetup Hamburg
Taming the Monolith - Microservices Meetup Hamburg
 
Taming the Monolith
Taming the MonolithTaming the Monolith
Taming the Monolith
 
DDD / Microservices @ Trivento Spring Camp, Utrecht, 2015
DDD / Microservices @ Trivento Spring Camp, Utrecht, 2015DDD / Microservices @ Trivento Spring Camp, Utrecht, 2015
DDD / Microservices @ Trivento Spring Camp, Utrecht, 2015
 
Strategic Appplication Development with Domain-Driven Design (DDD)
Strategic Appplication Development with Domain-Driven Design (DDD)Strategic Appplication Development with Domain-Driven Design (DDD)
Strategic Appplication Development with Domain-Driven Design (DDD)
 
An Introduction to CQRS
An Introduction to CQRSAn Introduction to CQRS
An Introduction to CQRS
 
From DDD to CQRS
From DDD to CQRSFrom DDD to CQRS
From DDD to CQRS
 
Strategischer Anwendungsentwurf mit Domain-Driven Design
Strategischer Anwendungsentwurf mit Domain-Driven DesignStrategischer Anwendungsentwurf mit Domain-Driven Design
Strategischer Anwendungsentwurf mit Domain-Driven Design
 
CQRS-Einführung - Teil 2
CQRS-Einführung - Teil 2CQRS-Einführung - Teil 2
CQRS-Einführung - Teil 2
 
CQRS - Eine Einführung - NOUG 2011
CQRS - Eine Einführung - NOUG 2011CQRS - Eine Einführung - NOUG 2011
CQRS - Eine Einführung - NOUG 2011
 

Dernier

Dernier (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

DDD Modeling Workshop

  • 1. Domain-Driven Design Dennis Traub @dtraub #devspace 2012-10-19
  • 2. Domain A sphere of knowledge, influence, or activity. What an organization does and the world it does it in.
  • 3. Model A system of abstractions that describes selected aspects of a Domain and can be used to solve problems related to that Domain.
  • 5. we model useful abstractions of reality
  • 6. Ubiquitous Language A Language structured around the Domain Model and used by all team mebers.
  • 7. Bounded Context An explicit Boundary within which a Domain Model exists. Inside the boundary all terms have specific meaning. They are part of the context‘s Ubiquitous Language.
  • 8. BC Example: Online-Shop E-Commerce-System Inventory System Accounting
  • 9. Subdomains The whole Domain of the organization is comprised of Subdomains. They focus on only one specific area of the whole business Domain.
  • 10. Subdomain Example: Product Catalog Customer Management Orders Invoicing Inventory Shipping
  • 11. don‘t build a model that works for everyone
  • 12. Core Domain The distinctive part of the Model, central to the user‘s goals, that differentiates the application and makes it valuable. The most important Subdomain.
  • 13. Question # 1: Which subdomain creates the greatest competitive advantage?
  • 14. Question # 2: Which subdomain has the highest complexity?
  • 15. we don‘t use DDD when there‘s no value in formalizing the problem
  • 16. we only use DDD in parts where we get a competitive advantage
  • 17. Aggregate A cluster of associated objects that are treated as a unit for the purpose of data changes. A set of consistency rules applies within the Aggregate‘s boundaries.
  • 18. Exercise # 1: A simple Model We have Students, Courses, and Teachers A Student can take multiple Courses A Course can have multiple Students A Course is hosted by a Teacher A Teacher can host multiple Courses
  • 19. public class Teacher { public List<Course> Courses { get; private set; } } public class Course { public Teacher Teacher { get; private set; } public List<Student> Students { get; private set; } } public class Student { public List<Course> Courses { get; private set; } }
  • 20. Exercise # 2: Assign Teacher A Course ist hosted by a Teacher Constraint: A Teacher has a maximum number of students
  • 21. public class Teacher { public List<Course> Courses { get; private set; } public int MaximumNumberOfStudents { get; private set; } public void Assign(Course course) { if (course.Students.Count >= MaximumNumberOfStudents) throw ... this.Courses.Add(course); course.AssignTo(this); } } public class Course { public Teacher Teacher { get; private set; } public void AssignTo(Teacher teacher) { this.Teachers.Add(teacher); } }
  • 22. public class Teacher { public List<Course> Courses { get; private set; } public int MaximumNumberOfStudents { get; private set; } public void Assign(Course course) { if (course.Students.Count >= MaximumNumberOfStudents) throw ... this.Courses.Add(course); course.AssignTo(this); } } public class Course { public Teacher Teacher { get; private set; } public void AssignTo(Teacher teacher) { this.Teachers.Add(teacher); } }
  • 23. Pattern # 1: Responsibility We don‘t know where to put things. Responsibilities must be clear. Have one way of doing something, not many.
  • 24. public class Teacher { public List<Course> Courses { get; private set; } public int MaximumNumberOfStudents { get; private set; } public void Assign(Course course) { if (course.Students.Count >= MaximumNumberOfStudents) throw ... this.Courses.Add(course); // course.AssignTo(this); course.Teacher = this; } } public class Course { public Teacher Teacher { get; private set; } /* public void AssignTo(Teacher teacher) { this.Teachers.Add(teacher); } */ }
  • 25. public class Teacher { public List<Course> Courses { get; private set; } public int MaximumNumberOfStudents { get; private set; } public void Assign(Course course) { if (course.Students.Count >= MaximumNumberOfStudents) throw ... this.Courses.Add(course); // course.AssignTo(this); course.Teacher = this; } } public class Course { public Teacher Teacher { get; private set; } /* public void AssignTo(Teacher teacher) { this.Teachers.Add(teacher); } */ }
  • 26. Pattern # 2: Don‘t be stupid No rules on Teacher.Courses or Course.Teacher. We don‘t need the relationship.
  • 28. public class Teacher { public List<Course> Courses { get; private set; } public int MaximumNumberOfStudents { get; private set; } /* public void Assign(Course course) { if (course.Students.Count >= MaximumNumberOfStudents) throw ... this.Courses.Add(course); course.AssignTo(this); } */ } public class Course { // public Teacher Teacher { get; private set; } public int MaximumNumberOfStudents { get; private set; } public void AssignTo(Teacher teacher) { if (Students.Count >= teacher.MaximumNumberOfStudents) throw ... MaximumNumberOfStudents = teacher.MaximumNumberOfStudents; } }
  • 29. But what if ... What happens if the Teacher changes his/her maximum number of students?
  • 30. New Use Case. Ask the Domain Expert
  • 31. Exercise # 3: Enroll Student A Student can enroll in a Course Constraint: A Student can only enroll in up to five Courses Constraint: A Student‘s Courses may not overlap
  • 32. public class Course { public int MaximumNumberOfStudents { get; private set; } public void Students { get; private set; } private void TryEnroll(Student student) { if (Students.Count() >= MaximumNumberOfStudents) throw ... } public void Enroll(Student student) { this.TryEnroll(student); student.TryEnroll(this); this.Students.Add(student); student.EnrollIn(this); } } public class Student { public List<Course> Courses { get; private set; } public void TryEnrollIn(Course course) { foreach (var c in Courses) { if (c.OverlapsWith(course)) throw ... } } public void EnrollIn(Course course) { this.Courses.Add(course); } }
  • 33. public class Course { public int MaximumNumberOfStudents { get; private set; } public void Students { get; private set; } private void TryEnroll(Student student) { if (Students.Count() >= MaximumNumberOfStudents) throw ... } public void Enroll(Student student) { this.TryEnroll(student); student.TryEnroll(this); this.Students.Add(student); student.EnrollIn(this); } } public class Student { public List<Course> Courses { get; private set; } public void TryEnrollIn(Course course) { foreach (var c in Courses) { if (c.OverlapsWith(course)) throw ... } } public void EnrollIn(Course course) { this.Courses.Add(course); } }
  • 34. Pattern # 3: Capture Concepts We‘re missing an explicit concept: A Student has a Schedule The Schedule consists of Enrollments Constraint: Enrollments may not overlap Constraint: A Schedule may contain up to 5 Enrollments
  • 35. public class Course { public int MaximumNumberOfStudents { get; private set; } public void Students { get; private set; } private void TryEnroll(Student student) { if (Students.Count() >= MaximumNumberOfStudents) throw ... } internal void Enroll(Student student) { // this.TryEnroll(student); // student.TryEnroll(this); this.Students.Add(student); // student.EnrollIn(this); } } public class Student { public List<Course> Courses { get; private set; } public Schedule Schedule { get; private set; } public void EnrollIn(Course course) { var enrollment = course.CreateEnrollment(course); Schedule.TryAddEnrollment(enrollment); course.TryEnroll(this); Schedule.AddEnrollment(enrollment); course.Enroll(this); } }
  • 36. public class Course { public int MaximumNumberOfStudents { get; private set; } // public void Students { get; private set; } public int NumberOfStudents { get; private set; } private void TryEnroll(Student student) { // if (Students.Count() >= MaximumNumberOfStudents) throw ... if (NumberOfStudents >= MaximumNumberOfStudents) throw ... } internal void Enroll(Student student) { this.NumberOfStudents++; } } public class Student { public List<Course> Courses { get; private set; } public Schedule Schedule { get; private set; } public void EnrollIn(Course course) { var enrollment = course.CreateEnrollment(course); Schedule.TryAddEnrollment(enrollment); course.TryEnroll(this); Schedule.AddEnrollment(enrollment); course.Enroll(this); } }
  • 37. Aggregate A cluster of associated objects that are treated as a unit for the purpose of data changes. A set of consistency rules applies within the Aggregate‘s boundaries.
  • 38. In other words: A Transaction should only affect one Aggregate.
  • 39. What if ... ... they don‘t need to be done in one Transaction? ... we TryEnroll() but not Enroll()?
  • 40. How big is the business impact?
  • 41. Domain Events A Domain Event is a full-fledged part of the Domain Model, a representation of something that happened in the Domain. Something happened that Domain Experts care about, e.g. a Student enrolled in a Course.
  • 42. public class Course { public int MaximumNumberOfStudents { get; private set; } public int NumberOfStudents { get; private set; } private void TryEnroll(Student student) { if (NumberOfStudents >= MaximumNumberOfStudents) throw ... } /* public void Enroll(Student student) { this.NumberOfStudents++; } */ public void Handle(StudentEnrolledInCourse event) { this.NumberOfStudents++; } } public class Student { public List<Course> Courses { get; private set; } public Schedule Schedule { get; private set; } public void EnrollIn(Course course) { var enrollment = course.CreateEnrollment(course); Schedule.TryAddEnrollment(enrollment); course.TryEnroll(this); Schedule.AddEnrollment(enrollment); // course.Enroll(this); Bus.Publish(new StudentEnrolledInCourse( ... )); } }
  • 43. But, once again, what if ... ... the Student drops out of a Course?
  • 44. New Use Case. Ask the Domain Expert
  • 45. 1. don‘t be stupid 2. denormalize 3. break up consistencies
  • 46. References & Further Reading: Inspiration for the exercises: The works of Greg Young Domain-Driven Design – Tackling Complexity in the Heart of Software, by Eric Evans, Addison-Wesley, 2004 Implementing Domain-Driven Design by Vaughn Vernon, Addison-Wesley, 2012 http://domaindrivendesign.org - DDD-Forum maintained by Eric Evans, et al. Effective Aggregate Design: bit.ly/ncCOTH Google Group: groups.google.com/forum/?fromgroups#!forum/dddcqrs
  • 47. Dennis Traub @dtraub #devspace