Pareto Optimal Dev

How database models in domain logic undermine type-safety

Imagine you have a User model:

data User =
  User { userId :: Int
       , userName :: String
       }

To keep things “simple” (for at least our brand of simplicity) and not duplicate code we also use this type in our domain logic.

userSignup :: Servant.Handler
userSignup = do
  -- ... snip ...
  runDB insert User{..}

TODO change welcome example to content or article ex

Then akso on the welcome page greeting after a user signs in:

welcome :: Servant.Handler
welcome = do
  -- ... snip ...
  putStrLn "Hello " ++ userName u

Your site starts getting popular, but currently obly has free users. You are tasked with creating paid content that only premium users can access.

Trying to find the fastest si,plest solution without any re-architecting you make a few changes.

You update the User to have a type field. For type safety, you make this an ADT:

data UserType = Free | Premium
data User =
  User { userId :: Int
       , userName :: String
       , userType :: UserType
       }

Then your updated article handler looks like:

serveArticle :: ArtickeID -> Servant.Handler
serveArticle aId = do
  -- ... snip ...
  a <- runDB $ getArticke aId
  case artickeType a of
    PremiumArticle -> case userType u of
        Free -> Http400 "denied"
        Premium -> displayArticle articke

A couple weeks go by and you have 20 or 30 premium users. In order to keep them, the produvt owner asks you to implement a rewards point system onlybapplicable to Premium users. Each time a premium user reads a premium article they get 5 points and 1 point for any free article.

TODO premium users should have rewardPoints that increment every day, free users don’t have any TODO vroadcast messages to all users TODO demo a more type safe approach

then talk about how default things two separating model and domain types even in very simple cases is probably a good ideabecause of the fact how you architecture things every step of the waywith its resistance

and go over how to recover if you are in this sityation