O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Domain Modeling Made Functional

Book Description

You want increased customer satisfaction, faster development cycles, and less wasted work. Domain-driven design (DDD) combined with functional programming is the innovative combo that will get you there. In this pragmatic, down-to-earth guide, you'll see how applying the core principles of functional programming can result in software designs that model real-world requirements both elegantly and concisely - often more so than an object-oriented approach. Practical examples in the open-source F# functional language, and examples from familiar business domains, show you how to apply these techniques to build software that is business-focused, flexible, and high quality.

Domain-driven design is a well-established approach to designing software that ensures that domain experts and developers work together effectively to create high-quality software. This book is the first to combine DDD with techniques from statically typed functional programming. This book is perfect for newcomers to DDD or functional programming - all the techniques you need will be introduced and explained.

Model a complex domain accurately using the F# type system, creating compilable code that is also readable documentation---ensuring that the code and design never get out of sync. Encode business rules in the design so that you have "compile-time unit tests," and eliminate many potential bugs by making illegal states unrepresentable. Assemble a series of small, testable functions into a complete use case, and compose these individual scenarios into a large-scale design. Discover why the combination of functional programming and DDD leads naturally to service-oriented and hexagonal architectures. Finally, create a functional domain model that works with traditional databases, NoSQL, and event stores, and safely expose your domain via a website or API.

Solve real problems by focusing on real-world requirements for your software.

What You Need:

The code in this book is designed to be run interactively on Windows, Mac and Linux.You will need a recent version of F# (4.0 or greater), and the appropriate .NET runtime for your platform.Full installation instructions for all platforms at fsharp.org.

Table of Contents

  1.  Preface
    1. Who Is This Book For?
    2. What’s in This Book?
    3. Other Approaches to Domain Modeling
    4. Working with the Code in This Book
    5. Questions or Suggestions?
    6. Credits
    7. Acknowledgments
  2. Part I. Understanding the Domain
    1. 1. Introducing Domain-Driven Design
      1. The Importance of a Shared Model
      2. Understanding the Domain Through Business Events
      3. Partitioning the Domain into Subdomains
      4. Creating a Solution Using Bounded Contexts
      5. Creating a Ubiquitous Language
      6. Summarizing the Concepts of Domain-Driven Design
      7. Wrapping Up
    2. 2. Understanding the Domain
      1. Interview with a Domain Expert
      2. Fighting the Impulse to Do Database-Driven Design
      3. Fighting the Impulse to Do Class-Driven Design
      4. Documenting the Domain
      5. Diving Deeper into the Order-Taking Workflow
      6. Representing Complexity in Our Domain Model
      7. Wrapping Up
    3. 3. A Functional Architecture
      1. Bounded Contexts as Autonomous Software Components
      2. Communicating Between Bounded Contexts
      3. Contracts Between Bounded Contexts
      4. Workflows Within a Bounded Context
      5. Code Structure Within a Bounded Context
      6. Wrapping Up
      7. What’s Next
  3. Part II. Modeling the Domain
    1. 4. Understanding Types
      1. Understanding Functions
      2. Types and Functions
      3. Composition of Types
      4. Working with F# Types
      5. Building a Domain Model by Composing Types
      6. Modeling Optional Values, Errors, and Collections
      7. Organizing Types in Files and Projects
      8. Wrapping Up
    2. 5. Domain Modeling with Types
      1. Reviewing the Domain Model
      2. Seeing Patterns in a Domain Model
      3. Modeling Simple Values
      4. Modeling Complex Data
      5. Modeling Workflows with Functions
      6. A Question of Identity: Value Objects
      7. A Question of Identity: Entities
      8. Aggregates
      9. Putting It All Together
      10. Wrapping Up
    3. 6. Integrity and Consistency in the Domain
      1. The Integrity of Simple Values
      2. Units of Measure
      3. Enforcing Invariants with the Type System
      4. Capturing Business Rules in the Type System
      5. Consistency
      6. Wrapping Up
    4. 7. Modeling Workflows as Pipelines
      1. The Workflow Input
      2. Modeling an Order as a Set of States
      3. State Machines
      4. Modeling Each Step in the Workflow with Types
      5. Documenting Effects
      6. Composing the Workflow from the Steps
      7. Are Dependencies Part of the Design?
      8. The Complete Pipeline
      9. Long-Running Workflows
      10. Wrapping Up
      11. What’s Next
  4. Part III. Implementing the Model
    1. 8. Understanding Functions
      1. Functions, Functions, Everywhere
      2. Functions Are Things
      3. Total Functions
      4. Composition
      5. Wrapping Up
    2. 9. Implementation: Composing a Pipeline
      1. Working with Simple Types
      2. Using Function Types to Guide the Implementation
      3. Implementing the Validation Step
      4. Implementing the Rest of the Steps
      5. Composing the Pipeline Steps Together
      6. Injecting Dependencies
      7. Testing Dependencies
      8. The Assembled Pipeline
      9. Wrapping Up
    3. 10. Implementation: Working with Errors
      1. Using the Result Type to Make Errors Explicit
      2. Working with Domain Errors
      3. Chaining Result-Generating Functions
      4. Using bind and map in Our Pipeline
      5. Adapting Other Kinds of Functions to the Two-Track Model
      6. Making Life Easier with Computation Expressions
      7. Monads and More
      8. Adding the Async Effect
      9. Wrapping Up
    4. 11. Serialization
      1. Persistence vs. Serialization
      2. Designing for Serialization
      3. Connecting the Serialization Code to the Workflow
      4. A Complete Serialization Example
      5. How to Translate Domain Types to DTOs
      6. Wrapping Up
    5. 12. Persistence
      1. Pushing Persistence to the Edges
      2. Command-Query Separation
      3. Bounded Contexts Must Own Their Data Storage
      4. Working with Document Databases
      5. Working with Relational Databases
      6. Transactions
      7. Wrapping Up
    6. 13. Evolving a Design and Keeping It Clean
      1. Change 1: Adding Shipping Charges
      2. Change 2: Adding Support for VIP Customers
      3. Change 3: Adding Support for Promotion Codes
      4. Change 4: Adding a Business Hours Constraint
      5. Dealing with Additional Requirements Changes
      6. Wrapping Up
      7. Wrapping Up the Book