liveBook · Manning

Domain type

As explained in Functional Design and Architecture: Examples in Haskell

A domain type is a program type whose values represent concepts from the real-world problem domain.

A domain type gives program values the shape of a real-world concept, such as a meteor with size, mass, coordinates, and time. It belongs to the domain model rather than the storage scheme. section 11.2.1 section 12.1.1

Keep domain types separate from database types when persistence introduces its own representations:

  • Map a domain type to a database model before storing it, rather than exposing database-specific keys, timestamps, or fundamental types to domain logic. section 11.2.1 section 12.1.1
  • Use conversion functions in both directions so changes such as extracting coordinates into a separate type do not require changing the database representation. section 12.1.1
data Meteor = Meteor
 { size      :: Int
 , mass      :: Int
 , azimuth   :: Float
 , altitude  :: Float
 , timestamp :: DateTime
 }

The Meteor declaration makes the domain concept concrete with native fields for size, mass, coordinates, and timestamp. section 12.1.1

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains definition, database mapping, bidirectional conversion, and isolation, but does not develop broader domain-modeling techniques.
Examples
Uses a Meteor type with physical properties, a database-specific Meteor representation, coordinate extraction, and JSON or SQL mappings.
What you can do after
Apply it via conversion functions that isolate domain logic from database representations and accommodate domain-type changes.
How it connects
Connects domain models with database models, database entities, relational databases, algebraic data types, and conversion functions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.