Application Architecture April 2014

82
Application Architecture April 2014 Lars-Erik Kindblad Senior Consultant & Head Of Microsoft Community Of Practice

description

Slides from my talk on application architecture at Capgemini in Fredrikstad

Transcript of Application Architecture April 2014

Page 1: Application Architecture April 2014

Application ArchitectureApril 2014

Lars-Erik KindbladSenior Consultant

& Head Of Microsoft Community Of Practice

Page 2: Application Architecture April 2014

Feature Delivery For Most Software Projects

Project Progress• Complex and messy code• Code and logic is duplicated• No common coding standard• No strict architecture• ++

Page 3: Application Architecture April 2014

Ideal Feature Delivery

Project Progress

Page 4: Application Architecture April 2014

Agenda

Testable code Inversion of Control

Maintainable code with less duplication Single Responsibility Principle Extract Method Principle Execute Around Method Pattern

Well structured code N-Layered Architecture & Onion Architecture Ways to group and structure code

Reduce bugs Event Sourcing & replaying events

Page 5: Application Architecture April 2014

Inversion Of Control

Make code loosely coupledMake unit testing possible

Page 6: Application Architecture April 2014

Traditional Code

Page 7: Application Architecture April 2014

Inversion Of Control

Page 8: Application Architecture April 2014

Traditional code

Main class UserCreator

CreateUserDbCommand

1. Creates

2. Creates

Inversion of control code

Main classCreateUser

DbCommand

1. Creates

UserCreator

Dependency2. Creates withobject from 1.

Dependency

Page 9: Application Architecture April 2014

Dependency Injection - Constructor Injection

Page 10: Application Architecture April 2014

Dependency Injection - Interface Injection

Page 11: Application Architecture April 2014

Dependency Injection - Setter Injection

Page 12: Application Architecture April 2014

Static Service Locator

Page 13: Application Architecture April 2014

Injected Service Locator

Page 14: Application Architecture April 2014
Page 15: Application Architecture April 2014

Which Patterns To Use

Page 16: Application Architecture April 2014

Traditional Code

Page 17: Application Architecture April 2014

Dependency Injection - Constructor Injection

Page 18: Application Architecture April 2014

Dependency Injection - Interface Injection

Page 19: Application Architecture April 2014

Dependency Injection - Setter Injection

Page 20: Application Architecture April 2014

Static Service Locator

Page 21: Application Architecture April 2014

Injected Service Locator

Page 22: Application Architecture April 2014

When To Use Which Patterns

Page 23: Application Architecture April 2014

Always Use Dependency Injection – Constructor Injection

...except

Page 24: Application Architecture April 2014

Facade Classes

Page 25: Application Architecture April 2014

Dependency Injection

Page 26: Application Architecture April 2014

Service Locator

Page 27: Application Architecture April 2014

Loops

Page 28: Application Architecture April 2014

Dependency Injection

Page 29: Application Architecture April 2014

Service Locator

Page 30: Application Architecture April 2014

Base Classes

Page 31: Application Architecture April 2014

Dependency Injection

Page 32: Application Architecture April 2014

Service Locator

Page 33: Application Architecture April 2014

Unknown Types At Compile Time

Page 34: Application Architecture April 2014

Service Locator

Page 35: Application Architecture April 2014

Summary

Constructor Injection Injected Service Locator

Facade classes(WCF Services,MVC Controllers)

X

Loops X X

Base classes X

Unknown types at compile time

X

All other scenarios X

Page 36: Application Architecture April 2014

Inversion Of Control (IOC) Container

A framework that can automatically create an instance of a given type with all the required dependencies

Popular frameworks Unity, Castle Windsor, Ninject, StructureMap etc.

Manual approach

Using an IOC container

Page 37: Application Architecture April 2014

Configuration

Must register what types the container can resolveTypes are registered with a life time manager

PerContainer – Container.Resolve<UserCreator> returns the same UserCreator instance every time (Singleton)

Transient – Container.Resolve<UserCreator> returns a new UserCreator instance every time

PerRequest – Container.Resolve<UserCreator> returns the same UserCreator instance within a web request

Configuration can be done through XML – Read from the .config file. Bad practice, too limiting Code – Register all types through code

• Auto registration – Automatically register types based on conventions e.g. all types in assembly x

Page 38: Application Architecture April 2014

Where To Plugin

An IOC container should be created and initialized in the presentation layer or or above (DependencyResolution layer)

Any other layer should NOT have a reference to the IOC container

Presentation

Domain

Database, Web Service,Queue, SMTP server etc.

Infrastructure

Don’t reference container here

Don’t reference container here

Should reference and use container

Page 39: Application Architecture April 2014

Unity Example 1/3

Page 40: Application Architecture April 2014

Unity Example 2/3

Page 41: Application Architecture April 2014

Unity Example 3/3

Page 42: Application Architecture April 2014

Summary

Use an IOC Container to create type instances when using inversion of control

Only the presentation layer should know about the IOC ContainerConfiguration: Prefer code over xmlUse auto registration to reduce configuration code

Page 43: Application Architecture April 2014

Single Responsibility Principle

Single Responsibility Principle = SRP1 of the 5 SOLID principles ”A class or module should have one, and only one, reason to change”

A class should do one thing A class should have only one responsibility

Max 100-200 lines of code per class?The benefits

Easier to give the class a good name Easier to read Easier to maintain & extend Easier to test

Page 44: Application Architecture April 2014

The God Object

A God object is an object that knows too much or does too muchThe opposite of the SRP

Page 46: Application Architecture April 2014

Current responsibilities:• Facade class• Input validation• Talks to a database• Sends e-mail

Page 47: Application Architecture April 2014

Refactored To SRP

UserManager

CreateUser

GetUser

DeleteUser

Page 48: Application Architecture April 2014

Refactored UserManager

Responsibility• Facade class

Page 49: Application Architecture April 2014

Refactor GetUser & DeleteUser

Responsibility• Return the user from the

database

Responsibility• Delete the user from the

database

Page 50: Application Architecture April 2014

Refactored to CreateUser

Responsibility• Input validation• Create the user in the

database• Send confirmation e-mail

Page 51: Application Architecture April 2014

Future Refactoring

Split into multiple classes when the code increases in size

CreateUser

CreateUserDbCommand

SendEmailConfirmation

Page 52: Application Architecture April 2014

Summary

A class following the single responsibility principle is a class that does only one thing and has only one reason to change

The opposite of SRP is a God-objectBenefits

Easy to give the class a good name Less code per class means

• Reduced complexity = less errors

• Easier to maintain & extend

• Easier to test

Can use the Facade pattern if a special API is required

Page 53: Application Architecture April 2014

Code Duplication – Example #1

Duplicated code

Page 54: Application Architecture April 2014

Extract Method Principle - Refactored

Page 55: Application Architecture April 2014

Code Duplication – Example #2

Duplicated code

Page 56: Application Architecture April 2014

Execute Around Method Pattern - Refactored

Page 57: Application Architecture April 2014

Execute Around Method - Example #2

Page 58: Application Architecture April 2014

1-Layered Architecture

Presentation

Database, Web Service,Queue, SMTP server etc.

Page 59: Application Architecture April 2014

2-Layered Architecture

Presentation

Database, Web Service,Queue, SMTP server etc.

Infrastructure

Page 60: Application Architecture April 2014

3-Layered Architecture

Presentation

Domain Services

Database, Web Service,Queue, SMTP server etc.

Domain Model

Infrastructure

Page 61: Application Architecture April 2014

4-Layered Architecture

Presentation

Application Services

Domain Services

Database, Web Service,Queue, SMTP server etc.

Domain Model

Infrastructure

Page 62: Application Architecture April 2014

4-Layered Architecture – Dependency Inversion

Presentation

Application Services

Domain Services

Database, Web Service,Queue, SMTP server etc.

Domain Model

Infrastructure

Page 63: Application Architecture April 2014

Domain Model

Domain Services

Presentation

Application Services

Infrastructure

Frameworks and external endpoint integrations

Onion Architecture

A layer can only depend on inner layers

Frameworks and infrastructure concerns are pushed to the outer layer

A change of framework will not affect the application core

Application business rules= use cases

Enterprise business rules

Database, Web Service,Queue, SMTP server etc.

Application Core

Dep

ende

ncy

Res

olut

ion

Page 64: Application Architecture April 2014

The Use Case – Open New Account

Create new account in database and return account number

Is personal number empty?

Is the customer credit worthy?

Return error

No

Yes

Yes

No

Page 65: Application Architecture April 2014

Dependency Resolution

Domain Model

Domain Services

Presentation

Application Services

Infrastructure

AccountController

Database

Onion Architecture – Open New Account Use Case

• OpenNewAccountUseCase• ICreateAccountCommand

CreateAccount-DbCommand

• CreditCheckService• IGetTotalDebtForCustomerQuery

Web service

GetTotalDebtForCustomer-ServiceAgent

ExecuteUseCase

Page 66: Application Architecture April 2014

Process Flow

AccountController

OpenNewAccountUseCase

CreateAccountDbCommand

CreditCheckServiceGetTotalDebtForCustomer-

ServiceAgent

ExecuteUseCase

DependencyResolver Unity IOC Container

Database

Web service

• Transaction management• Exception handling• Logging

Insert/Update/Delete = *CommandRead = *Query

Page 67: Application Architecture April 2014
Page 68: Application Architecture April 2014
Page 69: Application Architecture April 2014
Page 70: Application Architecture April 2014
Page 71: Application Architecture April 2014
Page 72: Application Architecture April 2014
Page 73: Application Architecture April 2014

The Use Cases

Open new account

View accounts

Latest transactions

Single payment

Multiple payments

International payment

View Executed payments

View policies

Change address

Change payment limit

Page 74: Application Architecture April 2014

Group Into Components

Account•Open new account•View accounts•Latest transactions

Payments•Single payment•Multiple payments•International payment•View Executed payments

Insurance

•View policies

Settings•Change address•Change payment limit

Page 75: Application Architecture April 2014

Package By Layer

Application Services

• Account• Open new account

• OpenNewAccountUseCase• ICreateAccountCommand

Domain Services

• Account• Open new account

• CreditCheckService• IGetTotalDebtForCustomerQuery

...

Page 76: Application Architecture April 2014

Package By Component

Account

• Application Services• Open new account

• OpenNewAccountUseCase• ICreateAccountCommand

• Domain Services• Open new account

• CreditCheckService• IGetTotalDebtForCustomerQuery

• ...

Payments

• Application Services• ...

Page 77: Application Architecture April 2014

Package By Feature

Account

• Open new account• OpenNewAccountUseCase• ICreateAccountDbCommand• CreditCheckService• IGetTotalDebtForCustomerQuery

• ...

Payments

• ...

Page 78: Application Architecture April 2014

Change Request Comparison

Package by layer Must change code in multiple projects

Package by component Must change code in 1 project but multiple folders

Package by feature Must change code in 1 project, one folder

Page 79: Application Architecture April 2014

Event Sourcing

ExecuteUseCase: _log.UseCase(useCaseInput) generates a stream of executed use cases/events

A use case should either be a command or a query

Serialized data Execution date Status

OpenNewAccountInput 3. march Successful

ChangeAddressInput 4. march Successful

OpenNewAccountInput 7. march Successful

ChangePaymentLimitInput 10. march Failed

ChangePaymentLimitInput 11. march Successful

Page 80: Application Architecture April 2014

Replay Use Cases/Events

Stream of use cases/events can be replayed Test the system for errors Restore from failure

How to replay? Make an application that1. Delete all the data in the database

2. Read first event in log

3. Resolve use case class from use case input

4. Execute use case

5. Read next event and goto 23.

Page 81: Application Architecture April 2014

Summary

Use Inversion of Control to write testable and loosely coupled codeUse Single Responsibility Principle to write small, easy to maintain classesUse Extract Method Principle & Execute Around Method pattern to reduce

code duplicationUse the Onion Architecture or N-Layer to get a fixed architecturePackage by Layer-, Component- or Feature to get well organized codeLog executed use cases and replay them before a new release to check

for bugs

Page 82: Application Architecture April 2014

The information contained in this presentation is proprietary.© 2014 Capgemini. All rights reserved.

www.capgemini.com