Clean Architecture by Andrzej Bednarz

33
@bednarz_andrzej #DevoxxPL Platinum Sponsors: Clean architecture Andrzej Bednarz

Transcript of Clean Architecture by Andrzej Bednarz

Page 1: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Platinum Sponsors:

Clean architecture

Andrzej Bednarz

Page 2: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

About me

Img src: https://crisismapper.wordpress.com/2012/08/20/agile-development-manifesto-for-ict4d-projects/

Page 3: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

A new project

Image courtesy of adamr at FreeDigitalPhotos.net

Page 4: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Main guidelines

Image src: http://www.dlr.de/en/desktopdefault.aspx/tabid-78/7420_read-14264/

Fast development No fear to change

Image courtesy of dan at FreeDigitalPhotos.net

Page 5: Clean Architecture by Andrzej Bednarz
Page 6: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Clean architecture – principles by UB• The only way to go

fast is to go well

• Architecture is not about tools or frameworks instead it screams intended usage

• It allows to defer/change decisions about UI,

DB, DI, frameworks

• It’s decoupled - aka Hexagonal Architecture, Plugin Architecture

• It’s testable (TDD)

• See Uncle Bob’s presentation online

Page 7: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Screaming architecture

Top level visibility:modules & functionalitiesframeworks and tools e.g. Spring MVC,

Akka

Img src: https://www.flickr.com/photos/juhansonin/2147259806

Page 8: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Demo

Page 9: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Clean architecture – schema

Page 10: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Application core

Source: Uncle Bob, http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html

Page 11: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Interactor = use case

Source: Uncle Bob, http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html

Page 12: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

How do we call an interactor?

Source: Uncle Bob, http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html

Page 13: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

How do we return results?

Source: Uncle Bob, http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html

Page 14: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

How do we call infrastructure services?

Source: Uncle Bob, http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html

Page 15: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

All pieces together

Source: Uncle Bob, http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html

Page 16: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Demo

Page 17: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Layers?

Page 18: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Traditional layered architecture

Page 19: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Layers in clean architecture

Page 20: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Source: Uncle Bob, http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html

Page 21: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Summary & discussion

Page 22: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

• Lots of additional code: classes and interfaces

• DTOs and conversions

• Cost ...

The Dark Side

Img src: Uncle Bob, http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html

Page 23: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Cost – example with Rails controller

Source: David Heinemeier Hansson - DHH, https://gist.github.com/dhh/4849a20d2ba89b34b201Based on: Jim Weirich's "Decoupling from Rails"

Page 24: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Cost – example with Rails controller

Source: David Heinemeier Hansson - DHH, https://gist.github.com/dhh/4849a20d2ba89b34b201Based on: Jim Weirich's "Decoupling from Rails"

Page 25: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

• „Coding an app in Rails is always fun...

for the first 3 months”

• „After some time, small, little things show up. Thin controllers are not really so thin anymore. The tests are no longer below 1 minute.”

Troubles with the Rails way

Source: Andrzej Krzywda, http://rails-refactoring.com/, http://blog.jetbrains.com/ruby/2015/03/interview-with-andrzej-krzywda/

Page 26: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Excellent testability• Every component

and layer could be tested in isolation.

• Tests are fast and precise.

Clean arch – good sides

No fear to change

Page 27: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Easy to understand• Explicit by design

• Clearly defined separation of concerns

• Full control of frameworks and tools

Clean arch – good sides

FAST development

HighMaintainability

&Flexibility

with tests

Page 28: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

• Our way• model mapper, generic

classes, lombok

• Uncle Bob’s suggestion• Another (isolated)

boundary for DTO

• Greg Young’s

suggestion• CQRS

But DTOs and conversions - still pain

Img src: https://www.microsoftpressstore.com/articles/article.aspx?p=2248809

Page 29: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Think, what is fast and clean for you

Img src: https://thesunnyseason.files.wordpress.com/2015/01/closet-before-and-after-1024x781.jpg

Page 30: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Not new

Not only Java related

Not rocket science

Not home-grown

Page 31: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

• http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html

• http://blog.8thlight.com/uncle-bob/2011/11/22/Clean-Architecture.html

• http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html

• http://alistair.cockburn.us/Hexagonal+architecture

• http://www.growing-object-oriented-software.com/

• http://jeffreypalermo.com/blog/the-onion-architecture-part-1/

• http://martinfowler.com/articles/is-tdd-dead/

• https://groups.google.com/forum/#!forum/clean-code-discussion

• http://en.wikipedia.org/wiki/Data,_context_and_interaction

• http://andrzejonsoftware.blogspot.com/2013/12/the-four-architectures-that-will.html

• Object Oriented Software Engineering: A Use Case Driven Approach, Ivar Jacobson, 1992

References

Page 33: Clean Architecture by Andrzej Bednarz

@bednarz_andrzej#DevoxxPL

Thank you!

Questions?