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...
don‘t model reality
we model useful abstractions ofreality
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 me...
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...
Subdomain Example:  Product Catalog  Customer Management  Orders  Invoicing  Inventory  Shipping
don‘t build a model thatworks for everyone
Core Domain  The distinctive part of the Model, central to the user‘s  goals, that differentiates the application and make...
Question # 1:Which subdomain creates the greatestcompetitive advantage?
Question # 2:Which subdomain has the highest complexity?
we don‘t     use DDD whenthere‘s no value in formalizingthe problem
we only use DDD in parts wherewe 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 consiste...
Exercise # 1: A simple Model  We have Students, Courses, and Teachers  A Student can take multiple Courses  A Course can h...
public class Teacher{    public List<Course> Courses { get; private set; }}public class Course{    public Teacher Teacher ...
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; pr...
public class Teacher{    public List<Course> Courses { get; private set; }    public int MaximumNumberOfStudents { get; pr...
Pattern # 1: Responsibility  We don‘t know where to put things.  Responsibilities must be clear.  Have one way of doing so...
public class Teacher{    public List<Course> Courses { get; private set; }    public int MaximumNumberOfStudents { get; pr...
public class Teacher{    public List<Course> Courses { get; private set; }    public int MaximumNumberOfStudents { get; pr...
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; pr...
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  Cours...
public class Course{    public int MaximumNumberOfStudents { get; private set; }    public void Students { get; private se...
public class Course{    public int MaximumNumberOfStudents { get; private set; }    public void Students { get; private se...
Pattern # 3: Capture Concepts  We‘re missing an explicit concept:  A Student has a Schedule  The Schedule consists of Enro...
public class Course{    public int MaximumNumberOfStudents { get; private set; }    public void Students { get; private se...
public class Course{    public int MaximumNumberOfStudents { get; private set; }    // public void Students { get; private...
Aggregate  A cluster of associated objects that are treated as a  unit for the purpose of data changes.  A set of consiste...
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...
public class Course{    public int MaximumNumberOfStudents { get; private set; }    public int NumberOfStudents { get; pri...
But, once again, what if ...   ... the Student drops out of a Course?
New Use Case.Ask the Domain Expert
1. don‘t be stupid2. denormalize3. break up consistencies
References & Further Reading:  Inspiration for the exercises: The works of Greg Young  Domain-Driven Design – Tackling Com...
Dennis Traub  @dtraub #devspace
Upcoming SlideShare
Loading in...5
×

DDD Modeling Workshop

1,474

Published on

These are the slides from my DDD-based modeling workshop at the Developer Open Space in Leipzig, Germany: http://netopenspace.de/2012/

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,474
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "DDD Modeling Workshop"

  1. 1. Domain-Driven Design Dennis Traub @dtraub #devspace 2012-10-19
  2. 2. Domain A sphere of knowledge, influence, or activity. What an organization does and the world it does it in.
  3. 3. Model A system of abstractions that describes selected aspects of a Domain and can be used to solve problems related to that Domain.
  4. 4. don‘t model reality
  5. 5. we model useful abstractions ofreality
  6. 6. Ubiquitous Language A Language structured around the Domain Model and used by all team mebers.
  7. 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. 8. BC Example: Online-Shop E-Commerce-System Inventory System Accounting
  9. 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. 10. Subdomain Example: Product Catalog Customer Management Orders Invoicing Inventory Shipping
  11. 11. don‘t build a model thatworks for everyone
  12. 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. 13. Question # 1:Which subdomain creates the greatestcompetitive advantage?
  14. 14. Question # 2:Which subdomain has the highest complexity?
  15. 15. we don‘t use DDD whenthere‘s no value in formalizingthe problem
  16. 16. we only use DDD in parts wherewe get a competitive advantage
  17. 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. 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. 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. 20. Exercise # 2: Assign Teacher A Course ist hosted by a Teacher Constraint: A Teacher has a maximum number of students
  21. 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. 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. 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. 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. 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. 26. Pattern # 2: Don‘t be stupid No rules on Teacher.Courses or Course.Teacher. We don‘t need the relationship.
  27. 27. don‘t model reality
  28. 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. 29. But what if ... What happens if the Teacher changes his/her maximum number of students?
  30. 30. New Use Case.Ask the Domain Expert
  31. 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. 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. 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. 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. 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. 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. 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. 38. In other words: A Transaction should only affect one Aggregate.
  39. 39. What if ... ... they don‘t need to be done in one Transaction? ... we TryEnroll() but not Enroll()?
  40. 40. How big is the business impact?
  41. 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. 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. 43. But, once again, what if ... ... the Student drops out of a Course?
  44. 44. New Use Case.Ask the Domain Expert
  45. 45. 1. don‘t be stupid2. denormalize3. break up consistencies
  46. 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. 47. Dennis Traub @dtraub #devspace

×