Chapters 1&2 of
Patterns of Enterprise Application Architecture
Benefits
- A single layer is a coherent whole
- Layer substitution made easy
- Minimize dependency
- Good place for Standardization
- Usable for many other services
Downsides
- A simple change in one layer may need all other layers to be changed, e.g. adding a field on the UI
- Performance
Layering was there from early days
- Client (UI) , Server(Database)
- Where was business logic?
WEB
The rise of the WEB made it necessary to have an intermediate layer for business logic.
Layer vs. Tier
Layers need not be physically distinct.
Three Principal Layers
Presentation, Domain, and Data Source
How these 3 layers communicate?
- Many times, Presentation layer does not have access to Data Source layer.
- D.S. and Domain layer should not have any call to presentation layers
- Transaction Script
- Table Module
- Domain Model
Transaction Script
One procedure per action:
Do every thing(get data, process, store in DB, etc.) in one procedure.
It is simple , works well, and is easy to develop.
but
very complex as the domain logic grows, duplicate codes starts to appear, and is hard to maintain.
Domain Model
Build everything around domain.
Domain is important entities(nouns) in the system.
So a banking system might have domains
loan,
customer,
account, etc.
It's complex and needs more code.
Table Module
It's somewhere in between. Domains are just tables of the database.
Service Layer
It's good to separate the interface that communicates to presentation layer into a different layer:
Service Layer
It does controlling, security checking, ...
Basically everything which is not domain dependent.
Model View Controller

Transform View/ Template View
Transform View: (e.g. XSLT) it's like a program that takes the input and convert it into presentation
Template View: (e.g. ASP, JSP, PHP) embed markes inside the presentation
Single Stage View vs. Two Step View
Single Stage View: The entire HTML output is produced at once.
Two Step View: An intermediate logical output is first created and the HTML output is produced in the second round

Input Controller Patterns
Page Controller: One controller per action
Front Controller:
Process Request and Decide what to do in a central place
Chapter 5 of PEAA
- Concurrent manipulation of data
Lost update
A: reads B: reads B: updates A: updates
Inconsistent Read
A: read; B: update; A: continues reading.
Correctness vs. Liveness
Which one you care most?
Terminology
- Session vs. Request
- Process vs. Thread
- DB Transaction
Solutions: isolation and immutability
Isolation: partition resources and put a lock on each!
Immutability: Identify those data that are not modifiable.
Optimistic vs. Pessimistic Concurrency Control
Deadlocks
happens in Pessimistic control.
A edits X
B edits Y
A requires access to Y
B requires access to X
How to handle them?
- Detect and pick a victim!
- Give timeout to locks!
- All locks at once
- Prevent Deadlock
Transactions
They need to have ACID properties:
- Atomicity: Partial completion is not allowed
- Consistency: The system state must always be consistent before and after the transaction
- Isolation: No intermediate result is visible
- Durability: The result should be permanent and survive from any sort of crash