Your SlideShare is downloading. ×
DDD Modeling Workshop
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

DDD Modeling Workshop

1,404
views

Published on

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

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,404
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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.
  • 4. don‘t model reality
  • 5. we model useful abstractions ofreality
  • 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 thatworks 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 greatestcompetitive advantage?
  • 14. Question # 2:Which subdomain has the highest complexity?
  • 15. we don‘t use DDD whenthere‘s no value in formalizingthe problem
  • 16. we only use DDD in parts wherewe 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.
  • 27. don‘t model reality
  • 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 stupid2. denormalize3. 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