Hard Coding as a design
      approach
 Oren Eini aka Ayende Rahien
     http://ayende.com
    ayende@ayende.com
e
Why building traditional 3 layers, repository
laden, abstractions heavy applications is like
             drowning puppies.
A bit of history
• A long time ago….
But it got better
Typical architecture?
• Disconnected apps
• No network
• 1 user
Then came the network…
• Concurrent users (low number)
• One server *
• Many clients
What is a server…
• A database
Problems with this approach
• Clients have direct access to db.
• Can modify data at will.

• Solutions?
Create an API
• Server is still a DB.
• API built using Stored Procedure.
• Security via controlling access to those sprocs.
Typical architecture?




      Clients –    Stored Procs
                                  Database
   Windows apps,       “API”
Then came the web…
• Many users

• How to deal?
If it ain’t borken….
Minimum modifications




     Clients –    Actual Server –   Stored Procs
                                                   Database
  Windows apps,    BL goes here         “API”
3 Tiered Architecture
• Actually all about connection pools

• Later..
   – Security
   – Robustness
   – This is how you DO it
Today?
• 3 tiered systems rarely required.
• Usually
  – 2 tier (web / db)
  – Multi tier ( web farm / db farm)
  – System broken to multiple apps
Why the history lesson?
In software…
• Long time ago…

• Direct DB access

• Then SP…
How did it look like?
• usp_FindCustomerById(@id)
• usp_FindCustomersByName(@name)
• usp_FindCustomersByNameSortLatest()
Let the years roll
Now?

                                         OR/M •
                   Customers
                   Repository         Database •



       Customers
        Service


                         Customers
                         Controller
Do you want to see it in action?
Controller calls…
Business Logic!
DAL
What I think of this
Good architecture?
• Maintainable?
• Easy to work with?
• Easy to change?
But we use an OR/M…
• And we have repositories and services and IoC
  and Linq and goodness and spice.
My precious…
Typically…
Implemented as…
Usage
Digging down..
Spot the differences
• ITaskService.GetTaskById()   • usp_GetTaskById
Controller calls…
Why so cruel?
Half way there…
The truth about reusable code…
What do I suggest?
Simplicity, part I
Simplicity, Part II
Limit your abstractions
• Very small number, 4 – 8 in most apps
• Possible abstractions:
  – Controllers
  – Views
  – Entities
  – Commands
  – Tasks
  – Events
  – Queries
Small infrastructure
• Enough to give you context for running
• < 5% of your app, or you lose

• Allow to make assumption about where you
  run
Forget your testing instincts
• Code that makes assumptions about where it
  is running is good code.*

  – I have a way to access db
  – I have a way to get current context info



• * for a given set of assumptions
For that matter, what about testing?
Most tests end up here…
Unit testing?
• Do you actually care about the unit?

• I care about the system, not the unit.

• What happen when I need to refactor?
  – Unit Tests make assumptions about
    implementation.
     • You’ll have a User Service
Unit testing, my way…
And the test…
What you end up with?
• A lot of classes, following same patterns.
• Cookie cutter code.

• Easy to get into
• Easy to understand

• Major decisions in infrastructure (when to
  run, how to run, what is the context)
How to ensure it works?
• Tests
• System tests, actually testing meaningful stuff
Summary
• Reduce abstractions
• Limit scope of actions
  – As few code / layers between input & reaction
• Avoid the buzz
Question & Lynching

Hard Coding as a design approach