pragmatic architecture in .net

48
Pragmatic Architecture in .NET Cory House @housecor | bitnative.com

Upload: housecor

Post on 14-Dec-2014

1.167 views

Category:

Technology


1 download

DESCRIPTION

An architect’s job is to reduce complexity, not increase it. Yet the developer life is filled with jargon, acronyms, and seemingly infinite choices. So how do we know when complexity makes sense? We’ll learn when abstractions are justified and discuss how to structure applications so they’re maintainable, scalable, and testable. We’ll make sure everyone is comfy with the core jargon like N-Teir, separation of concerns, and loose coupling. Then we’ll dive into various patterns for implementing the three common layers: data access, business logic, and presentation. You’ll learn when table module, active record, DDD, and ORMs make sense and walk away with the tools to better evaluate and justify complexity. We’ll focus on the value of keeping things simple whenever we can. Examples are presented in C# in Visual Studio but developers in Java, PHP, and other C-like languages should be able to follow right along.

TRANSCRIPT

Page 1: Pragmatic Architecture in .NET

Pragmatic Architecture in .NETCory House

@housecor | bitnative.com

Page 2: Pragmatic Architecture in .NET

The problem.

Page 3: Pragmatic Architecture in .NET

X is good It Depends

Page 4: Pragmatic Architecture in .NET

Three Simple Goals:

1. Cost vs benefit2. Contrast extremes3. Review our options in .NET

Page 5: Pragmatic Architecture in .NET

Why?

Save money.Save effort.Save time.

Page 6: Pragmatic Architecture in .NET

Why Not?

To improve my resume’.To challenge myself.To experiment.

Page 7: Pragmatic Architecture in .NET

Assumptions

1. Web based2. RDBMS3. Enterprise line of business app

Page 8: Pragmatic Architecture in .NET

Astronaut Assessment

We must use <shiny new technology> for this!

Reinvent the wheel100% test coverageNever ORMAlways code to an interfaceUse all the patternsResume Driven Development

Page 9: Pragmatic Architecture in .NET

Consider Simplicity

1. Do simplest thing that could possibly work2. Lean / Agile principles3. YAGNI4. 80/20 Rule

Page 10: Pragmatic Architecture in .NET

Consider Complexity

• Unit Testing• Coding to an interface• SOA• Rich Domain Model• Layered N-teir architecture

Page 11: Pragmatic Architecture in .NET

Which is better?

Tightly Coupled

No Tests

No Layers

No API

Coded to Interfaces

100% Test coverage

Rich Domain Model

Service Oriented

Page 12: Pragmatic Architecture in .NET

It depends.

Page 13: Pragmatic Architecture in .NET

What if I said…

• $250k over budget• Customers hated it, or worse, ignored it• Idea didn’t pan out• Project funding dried up• 1 month late = worthless

Page 14: Pragmatic Architecture in .NET

Work expands to fill available time.Parkinson’s Law

Page 15: Pragmatic Architecture in .NET

Agile Estimation: Flex one.

Quality

Date Cost

Features*ArchitectureReusePerformanceSecurityTestingScalabilityDocumentation

* Fat chance

Page 16: Pragmatic Architecture in .NET

Quality is reduced to meet deadlines.

Software quality is directly related to deadline sanity.

Deadline and manpower are often a constant. Thus, software quality is directly related to deadline sanity.

Quality software requires realistic deadlines.

Page 17: Pragmatic Architecture in .NET

Q: Is debt bad?

Later

Now

A: Is the deadline hard?

Page 18: Pragmatic Architecture in .NET

Hard vs Soft Deadlines

Trade showPublished advertisingX-team dependencies1st to market or deadNetwork effects

Single loud customerWAGSalesman misspokeMS Project said so

Page 19: Pragmatic Architecture in .NET

Perfect architecture? Who cares.

FacebookTwitterStackOverflow

The problem is getting people to care.

Page 20: Pragmatic Architecture in .NET

We’re paid for solutions, not code.

Unit Testing – Insurance– Investment in the future (ease of maint)– Potential long-term cost in complexity/manpower

Page 21: Pragmatic Architecture in .NET

Enough Theory…

Let’s get geeky.

Page 22: Pragmatic Architecture in .NET

Presentation

Service

Domain

Data

Web Forms vs MVCJavaScript, CSS, jQuery, Knockout, Angular, IoC

Web API, WCF, ASMX, ServiceStack or POCOs

C#, VB.Net

Entity Framework, nHibernate, LLBLGen, Dapper, ADO.Net

Page 23: Pragmatic Architecture in .NET

UI

API

Business Logic (BLL)

Data Access (DAL)

Presentation

Service

Domain

Persistence

Page 24: Pragmatic Architecture in .NET

Layers vs Tiers

Logical Physical

Page 25: Pragmatic Architecture in .NET

Tiers

ScalabilitySecurityAbstract complexityMinimize dependencesReusable

Performance costIncreased complexity

Page 26: Pragmatic Architecture in .NET

Layers

Separate concernsAid understandingAbstract complexitySupport testingMinimize dependencesStandardizeEnable reuse

LeakMore code

Page 27: Pragmatic Architecture in .NET

Small app?methods = layers.

Page 28: Pragmatic Architecture in .NET

ORM honors DRY

1. Database 2. Class property3. Select statements4. Insert statements5. Update statements6. Delete statements7. SQL marshalling code

1. Database 2. Class property3. .hbm.xml file

Page 29: Pragmatic Architecture in .NET

When ORM?

Go for it:• DRY = faster

development• Speedier changes• RDBMS agnostic• Type safe interface• Avoid writing SQL• SQL Injection protection• Security, performance,

caching, mapping OOTB

Pass:• Lose control over SQL• DBAs lose control• Security concerns• Performance penalty

Page 30: Pragmatic Architecture in .NET

SOA

Heterogeneous systemsAutonomous ReusableFormal

Performance hitRisky changesBoth sides must test

Page 31: Pragmatic Architecture in .NET

Architectural LevelsL1 Simplest

thingL2 Somewhere

in between

L3 Every tool in the shed

Page 32: Pragmatic Architecture in .NET

Level 1

Presentation

Domain & Persistence

Web Forms

Active Record Pattern Linq-2-SQL, Subsonic, Castle ActiveRecord

Page 33: Pragmatic Architecture in .NET

Active Record

Presentation

Domain

Data

Page 34: Pragmatic Architecture in .NET

Active Record

Customer Address Purchase

Customer Address Purchase

DB Tables

ClassesOne instance = One row

Page 35: Pragmatic Architecture in .NET

Active Record

Simple & obviousSpeedy dev and changesCompliments CRUD appsGood for simple domainNo OR mismatchHonors YAGNI

Rigid: Domain model = DBLeads to God objectLow cohesion: Breaks SRPHard to testTricky transactionsRepository pattern = pain

Page 36: Pragmatic Architecture in .NET

Level 3

Presentation

Service

Domain

Persistence

ASP.Net MVC, Knockout/Angular

Repository Pattern nHibernate, EF POCOs, Dapper

DDD with POCOs

WCF, ServiceStack

Page 37: Pragmatic Architecture in .NET

Domain Model

It’s all about the objects.

Page 38: Pragmatic Architecture in .NET

Domain Model

Customer Address Purchase

Customer with list of addresses

Purchase

DB Tables

ClassesNote OR Mismatch

Page 39: Pragmatic Architecture in .NET

Business Layer Patterns: Domain Model

Complex business domainLeverage design patternsSpeak business languageAbstracts ugly DB schemaLarge teamReusable

Learning curveTime-consuming designLong-term commitmentDB mapping overhead

Page 40: Pragmatic Architecture in .NET

Level 3 SummaryTestableManage object lifecycleAOP via dynamic proxiesSwappable UI or DALSpeaks in business languageLeverage design patterns

Slower initial devLarger initial dev costLearning curveMore places to changeSenior developers

Page 41: Pragmatic Architecture in .NET

Bottom Line: L1 vs L3

Page 42: Pragmatic Architecture in .NET

Points to Add a Feature

Level 11. UI2. DB 3. Implement BL (in AR class)4. Regen DAL

Level 31. UI2. DB3. Implement BL in POCO4. Update/create interface5. Update IoC bootstrap6. Update DB mapping7. Update/Add service call8. Add/update tests

Page 43: Pragmatic Architecture in .NET

Choice by Complexity

Level 3

Level 2Level 1

Effort to enhance

Application Complexity

Page 44: Pragmatic Architecture in .NET

Architectural LevelsLevel 1 Level 2 Level 3

Centralized Data Access ?

Mockable Data Access ?

Central Lifetime Management ?

Separation of Concerns ?

Domain Driven Design ?

Unit Testing Friendly ?

Concurrent Development ?

Service Oriented ?

SOLID ?

SPA friendly ?

Simplest thing/YAGNI

Page 45: Pragmatic Architecture in .NET

Architecture Selection

MVPJunior teamSmall teamSimple DomainTight timelineThrowawayNo security concernsLittle chance for reuse

Flagship productSenior TeamLarge Team

Complex domainFlexible timeline

Long-termSecurity matters

Other apps could reuse

L1 L3

Page 46: Pragmatic Architecture in .NET

Reading

Page 47: Pragmatic Architecture in .NET

Bottom Line

1. Consider the simplest thing that could possibly work.

2. Context matters. X isn’t good or bad.3. Best practices have a benefit and cost.

Page 48: Pragmatic Architecture in .NET

Cory House

@housecor | bitnative.com