domain-driven design for the database-driven mind julie lerman thedatafarm.com @julielerman

23
Design for the Database- Driven Mind Julie Lerman theDataFarm.com @julielerman

Upload: harriet-gibson

Post on 19-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

Domain-Driven Designfor the Database-Driven

Mind

Julie LermantheDataFarm.com

@julielerman

Page 2: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.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

Page 3: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

Your Workflow?

Software Solution

Page 4: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

domaindatabase

Page 5: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

Forgetpersistence?

What aboutshared data

& types?

How will DDD patterns resolvewith my ORM?

Page 6: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

Value of Domain-Driven Design

Patterns for Implementing Persistence for DDD

Page 7: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

Focuson the

Business Problem

Page 8: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

Data Storage is not

theBusiness Problem

Page 9: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

DomainProblem Data

Persistence

Cross-Cutting Concerns

Page 10: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

DDD Thinking: Divide and Conquer

BoundedContext

BoundedContext

BoundedContext

BoundedContext

ProductionHuman

ResourcesShipping Sales

Page 11: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 12: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 13: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 14: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

PANIC!No Shared Data?

No Shared Types?

Page 15: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 16: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 17: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 18: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 19: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 20: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

Yes You Can

Design with DDD, Today&

Persist data with EF, Tomorrow

Page 21: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© DEVintersection. All rights reserved.http://www.DEVintersection.com

Code Sample for this session is at github.com/julielerman/DomainDrivenDesignforDatabaseDrivenMind

Page 22: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

© 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

Page 23: Domain-Driven Design for the Database-Driven Mind Julie Lerman theDataFarm.com @julielerman

Julie LermanEmail: [email protected]: theDataFarm.comTwitter: @julielerman