Domain Driven Design
Primer
The
Communication
Chasm
Reduce Verbal Friction
DDD and it’s components
Ubiquitous Language
Sounds Easy
It’s not
Technical Terms
class Customer {
public string Cust_Name {…}
}
class CustomerFactory {
…
}
Not Just Class Names
Industry vs Business
Nomenclatures
class GobbleGobble {
…
}
// or
class Turkey {
…
}
Fluency
invoice.ChangeShipDateTo
(newShipDate)
Code Impact
Aggregate Roots
Purchaser
Invoice
Ship To
Address Items
Product Discount
Code
Tax
Code
Billing
Address
Many per Application
Overlap
Invoice
vs
Statement.Invoice
Clarity > Reuse
Context is King
Encapsulation
class Invoice {
public List<InvoiceItem> Items
{ get; set; }
}
invoice.Items.Add
(new InvoiceItem);
class Invoice {
private List<InvoiceItem> _items;
public IEnumerable<InvoiceItem> Items
{ get {return _items;}}
public void AddItem(InvoiceItem item) {
_items.Add(item);
//change invoice total
//change invoice tax totals
//change invoice item count
}
}
class Invoice {
public void ChangeShippingAddressTo(
ShippingAddress newAddress) {
//change address
//add change fee
//etc.
}
}
invoice.ShippingAddress.XXX =
“blah”;
var xyz =
invoice.ShippingAddress.XXX;
Data Access
UI
Service Layer
Domain Model
Data Access
Only via Aggregate Roots
Load aggregate root
Modify values
Persist aggregate root
But now we’re loading things
we might not need/use…
Use an ORM
Lazy Loading
DDD anti-patterns
Anemic Domain Model
Default constructors
1 Repository class
:
1 Domain class
Domain model in the UI
Auto-mapping
from
UI -> Domain model
Public property setters
What will happen
when you try DDD?
You’ll have to talk to the
business more
The business will talk to you
just like your code reads
You will struggle
to give up the
“One model to rule them all”
Large, complex systems
will seem simpler
Find it hard to keep the
model out of the UI
Have difficulties when
reading data for display
Erroneously try to use DDD
everywhere
donald.belcham@igloocoder.com
@dbelcham
www.igloocoder.com

Domain Driven Design Primer

Editor's Notes

  • #10 Database terminology or naming creeps into the code
  • #11 Design pattern terminology creeps into the code which is not how the business talks
  • #12 Properties, methods, events,enums, etc should all use the terms used by the business users
  • #13 Industry may seem more “proper” or “correct” but if the business uses the term so should you. One exception is when you are building an app to be used by multiple businesses in the same industry.
  • #15 Making your code read like the business speaks
  • #16 Database terminology or naming creeps into the code
  • #18 Business usually talks in focused areas. i.e. Inventory or Invoicing or Accounting.
  • #19 Business usually talks in focused areas. i.e. Inventory or Invoicing or Accounting.
  • #20 Except in the smallest of apps
  • #21 Aggregate roots will overlap with each other. This is fine. Remember to work within the context of that aggregate root though.
  • #22 Invoice might be detailed as outlined in our previous example. Statement.Invoice might just be a summary of that information.
  • #23 Model clarity is significantly more valuable than code reuse in almost all cases. Interface Segregation Principle is very important here.
  • #24 The context that you are working in is more important than the overriding concept
  • #28 When we take a business action it’s very common for many things to need to happen. Encapsulate those into methods on the Aggregate root
  • #29 Law of Demeter. You can pick your nose and you can pick your friends. You can’t pick your friend’s nose. You can, however, look at your friend’s nose.
  • #31 Service and Data Access layers know about Domain Model, but not the other way around
  • #32 The only items that should be passed to the data access layer are the aggregate root objects.
  • #33 The pattern of work that results from only doing data access with the aggregate root.
  • #34 Working with aggregate roots only can mean you are going to load data into objects in the tree that you may not use.
  • #35 Without an ORM the aggregate root DAL approach becomes very painful
  • #36 Leverage the functionality of the ORM. Load data only when you need it.
  • #38 A domain model that is only properties. We want our models to be rich, or Rubenesque; properties, methods and events will exist.
  • #39 If domain objects only use default constructors something is missing. Usually when you create a “new” instance of an object there will be minimum requirements for that object to be valid.
  • #40 1 repository to 1 aggregate root. All data access happens via aggregate roots.
  • #41 Domain model maps to the business functionality not to how it displays
  • #42 Auto-mapping from domain to UI is a great thing. When pulling data from the UI we want to apply it to the domain model via methods exposed on the aggregate root. Auto-mapping can’t do this. It can only do property to property
  • #43 Publically exposing property setters can indicate that the model is anemic.
  • #47 Changing your thought pattern to having overlapping aggregates and multiple classes representing the same data tables is difficult.
  • #49 Sometimes it seems like silly extra work to keep a UI model and a domain model separate. Sometimes this can indicate that all you’re building is forms-over-data with no business richness.
  • #50 DDD is great for the C, U and D parts of CRUD but it isn’t a great solution for the R portion. Separating how you read data for display on the screen makes the retrieval of the data much easier. Look into CQRS which separates these two concepts nicely.
  • #51 Knowing when there isn’t enough complexity to warrant DDD is just as important as knowing how to do DDD