ddd with asp.net mvc - steve smith | falafelcon 2014
DESCRIPTION
Domain-Driven Design provides a set of principles and patterns that are useful for tackling complex software problems. In this session, we'll review a few DDD concepts and demonstrate how they can be applied within an ASP.NET MVC Solution to keep concerns separated and implementation details loosely coupled from business logic and your domain model.TRANSCRIPT
![Page 2: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/2.jpg)
What is Domain-Driven Design (DDD)?
![Page 3: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/3.jpg)
![Page 4: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/4.jpg)
Communication
• Ubiquitous Language
• Domain Expert Interaction
Modeling
• Core Domain
• Generic Subdomains
• Bounded Context
• Context Map
• Shared Kernel
• Anti-Corruption Layer
Implementation
• Model-Driven Design
• Layered Architecture
• Entities
• Value Objects
• Services
• Factories
• Aggregates
• Repositories
• Domain Events
![Page 5: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/5.jpg)
DDD is BIG
“The more you know, the more you realize you know nothing.”
Socrates
![Page 6: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/6.jpg)
DDD Fundamentals Course
• Over 4 hours of content (demos using MVC + SignalR)
• http://bit.ly/PS-DDD
![Page 7: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/7.jpg)
DDD Benefits• Flexibility
• Software models customer’s understanding of problem
• Breaks complexity into manageable pieces
• Well-organized; easily tested
• Business logic lives in one place
![Page 8: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/8.jpg)
DDD Drawbacks• Time and Effort
• Learning Curve
• Overkill without Complexity• “Anemic” domain model problem
![Page 9: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/9.jpg)
Communication
“As software developers, we fail in two ways: we build the thing wrong, or we build the wrong thing.”
Me
![Page 10: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/10.jpg)
![Page 11: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/11.jpg)
Ubiquitous Language
http://upload.wikimedia.org/wikipedia/commons/2/23/Rosetta_Stone.JPG
![Page 12: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/12.jpg)
Language
“A project faces serious problems when its language is fractured.”
Eric Evans
![Page 13: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/13.jpg)
Ubiquitous Language• Ubiquitous – adjective. Present, appearing, or
found everywhere.• Synonyms: pervasive, universal
• Used within a given Bounded Context
• Used in code, design documents, and conversations-- Everywhere
![Page 14: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/14.jpg)
Domain Terms
Domain Experts
Problem Domain
Core Domain
Sub-Domains
![Page 15: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/15.jpg)
Bounded Contexts• Provide Separation of Concerns
• Limit complexity
• Should be clearly bounded and separate
![Page 16: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/16.jpg)
Appointment Scheduling Billing
![Page 17: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/17.jpg)
Appointment Scheduling Billing
Anti-Corruption
Layer
![Page 18: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/18.jpg)
Model Driven Design
Not Data-Driven
![Page 19: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/19.jpg)
Layered Architecture
• Ports and Adapters
• Hexagonal
• Onion
![Page 20: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/20.jpg)
Organizing in a Solution
![Page 21: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/21.jpg)
Entities
“Many objects are not fundamentally defined by their attributes, but rather by a thread of continuity and identity.”
Eric Evans
![Page 22: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/22.jpg)
Changing Attributes Doesn’t Change Which One We’re Talking About
ID:1
•Name: Steve Smith
•Twitter: @ardalis
•Favorite Color: Blue
ID: 1
•Name: Steven Smith
•Twitter: @ardalis
•Favorite Color: Blue
ID: 1
•Name: Steven Smith
•Twitter: @ardalis
•Favorite Color: Orange
![Page 23: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/23.jpg)
![Page 24: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/24.jpg)
![Page 25: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/25.jpg)
Value Objects
• Defined by their attributes
• Immutable
• Should have no side effects
• Examples: strings, addresses, currency
![Page 26: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/26.jpg)
![Page 27: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/27.jpg)
Immutable!
![Page 28: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/28.jpg)
Domain Services• Not a natural part of an Entity or Value Object
• Interface defined in terms of other model elements
• Should be stateless (but may have side effects)
![Page 29: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/29.jpg)
Services in Different Layers
UI Layer& Application Layer
InfrastructureDomain(“Application Core”)
Message Sending
Message Processing
XML Parsing
UI Services
Transfer Between Accounts
Process Order
Send Email
Log to a File
![Page 30: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/30.jpg)
Domain Events
“Use a Domain Event to capture an occurrence of something that happened in the domain.”
Vaughn VernonImplementing Domain-Driven Design
![Page 31: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/31.jpg)
Domain Event Tips• Consider for cases of “when this happens, then…”
• Or “Notify someone when…”
• Domain events represent the past• They already happened
• Thus, they should be immutable
![Page 32: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/32.jpg)
Examples of Domain Events
User
Authenticated
Appointment
Confirmed
Payment
Received
$¢£¥
![Page 33: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/33.jpg)
Designing Domain Events• Each Event is a Class
• Use a common interface (e.g. IDomainEvent)• Capture when the event took place
• Include details• What would you need to know to trigger this event again?• Include identities of any entities involved
• Initialize all state in constructor
• No behavior or side effects – just state
![Page 34: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/34.jpg)
More DDD Topics
• Aggregates
• Repositories
• Factories
DDD Fundamentals on PluralsightEric Evans’ DDD [email protected]
![Page 35: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/35.jpg)
Domain Models and MVC Models• UI interacts directly with Domain Model
• Entities, Value Objects• Interfaces, Services
• Views may work with custom ViewModels
• Client (HTML/JS) code may use another ViewModel as well
![Page 36: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/36.jpg)
Controllers
• Keep as small as possible
• Eliminate business logic
• Inject all dependencies
![Page 37: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/37.jpg)
Views
• No logic unless encapsulated in tested helpers
• No business logic if it can instead be modeled in the domain
![Page 38: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/38.jpg)
SignalR
• Awesome addition to ASP.NET
• Great for notifications to multiple users
• Ties in easily with Domain Events
![Page 39: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/39.jpg)
Solution Structure• Core
• Interfaces• Model (Entities, Value Objects)• Domain Services
• Infrastructure• DbContext• File Access• System Clock Access• Email services
• Web• MVC Project• No direct use of Infrastructure
![Page 40: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/40.jpg)
DemoPutting DDD into ASP.NET MVC
![Page 41: DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014](https://reader033.vdocument.in/reader033/viewer/2022052601/559453c21a28abe14f8b47eb/html5/thumbnails/41.jpg)