Table Of ContentTableUse ro Deexpvfeelroi epnCmcee nfiotr satn . n .d . d . .et .si .ge .n . .vn .e .c .t .ot .rs .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .3 .8 . . 64
FocuSsO oLnI Din pterrinaccitpiolenss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . .3 .8 . . 64
UX isP naottte UrnI s . f .o .r . .h .a .n .d .l .in .g . .d .e .p . .e .n .d .e .n .c .i .e .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .4 .0 . . 69
HowC tood cirnega tvee actno ersf fe .c .t .i .v .e . e .x .p .e .r .i .e .n .c .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .4 .3 . . 72
Realistic scUensea roiof sp . a . .tt .e .r .n .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .4 .7 . . 75
ASP .NReEfTa cwteobrisnitge .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .4 .7 . . 77
Introduction xiii
DWefeenbs Fivoer mprso vgsr .a AmSmP .NinEgT . .M .V . .C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .5 .2 . . 78
ErraAtad, duiTpnhgde ad tIeef-vsT,i hc&ee nbsu-oTpohpkro osrwutp tppoao twrttee .rbn .s . . it . .e . .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . .5x .5v . .iii78
SinglSeo-pftawgaer ea pcopnlictraatciotsn . s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .6 .0 . . 79
Free ebooks from Microsoft Press . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
SuDmesmkatoryp . r . i .c .h . c . l .i e . n . t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . 6 . 4 . . .83
We want to hear from you . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
SummFinairsyh . i .n .g . .w . i .t h . . a . .s m . . i .l e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .6 .6 . . 84
Stay in touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Finishing with a smile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Writing software of quality 85
Foundation 1
The mythical business layer 167
The art of writing testable code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Architects Paanttde ransr cfohWri otheragcta tinsu itzreienstg at bothidleit ayb,yu a sninyewsas yl?o . g .i .c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .6 .7 . .386 Microsoft .NET:
WhaTth’se sfToaefirstywtitnaagrlee y aoorfuc Crh RistoUefcDttw uaarnerde, a .an .n .y .aw .rac .yh .?i . .t . .e . .c . .tu . . .r .e . . . .P . .r . .in . .c . .e . . . .C . .h . .a . .r . .m . . . .in . . . .g . . . . . . . . . . . . . . . .1 . .6 . . .8 . . .488
ThAe pTCproalymnisnmagco tanior cpnh rSiatcecrtciiptcuet rspa oal tpf tsreionrnfc .t iw .p .ale .rs .e . tto .e . ss .toi .nf .tg .w . . a . .r . .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . . .6 . .9 . . .496 Architecting Applications
ThTeh Aep crDkaonctmoicwaeli neod fM gcioonddgee rl eepqxatuteitnreesrminb . ei .lni .tty .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . . .7 . .2 . . 1701
ThWe AIhnnattee’msr faaicrcc eDh-oibtmeacsateiudnr edM eaosniddge nwl . ( .ha .an .tt’ .is- . )np .o .a .ttt . .e . .r . .n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . .7 . .4 .11102 for the Enterprise,
Moving tThhePe l fuaogrcciunhs i atferrccohtmuitr edec aptturaro etc .o e . s .tsa . .s . .k . .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . .7 . . .6 .11403
WhoTa’ss tkh Soetr aacthrece hmsittareaccthtioi,n anen siyn .w .A .aS .yP .? . . N . . .E . .T . . .M . . . .V . .C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . .7 . . .7 .11703 Second Edition
WOrirtAcinhnge a scrtorcahdtieitn etgch tat’sat sroketssh pweoristnh sciianbn itl ihrteieea sd .d .o . . .m . .a . .i . .n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . . .8 . . .0 .11804
Moving dTahRteae raaodclerao bosifsli tttyhh eea sab raoc ushonitfdetacwtra ier .es . . . a . .t . .tr . .i .b . . .u . .t . .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . .8 . . .2 .21005
DaCtaoS mfloommweo innp rmlaaciystecicroeandl cr eauprlcethiso iftnoesrc atimubropeur .ot . va .ir .nc .gh . i .rtee .a .cd .ts .a . . .b . .il . .it . .y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . .8 . . 2 . 2 .1107
SuSmuSmmhaamrriyan rg . y . .t .h . . .e . . .D . . .o . .m . . . .a . .i .n . . .M . . . .o . .d . . .e . .l . .e . .n . .t . .i .t .i .e . .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . . 8 . . . 4 . 2 .1409
Using data-transfer objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
FinFiisnhisinhgin wg iwthi tah sam simle .i l .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21510
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
DeDseigvinsiinngg Fftiohnires h saiunrgcc cwheiittshes a c tsmurilee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18821711
Supporting architeTchteu “rBeisg Ball of Mud” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Dino Esposito
Discovering the domain architecture 113
Causes of the “Big Ball of Mud” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Introducing Domain Model 191 Andrea Saltarello
The Sryemal patdodmesd ovfa tlhuee “oBf idgo Bmalal ionf- dMriuvden” . d .e .s .i .g .n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31214
The dataU-tsoWin-bhgae mth’sae vtinrioi cDrs DsthoDi f dtfoe . .tre .mc .t .e .a? . . B . .B . . .M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . . .9 . .1 .31414
RatioCnoanled ubcethiningd a mnaoldyseilss uasnindg d DomDDain . .s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .9 .2 . 115
Mechanics of software projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
DataSbtarasete igs iicn fmraosdtreul cdtuesreig .n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .9 .5 . 116
Organizational culture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
InsidTeh teh Heu ebdliopqmiuniagtoi ntuh slea l yateenrag m .u .a w .g .re . .i t . .e . . b . . . .e . .tt . .e . .r . . c . .o . . . .d . .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . . .9 . . .6 .31818
DomPauinr pmoosed eolf . t .h .e . .u .b .i .q .u .it .o .u . .s .la .n . .g .u .a .g .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 . .9 .6 . 118
Getting out of the mess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
AggrSetgruactetus r . e . .o .f . t .h .e . .u .b .i .q .u .it .o .u .s . .la .n . .g .u .a .g .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 .9 .9 . 119
That odd thing called “legacy code” . . . . . . . . . . . . . . . . . . . . . . . . . . .44
DomHaionw s etorv diceefis .n .e . t .h .e . .u .b .i .q .u .i .to .u . .s .la .n . .g .u .a .g .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 .0 .5 . 119
Checkmate in three moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
DomKaeine peivnegn tlas .n .g .u .a .g .e . .a .n .d . .m . .o .d .e .l . i .n . s .y .n .c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 . .0 .9 . 121
Deciding whether or not to add manpower . . . . . . . . . . . . . . . . . . . . 49
Cross-cutting concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Bounded contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
Summary . .D . is . c . o . v . e . r . i .n .g . .c .o .n .t .e .x .t s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 .1 .5 . 122
Finishing with a smile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Finishing wCitohn ate sxmt imlea . p . .p .in . .g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 .1 .5 . 125
Giving each context its own architecture . . . . . . . . . . . . . . . . . . . . . 127
Principles of software design 53
Implementing Domain Model 217
The layered architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Universal principles of software design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
The online Ostroigrein ssa omf pthlee plaryoejerectd . . a .r .c .h .it .e .c .t .u .r .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 .1 .8 . 129
From spaghetti code to lasagna code . . . . . . . . . . . . . . . . . . . . . . . . . 54
SelecPtreeds eunstea-tcioasne lsa . y .e .r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 .1 .8 . 131
Separation of concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
SelecAtepdp laicpaptiroona clahy . e .r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 .1 .9 . 132
Isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
StrucDtuorme aoifn t lhaey eI-r B . .u .y .- .St .u .f .f . p .r .o .j .e .c .t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 .2 .0 . .134
Object-oriented design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
SelecIntefrda stetrcuhcntoulroeg laieyse . r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 . .2 .2 . 134
Pertinent classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
SuBmoumnadreyd . . c . o . n . t . e . x . t .s . f .o . r . a . n . . o . n . l . in . .e . s . t .o .r .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 . 2 . 2 . .135
Program to an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Context map of the I-Buy-Stuff application . . . . . . . . . . . . . . . . . . . 224
FinisChoinmgp woistihti oan s mvsi .l ein . .h .e .r .it .a .n .c .e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61135
A second pass at object-orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The presentation layer 137
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2014 by Dino Esposito and Andrea Saltarello
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any
means without the written permission of the publisher.
Library of Congress Control Number: 2014940680
ISBN: 978-0-7356-8535-2
Printed and bound in the United States of America.
Second Printing: January 2015
Microsoft Press books are available through booksellers and distributors worldwide. If you need support related
to this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think of
this book at http://aka.ms/tellpress.
Microsoft and the trademarks listed at http://www.microsoft.com/en-us/legal/intellectualproperty/Trademarks/
EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their respective
owners.
The example companies, organizations, products, domain names, email addresses, logos, people, places, and
events depicted herein are fi ctitious. No association with any real company, organization, product, domain name,
email address, logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without
any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or
distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by
this book.
Acquisitions and Developmental Editor: Devon Musgrave
Project Editor: Carol Dilllingham
Editorial Production: Waypoint Press, www.waypointpress.com
Peer Reviewer: Cesar De la Torre Llorente
Copyeditor: Roger LeBlanc
Indexer: Christina Yeager
Cover: Twist Creative • Seattle and Joel Panchot
To my wife Silvia. You make me feel sandy like a clepsydra. I get empty and filled all the time; but
it’s such a thin kind of sand that even when I’m full, without you, I just feel empty.
—Dino
To Laura, mum, and Depeche Mode. Moved, lifted higher. Moved, by a higher love.
—AnDreA
This page intentionally left blank
Contents at a glance
PART I FOUNDATION
CHAPTER 1 Architects and architecture today 3
CHAPTER 2 Designing for success 27
CHAPTER 3 Principles of software design 53
CHAPTER 4 Writing software of quality 85
PART II DEVISING THE ARCHITECTURE
CHAPTER 5 Discovering the domain architecture 113
CHAPTER 6 The presentation layer 137
CHAPTER 7 The mythical business layer 167
PART III SUPPORTING ARCHITECTURES
CHAPTER 8 Introducing Domain Model 191
CHAPTER 9 Implementing Domain Model 217
CHAPTER 10 Introducing CQRS 255
CHAPTER 11 Implementing CQRS 291
CHAPTER 12 Introducing event sourcing 311
CHAPTER 13 Implementing event sourcing 325
PART IV INFRASTRUCTURE
CHAPTER 14 The persistence layer 353
This page intentionally left blank
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii
PART I FOUNDATION 1
Chapter 1 Architects and architecture today 3
What’s software architecture, anyway? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
Applying architectural principles to software . . . . . . . . . . . . . . . . . . . .4
Acknowledging requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
What’s architecture and what’s not . . . . . . . . . . . . . . . . . . . . . . . . . . .11
The architecture process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Who’s the architect, anyway? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
An architect’s responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
The role of the architect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Common misconceptions about architects . . . . . . . . . . . . . . . . . . . . .21
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
Finishing with a smile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
Chapter 2 Designing for success 27
The “Big Ball of Mud” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Causes of the “Big Ball of Mud” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Symptoms of the “Big Ball of Mud” . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Using metrics to detect a BBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
Mechanics of software projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Organizational culture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Helping the team write better code . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Getting out of the mess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
That odd thing called “legacy code” . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Checkmate in three moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Deciding whether or not to add manpower . . . . . . . . . . . . . . . . . . . .49
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
Finishing with a smile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51
What do you think of this book? We want to hear from you!
Microsoft is interested in hearing your feedback so we can continually improve our
books and learning resources for you. To participate in a brief online survey, please visit:
microsoft.com/learning/booksurvey
vii
Chapter 3 Principles of software design 53
Universal principles of software design . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
From spaghetti code to lasagna code . . . . . . . . . . . . . . . . . . . . . . . . .54
Separation of concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Object-oriented design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Pertinent classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Program to an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Composition vs . inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
A second pass at object-orientation . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Development and design vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
SOLID principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
Patterns for handling dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Coding vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
Use of patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75
Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
Defensive programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78
The If-Then-Throw pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78
Software contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
Finishing with a smile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84
Chapter 4 Writing software of quality 85
The art of writing testable code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
What is testability, anyway? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
Testing your software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Common practices of software testing . . . . . . . . . . . . . . . . . . . . . . . .96
The practice of code extensibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
Interface-based design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
Plugin architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
State machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
Writing code that others can read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104
Readability as a software attribute . . . . . . . . . . . . . . . . . . . . . . . . . . .105
Some practical rules for improving readability . . . . . . . . . . . . . . . . .107
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
Finishing with a smile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110
PART II DEVISING THE ARCHITECTURE 111
Chapter 5 Discovering the domain architecture 113
The real added value of domain-driven design . . . . . . . . . . . . . . . . . . . . .114
What’s in DDD for me? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114
viii Contents
Conducting analysis using DDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115
Strategic model design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116
The ubiquitous language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118
Purpose of the ubiquitous language . . . . . . . . . . . . . . . . . . . . . . . . .118
Structure of the ubiquitous language . . . . . . . . . . . . . . . . . . . . . . . .119
How to define the ubiquitous language . . . . . . . . . . . . . . . . . . . . . .119
Keeping language and model in sync . . . . . . . . . . . . . . . . . . . . . . . .121
Bounded contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
Discovering contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
Context mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
Giving each context its own architecture . . . . . . . . . . . . . . . . . . . . .127
The layered architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Origins of the layered architecture . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Presentation layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131
Application layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132
Domain layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134
Infrastructure layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
Finishing with a smile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
Chapter 6 The presentation layer 137
User experience first . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Focus on interactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
UX is not UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140
How to create an effective experience . . . . . . . . . . . . . . . . . . . . . . .143
Realistic scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147
ASP .NET websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147
Web Forms vs . ASP .NET MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
Adding device support to websites . . . . . . . . . . . . . . . . . . . . . . . . . .155
Single-page applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160
Desktop rich client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
Finishing with a smile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
Chapter 7 The mythical business layer 167
Patterns for organizing the business logic . . . . . . . . . . . . . . . . . . . . . . . . . .167
The fairytale of CRUD and an architecture Prince Charming . . . . .168
The Transaction Script pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169
The Domain Model pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172
The Anemic Domain Model (anti-)pattern . . . . . . . . . . . . . . . . . . . .174
Moving the focus from data to tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
Task orchestration in ASP .NET MVC . . . . . . . . . . . . . . . . . . . . . . . . . .177
Orchestrating tasks within the domain . . . . . . . . . . . . . . . . . . . . . . .180
Contents ix
Description:Microsoft .NET: Architecting Applications for the Enterprise,. Second Edition. Dino
Esposito Neither the authors, Microsoft Corporation, nor its resellers, or.