Application Architecture April 2014
-
Upload
kindblad -
Category
Technology
-
view
418 -
download
2
description
Transcript of Application Architecture April 2014
![Page 1: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/1.jpg)
Application ArchitectureApril 2014
Lars-Erik KindbladSenior Consultant
& Head Of Microsoft Community Of Practice
![Page 2: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/3.jpg)
Ideal Feature Delivery
Project Progress
![Page 4: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/5.jpg)
Inversion Of Control
Make code loosely coupledMake unit testing possible
![Page 6: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/6.jpg)
Traditional Code
![Page 7: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/7.jpg)
Inversion Of Control
![Page 8: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/9.jpg)
Dependency Injection - Constructor Injection
![Page 10: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/10.jpg)
Dependency Injection - Interface Injection
![Page 11: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/11.jpg)
Dependency Injection - Setter Injection
![Page 12: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/12.jpg)
Static Service Locator
![Page 13: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/13.jpg)
Injected Service Locator
![Page 14: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/14.jpg)
![Page 15: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/15.jpg)
Which Patterns To Use
![Page 16: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/16.jpg)
Traditional Code
![Page 17: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/17.jpg)
Dependency Injection - Constructor Injection
![Page 18: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/18.jpg)
Dependency Injection - Interface Injection
![Page 19: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/19.jpg)
Dependency Injection - Setter Injection
![Page 20: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/20.jpg)
Static Service Locator
![Page 21: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/21.jpg)
Injected Service Locator
![Page 22: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/22.jpg)
When To Use Which Patterns
![Page 23: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/23.jpg)
Always Use Dependency Injection – Constructor Injection
...except
![Page 24: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/24.jpg)
Facade Classes
![Page 25: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/25.jpg)
Dependency Injection
![Page 26: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/26.jpg)
Service Locator
![Page 27: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/27.jpg)
Loops
![Page 28: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/28.jpg)
Dependency Injection
![Page 29: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/29.jpg)
Service Locator
![Page 30: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/30.jpg)
Base Classes
![Page 31: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/31.jpg)
Dependency Injection
![Page 32: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/32.jpg)
Service Locator
![Page 33: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/33.jpg)
Unknown Types At Compile Time
![Page 34: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/34.jpg)
Service Locator
![Page 35: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/35.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/36.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/37.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/38.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/39.jpg)
Unity Example 1/3
![Page 40: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/40.jpg)
Unity Example 2/3
![Page 41: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/41.jpg)
Unity Example 3/3
![Page 42: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/42.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/43.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/44.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/46.jpg)
Current responsibilities:• Facade class• Input validation• Talks to a database• Sends e-mail
![Page 47: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/47.jpg)
Refactored To SRP
UserManager
CreateUser
GetUser
DeleteUser
![Page 48: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/48.jpg)
Refactored UserManager
Responsibility• Facade class
![Page 49: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/49.jpg)
Refactor GetUser & DeleteUser
Responsibility• Return the user from the
database
Responsibility• Delete the user from the
database
![Page 50: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/50.jpg)
Refactored to CreateUser
Responsibility• Input validation• Create the user in the
database• Send confirmation e-mail
![Page 51: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/51.jpg)
Future Refactoring
Split into multiple classes when the code increases in size
CreateUser
CreateUserDbCommand
SendEmailConfirmation
![Page 52: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/52.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/53.jpg)
Code Duplication – Example #1
Duplicated code
![Page 54: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/54.jpg)
Extract Method Principle - Refactored
![Page 55: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/55.jpg)
Code Duplication – Example #2
Duplicated code
![Page 56: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/56.jpg)
Execute Around Method Pattern - Refactored
![Page 57: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/57.jpg)
Execute Around Method - Example #2
![Page 58: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/58.jpg)
1-Layered Architecture
Presentation
Database, Web Service,Queue, SMTP server etc.
![Page 59: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/59.jpg)
2-Layered Architecture
Presentation
Database, Web Service,Queue, SMTP server etc.
Infrastructure
![Page 60: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/60.jpg)
3-Layered Architecture
Presentation
Domain Services
Database, Web Service,Queue, SMTP server etc.
Domain Model
Infrastructure
![Page 61: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/61.jpg)
4-Layered Architecture
Presentation
Application Services
Domain Services
Database, Web Service,Queue, SMTP server etc.
Domain Model
Infrastructure
![Page 62: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/62.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/63.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/64.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/65.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/66.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/67.jpg)
![Page 68: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/68.jpg)
![Page 69: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/69.jpg)
![Page 70: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/70.jpg)
![Page 71: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/71.jpg)
![Page 72: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/72.jpg)
![Page 73: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/73.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/74.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/75.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/76.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/77.jpg)
Package By Feature
Account
• Open new account• OpenNewAccountUseCase• ICreateAccountDbCommand• CreditCheckService• IGetTotalDebtForCustomerQuery
• ...
Payments
• ...
![Page 78: Application Architecture April 2014](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/78.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/79.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/80.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/81.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061119/5462d7c3af79595f5c8b456b/html5/thumbnails/82.jpg)
The information contained in this presentation is proprietary.© 2014 Capgemini. All rights reserved.
www.capgemini.com