Domain-Driven Designfor the Database-Driven
Mind
Julie LermantheDataFarm.com
@julielerman
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Getting Started with EF6Domain-Driven Design FundamentalsLooking Ahead to Entity Framework 7EF6 Ninja Edition: What’s New in EF6Automated Testing for Fraidy Cats Like MeGetting Started with Entity Framework 5Entity Framework in the EnterpriseEntity Framework Code First MigrationsData Layer Validation with Entity Framework 4.1+Entity Framework 4.1 - DbContext Data AccessEntity Framework 4.1 - Code FirstQuerying the Entity FrameworkDesigner Supported EDM CustomizationEntity Framework and Data ModelsEntity Framework 4.0 By Example
My Courses on
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Your Workflow?
Software Solution
© DEVintersection. All rights reserved.http://www.DEVintersection.com
domaindatabase
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Forgetpersistence?
What aboutshared data
& types?
How will DDD patterns resolvewith my ORM?
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Value of Domain-Driven Design
Patterns for Implementing Persistence for DDD
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Focuson the
Business Problem
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Data Storage is not
theBusiness Problem
© DEVintersection. All rights reserved.http://www.DEVintersection.com
DomainProblem Data
Persistence
Cross-Cutting Concerns
© DEVintersection. All rights reserved.http://www.DEVintersection.com
DDD Thinking: Divide and Conquer
BoundedContext
BoundedContext
BoundedContext
BoundedContext
ProductionHuman
ResourcesShipping Sales
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Each Bounded Context
Learn & Evolve Design with Domain Experts
Software Implementation
Ubiquitous Language
“Customer”public class Customer{
}
“A Customer Registers”private Customer(string Name, Date regDate){}
public static Create(string Name, Date regDate){ return new Customer(name,regDate);
Register
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Domain-Driven Design Thinking
BoundedContext
BoundedContext
BoundedContext
BoundedContext
Ubiquitous LanguageDomain Model
TypesRules
Ubiquitous LanguageDomain Model
TypesRules
Ubiquitous LanguageDomain Model
TypesRules
Ubiquitous LanguageDomain Model
TypesRules
© DEVintersection. All rights reserved.http://www.DEVintersection.com
public class Schedule : Entity<Guid>{ public int ClinicId { get; private set; } public DateTimeRange DateRange { get; private set; } private List<Appointment> _appointments; public IEnumerable<Appointment> Appointments {...} public Schedule(Guid id, DateTimeRange dateRange, int clinicId, IEnumerable<Appointment> appointments) : base(id) { ... MarkConflictingAppointments(); DomainEvents.Register<AppointmentUpdatedEvent>(Handle); } public Appointment AddNewAppointment(Appointment appt) { if (_appointments.Any(a => a.Id == appt)) { throw new ArgumentException(“Duplicate", "appointment"); } appt.State = TrackingState.Added; _appointments.Add(appt); ...
Entities: More than data containers
public class Patient { public int Id { get; set; } public Client PetOwner { get; set; }
public int ClientId { get; set; } public string Name { get; set; } public Gender Gender { get; set; } public int? DoctorId { get; set; }}
Rich Domain Model for DDDAnemic Type for CRUD
private privat
e
© DEVintersection. All rights reserved.http://www.DEVintersection.com
PANIC!No Shared Data?
No Shared Types?
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Data Sharing Patterns & Anti-Patterns
BoundedContext
Ubiquitous Language
Domain ModelTypesRules
Bad (No-Op)Read/Write from BCs to common tables
Good*Read reference data from shared tablesWrite BC types to different tables
BetterEach BC persists to its own tables
BestDatabase or DB Schema per model
Even Better than BestEvent Sourcing (rather than persist state)
BoundedContext
Ubiquitous Language
Domain ModelTypesRules
BoundedContext
Ubiquitous Language
Domain ModelTypesRules
BoundedContext
Ubiquitous Language
Domain ModelTypesRules
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Anti-Corruption LayerNot Just for Connecting Apps
BoundedContext
Ubiquitous Language
Domain ModelTypesRules
BoundedContext
Ubiquitous Language
Domain ModelTypesRules
BoundedContext
Ubiquitous Language
Domain ModelTypesRules
BoundedContext
Ubiquitous Language
Domain ModelTypesRules
Anti-Corruption Layers
Services Message Queues Adapters Mappers
© DEVintersection. All rights reserved.http://www.DEVintersection.com
When to Share Types
Shared Kernel Tightly coordinated Entities and Value Objects Common schema and behavior “Reduce duplication, but not eliminate it” (Eric Evans, DDD book)
Inheritance Infrastructure Not domain types Favor composition over inheritance
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Data Layer
PersistenceModel
Bounded ContextDomainModel
Aggregate Root
Entities
ValueObjects
ORM DB
Anti-Corruption
Layer
e.g.Mapper
Domain Model and Persistence Model
Bounded Context
DomainModel
Aggregate Root
Entities
ValueObjects
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Anti-Corruption LayerBounded Context
DomainModel
Aggregate Root
Entities
ValueObjects
DB
Entity Framework API,
DbContext&
Mappings
Domain as Persistence Model
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Yes You Can
Design with DDD, Today&
Persist data with EF, Tomorrow
© DEVintersection. All rights reserved.http://www.DEVintersection.com
Code Sample for this session is at github.com/julielerman/DomainDrivenDesignforDatabaseDrivenMind
© DEVintersection. All rights reserved.http://www.DEVintersection.com
ResourcesMY STUFF: Domain-Driven Design Fundamentals on Pluralsight bit.ly/PS-DDD All of my Pluralsight courses: pluralsight.com/author/julie-lerman
A Pattern for Sharing Data Across DDD Bounded Contexts (Part 1&2) MSDN Magazine, Oct & Dec 2014: bit.ly/DataPoints_Dec2014
Entity Framework Model Partitioning in Domain-Driven Design Bounded Contexts TechEd Europe 2014: bit.ly/TEE2014_EFDDD
Coding for Domain-Driven Design: Tips for Data-Focused Devs (3 Parts) MSDN Magazine, Aug, Sept & Oct 2013 bit.ly/15xMlDL
BOOKS: Domain-Driven Design, Eric Evans amzn.to/1kstiRg Implementing Domain-Driven Design, Vaughn Vernon amzn.to/1dgYRY3
Domain Modeling with Entity Framework Scorecard, Jimmy Bogard, bit.ly/1x925bu
CQRS Journey from MS Patterns & Practices: bit.ly/cqrsjourney
Julie LermanEmail: [email protected]: theDataFarm.comTwitter: @julielerman