When business logic directly handles HTTP requests or database details, changes become harder to isolate and tests can require external subsystems. A multi-tier application separates responsibilities into layers with explicit boundaries and controlled dependencies. section 14.2.2 section 4.3
A layer is a logical division organized around an architectural purpose or level of abstraction. It is not the same as a component: one component can span several layers, and one layer can contain several components. section 2.2.1 section 3.2
Common responsibilities include:
- Application: configure and initialize the program and bring its parts together. section 2.2.1 section 7.1.1
- Presentation: provide a user interface and handle input. section 2.2.1
- Domain model and business logic: represent domain concepts and describe the work the application performs. section 2.2.1 section 7.1.1
- Persistence: provide abstractions for storing and retrieving data. section 2.2.1
- Implementation and interoperability: carry out operations and support communication without exposing unnecessary details. section 4.2.1 section 2.2.1
Give subsystems clear, minimal interfaces and avoid bypassing them. Review dependencies early, and split large layers into sublayers when that improves cohesion or reduces coupling. Functional services can make boundaries explicit and allow real implementations to be replaced by mocks in isolated tests. section 4.3 section 4.2.1
A stateful sandwich application
A recipe script expresses business logic using embedded language interfaces. An interpreter implements those interfaces and manages ingredient state; the application’s main function creates the state, runs the interpreter, and prints the result. The layers have distinct roles, while the application layer ties them together. section 7.1.1
Listing 7.1 A simple stateful application
ingredients :: Ingredients #1 ingredients = Map.fromList [ (Tomato, 10), (Bread Toast, 6) , (Salt, 4), (Cheese, 5) ] main :: IO () #2 main = do -- sudo make me a sandwich ingredientsRef <- newIORef ingredients #3 sandwich <- interpretRecipe ingredientsRef mySandwich #4 print sandwich
The main function shows where state, interpreter, and business logic are assembled. section 7.1.1
A typed database layer
A raw key–value interface returns byte strings without guaranteeing that their contents make sense to business logic. A typed interface can sit between that raw interface and the business logic, without changing the underlying framework or its interpreters. section 11.2
Layering involves architectural choices rather than a single mandatory arrangement. For example, a web handler can run a separate business-logic scenario instead of containing the logic itself, but the two environments still need an explicit connection. section 14.2.2 section 7.1.1






