logo资料库

Domain Modeling Made Functional Tackle Software Complexity with ....pdf

第1页 / 共297页
第2页 / 共297页
第3页 / 共297页
第4页 / 共297页
第5页 / 共297页
第6页 / 共297页
第7页 / 共297页
第8页 / 共297页
资料共297页,剩余部分请下载后查看
Cover
Copyright
Table of Contents
Change History
Beta 5—November 20, 2017
Beta 4—September 6, 2017
Beta 3—July 25, 2017
Beta 2—June 28, 2017
Beta 1—June 7, 2017
Preface
Who Is This Book For?
What's In This Book?
Other Approaches To Domain Modeling
Working With the Code In This Book
Questions Or Suggestions?
Credits
Acknowledgements
Part I—Understanding the Domain
1. Introducing Domain-Driven Design
The Importance of a Shared Model
Understanding the Domain Through Business Events
Partitioning the Domain into Subdomains
Creating a Solution Using Bounded Contexts
Creating a Ubiquitous Language
Summarizing the Concepts of Domain-Driven Design
Wrapping Up
2. Understanding the Domain
Interview with a Domain Expert
Fighting the Impulse to Do Database-Driven Design
Fighting the Impulse to Do Class-Driven Design
Documenting the Domain
Diving Deeper into the Order-Taking Workflow
Representing Complexity in our Domain Model
Wrapping Up
3. A Functional Architecture
Bounded Contexts as Autonomous Software Components
Communicating Between Bounded Contexts
Contracts Between Bounded Contexts
Workflows Within a Bounded Context
Code Structure Within a Bounded Context
Wrapping Up
What's Next
Part II—Modeling the Domain
4. Understanding Types
Understanding Functions
Types and Functions
Composition of Types
Working with F# Types
Building a Domain Model by Composing Types
Modeling Optional Values, Errors, and Collections
Organizing Types in Files and Projects
Wrapping Up
5. Domain Modeling with Types
Reviewing the Domain Model
Seeing Patterns in a Domain Model
Modeling Simple Values
Modeling Complex Data
Modeling Workflows with Functions
A Question of Identity: Value Objects
A Question of Identity: Entities
Aggregates
Putting It All Together
Wrapping Up
6. Integrity and Consistency in the Domain
Integrity of Simple Values
Units of Measure
Enforcing Invariants with the Type System
Capturing Business Rules in the Type System
Consistency
Wrapping Up
7. Modeling Workflows as Pipelines
The Workflow Input
Modeling an Order as a Set of States
State Machines
Modeling Each Step in the Workflow With Types
Documenting Effects
Composing the Workflow From the Steps
Are Dependencies Part of the Design?
The Complete Pipeline
Long Running Workflows
Wrapping Up
What's Next
Part III—Implementing the Model
8. Understanding Functions
Functions, Functions, Everywhere
Functions Are Things
Total Functions
Composition
Wrapping Up
9. Implementation: Composing a Pipeline
Working With Simple Types
Using Function Types to Guide the Implementation
Implementing the Validation Step
Implementing the Rest of the Steps
Composing the Pipeline Steps Together
Injecting Dependencies
Testing Dependencies
The Assembled Pipeline
Wrapping Up
10. Implementation: Working with Errors
Using the Result Type to Make Errors Explicit
Working with Domain Errors
Chaining Result-Generating Functions
Applying "bind" and "map" to Our Pipeline
Adapting Other Kinds of Functions to the Two-Track Model
Making Life Easier with Computation Expressions
Monads and more
Adding the Async Effect
Wrapping Up
11. Serialization
Persistence vs. Serialization
Designing for Serialization
Connecting the Serialization Code to the Workflow
A Complete Serialization Example
How to Translate Domain types to DTOs
Wrapping Up
12. Persistence
Pushing Persistence to the Edges
Command-Query Separation
Bounded Contexts Must Own Their Data Storage
Working with Document Databases
Working with Relational Databases
Transactions
Wrapping Up
13. Evolving a Design and Keeping It Clean
Change 1: Adding Shipping Charges
Change 2: Adding Support for VIP Customers
Change 3: Adding Support for Promotion Codes
Change 4: Adding a Business Hours Constraint
Dealing with Additional Requirements Changes
Wrapping Up
Wrapping Up the Book
ß Under Construction: The book you’re reading is still under development. As part of our Beta book program, we’re releasing this copy well before a normal book would be released. That way you’re able to get this content a couple of months before it’s available in finished form, and we’ll get feedback to make the book even better. The idea is that everyone wins! Be warned: The book has not had a full technical edit, so it will contain errors. It has not been copyedited, so it will be full of typos, spelling mistakes, and the occasional creative piece of grammar. And there’s been no effort spent doing layout, so you’ll find bad page breaks, over-long code lines, incorrect hyphen- ation, and all the other ugly things that you wouldn’t expect to see in a finished book. It also doesn't have an index. We can’t be held liable if you use this book to try to create a spiffy application and you somehow end up with a strangely shaped farm implement instead. Despite all this, we think you’ll enjoy it! Download Updates: Throughout this process you’ll be able to get updated ebooks from your account at pragprog.com/my_account. When the book is com- plete, you’ll get the final version (and subsequent updates) from the same ad- dress. Send us your feedback: In the meantime, we’d appreciate you sending us your feedback on this book at pragprog.com/titles/swdddf/errata, or by using the links at the bottom of each page. Thank you for being part of the Pragmatic community! Andy
Domain Modeling Made Functional Tackle Software Complexity with Domain-Driven Design and F# Scott Wlaschin The Pragmatic Bookshelf Raleigh, North Carolina
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. For sales, volume licensing, and support, please contact support@pragprog.com. For international rights, please contact rights@pragprog.com. Copyright © 2017 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-68050-254-1 Encoded using the finest acid-free high-entropy binary digits. Book version: B5.0—November 20, 2017
Contents Change History . Preface . . . . . . . . . . . . . . . . . . . . . . . . . vii ix Part I — Understanding the Domain . . . . . . . . . . Introducing Domain-Driven Design The Importance of a Shared Model Understanding the Domain Through Business Events Partitioning the Domain into Subdomains Creating a Solution Using Bounded Contexts Creating a Ubiquitous Language Summarizing the Concepts of Domain-Driven Design Wrapping Up . . . . . Understanding the Domain . Interview with a Domain Expert Fighting the Impulse to Do Database-Driven Design Fighting the Impulse to Do Class-Driven Design Documenting the Domain Diving Deeper into the Order-Taking Workflow Representing Complexity in our Domain Model Wrapping Up . . . . A Functional Architecture Bounded Contexts as Autonomous Software Components Communicating Between Bounded Contexts Contracts Between Bounded Contexts Workflows Within a Bounded Context Code Structure Within a Bounded Context . . . . . 3 4 7 15 17 22 23 24 27 27 31 33 34 35 38 44 47 48 49 52 54 57 1. 2. 3.
Contents • iv 4. 5. 6. Wrapping Up What’s Next Part II — Modeling the Domain . . . . . . Understanding Types Understanding Functions Types and Functions Composition of Types Working with F# Types Building a Domain Model by Composing Types Modeling Optional Values, Errors, and Collections Organizing Types in Files and Projects Wrapping Up . . . . . . . . . . . . Domain Modeling with Types Reviewing the Domain Model Seeing Patterns in a Domain Model Modeling Simple Values Modeling Complex Data Modeling Workflows with Functions A Question of Identity: Value Objects A Question of Identity: Entities Aggregates Putting It All Together Wrapping Up . . . . . Integrity and Consistency in the Domain Integrity of Simple Values Units of Measure Enforcing Invariants with the Type System Capturing Business Rules in the Type System Consistency Wrapping Up 7. Modeling Workflows as Pipelines . . . . . . . The Workflow Input Modeling an Order as a Set of States State Machines Modeling Each Step in the Workflow With Types Documenting Effects 60 60 63 63 65 68 71 72 74 77 79 81 81 82 83 86 89 92 93 98 102 105 107 108 110 112 112 117 122 123 124 126 129 133 138
Contents • v 8. 9. 10. Composing the Workflow From the Steps Are Dependencies Part of the Design? The Complete Pipeline Long Running Workflows Wrapping Up What’s Next Part III — Implementing the Model . . . . . . . . . Understanding Functions Functions, Functions, Everywhere Functions Are Things Total Functions Composition Wrapping Up . . . . . Implementation: Composing a Pipeline . Working With Simple Types Using Function Types to Guide the Implementation Implementing the Validation Step Implementing the Rest of the Steps Composing the Pipeline Steps Together Injecting Dependencies Testing Dependencies The Assembled Pipeline Wrapping Up . . . . . Implementation: Working with Errors . Using the Result Type to Make Errors Explicit Working with Domain Errors Chaining Result-Generating Functions Applying “bind” and “map” to Our Pipeline Adapting Other Kinds of Functions to the Two-Track Model Making Life Easier with Computation Expressions Monads and more Adding the Async Effect Wrapping Up 11. Serialization . . . . . . . . . . . . Persistence vs. Serialization Designing for Serialization 141 141 143 145 147 147 151 151 152 158 160 164 167 168 169 171 178 183 186 190 192 195 197 197 198 202 209 211 215 223 224 226 227 227 228
Contents • vi 12. Persistence . . . . . . . Connecting the Serialization Code to the Workflow A Complete Serialization Example How to Translate Domain types to DTOs Wrapping Up . . . . . Pushing Persistence to the Edges Command-Query Separation Bounded Contexts Must Own Their Data Storage Working with Document Databases Working with Relational Databases Transactions Wrapping Up 13. Evolving a Design and Keeping It Clean . . . . . . Change 1: Adding Shipping Charges Change 2: Adding Support for VIP Customers Change 3: Adding Support for Promotion Codes Change 4: Adding a Business Hours Constraint Dealing with Additional Requirements Changes Wrapping Up Wrapping Up the Book 229 230 235 244 245 245 250 255 256 257 268 269 271 272 276 279 286 288 289 289
分享到:
收藏