Download - Principled N-Tier Design

Transcript
Page 1: Principled N-Tier Design

Principled N-Tier Designor,

a Solution to the Solution Problem

Steve Smith@ardalis | ardalis.com

Telerik, Inc.

Page 2: Principled N-Tier Design

The Problem

Page 3: Principled N-Tier Design

Solution Level Pain

• Too Many Projects

• Too Few Projects

• Incorrect Abstraction

• The “Goldilocks” Solution– “Just right”

Page 4: Principled N-Tier Design

Relevant Principles

• Common Closure Principle• Common Reuse Principle• Stable Dependencies Principle• Stable Abstractions Principle• Separation of Concerns• Dependency Inversion Principle

Page 5: Principled N-Tier Design

Common Closure Principle

Classes that change together are packaged together.

Page 6: Principled N-Tier Design

Common Reuse Principle

Classes that are used together are packaged together.

Page 7: Principled N-Tier Design

Stable Dependencies Principle

Depend in the direction of stability.

Page 8: Principled N-Tier Design

Stable Abstractions Principle

Abstractness increases with stability.

Stability

Abst

ract

ness

Ideal

Rigid

Useless

Page 9: Principled N-Tier Design

More Principles

• Separation of ConcernsEstablish boundaries to separate behaviors and responsibilities within a system.

• Dependency InversionDepend on abstractions, not concrete implementations.

Page 10: Principled N-Tier Design

DEMOA Simple Guestbook Application

Page 11: Principled N-Tier Design

Testability

• Testability correlates with:– Loose coupling– Separation of Concerns– Maintainability

• You don’t have to have unit tests to have testable code!– Unit tests prove (or disprove) testability

Page 12: Principled N-Tier Design

DEMOAdding “Unit” Tests

Page 13: Principled N-Tier Design

SMTP4DEVhttp://smtp4dev.codeplex.com/

Page 14: Principled N-Tier Design
Page 15: Principled N-Tier Design

Incremental Improvement

• From a Single Project– Separate responsibilities using folders– Add Project(s)

• From Many Projects– Create a new Solution with only what you need– Spin off separate applications into own solutions

Page 16: Principled N-Tier Design

DEMOSeparation via Folders (still 1 project)

Page 17: Principled N-Tier Design

Problems

• Tests are still not unit tests– Tight coupling– Static Cling– Dependence on Data and Email Infrastructure concerns

• Nothing to prevent improper referencing of classes– e.g. UI code can call DAL code directly and bypass business

logic

• UI Content Folders mixed with Application Code Folders

Page 18: Principled N-Tier Design

Onion Architecture

• aka Ports and Adapters, • aka Hexagonal Architecture

• Core models and interfaces at center• Infrastructure and Implementation depends

on Core• UI depends on Core and has access to

Infrastructure

Page 19: Principled N-Tier Design

Onion Architecture

Page 20: Principled N-Tier Design

DEMOIntroduce Abstractions and Invert Dependencies (in folders)

Page 21: Principled N-Tier Design

Results

• Yay! Unit Tests that work!

• Boo! Still nothing to prevent improper referencing of classes– e.g. UI code can call DAL code directly and bypass

business logic

• Boo! Still mixing content and code folders

Page 22: Principled N-Tier Design

DEMORefactor into Projects

Page 23: Principled N-Tier Design

Results

• Testable Code• Separation of Concerns• Loose Coupling• Minimal logic in UI layer

A solid foundation for a maintainable application!

Page 24: Principled N-Tier Design

Automate

• One Click Build• One Click Test

Page 25: Principled N-Tier Design

DEMOSolution Automation – Build Scripts

Page 26: Principled N-Tier Design

DEMOMore Tests

Page 27: Principled N-Tier Design

References

• Separation of Concerns - http://bit.ly/zWujRe • Hexagonal Architecture - http://bit.ly/8Uxl5• Onion Architecture - http://bit.ly/4tWMT3• More Onion Architecture - http://bit.ly/NzF2Sz • New is Glue - http://bit.ly/Ijn98e

Pluralsight Resources• N-Tier Application Design in C# http://bit.ly/Msrwig • Design Patterns Library http://bit.ly/vyPEgK • SOLID Principles of OO Design http://bit.ly/OWF4la

Page 28: Principled N-Tier Design

Thanks!

• Find me at http://ardalis.com

• We’re hiring developers and trainers at Telerik!

• http://telerik.com/company/careers.aspx