SlideShare a Scribd company logo
1 of 21
Clean Architecture
Architecture driven by intent
Why?
2
Why?
• Independent of Frameworks
• Testable
• Independent of UI
• Independent of Database
3 https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
Communicate
intent through
source code
4
How?
5
How?
Separation of concerns
6
7 https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
The Dependency Rule
"...source code
dependencies can only
point inwards."
8 https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
Show me the code!
At least some of it.
9
This user knows too much
class User < ActiveRecord::Base
# ...
def password_required?
# Password is required if it is being set, but not for new records
if !persisted?
false
else
!password.nil? || !password_confirmation.nil?
end
end
end
10
https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation
Interactors for the rescue
class EmailSignUp < Interactor
def call(request, response)
# ... validate data without password
# ... application rules through domain objects
# ... build response
end
end
class PasswordSignUp < Interactor
def call(request, response)
# ... validate data, password included
# ... application rules through domain objects
# ... build response
end
end
11
What is this?
def update
with_unconfirmed_confirmable do
if @confirmable.has_no_password?
@confirmable.attempt_set_password(params[:user])
if @confirmable.valid? and @confirmable.password_match?
do_confirm
else
do_show
@confirmable.errors.clear #so that we wont render :new
end
else
@confirmable.errors.add(:email, :password_already_set)
end
end
if !@confirmable.errors.empty?
self.resource = @confirmable
render 'devise/confirmations/new' #Change this if you don't have the
views on default path
end
end
12
https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation
Cleaning it up!
class Controller
def update
@presenter = build_response
interactor.call(user_params, @presenter)
if @presenter.valid?
redirect_to dashboard_path
else
render :new
end
end
end
class SetPassword < Interactor
def call(request, response)
# ... validate data and fetch user
user.set_password(request[:password])
# ... save user and other application rules
end
end
class User
def set_password(new_password)
raise PasswordAlreadySetError unless encrypted_password.blank?
self.encrypted_password = encrypt(new_password)
end
end
13
Business Object
class User
def set_password(new_password)
raise PasswordAlreadySetError unless encrypted_password.blank?
self.encrypted_password = encrypt(new_password)
end
end
14
Use case
class SetPassword < Interactor
def call(request, response)
# ... validate data and fetch user
user.set_password(request[:password])
# ... save user and other application rules
end
end
15
Delivery mechanism
class Controller
def update
@presenter = build_response
interactor.call(user_params, @presenter)
if @presenter.valid?
redirect_to dashboard_path
else
render :new
end
end
end
Interface adapter
16
Wrap up
17
Wrap up
• Your application should be framework agnostic
• Your application should be infrastructure agnostic
• Web is a delivery mechanism (so is a rake task, the
console)
• Testing should be fast
18
And what about
Hanami?
19
And what about Hanami?
• Web delivery: Web::Controllers
• Use cases: Hanami::Interactor
• Infrastructure: Hanami::Repository
• Business objects: Hanami::Entity
20
Thank you very much!
Questions?
21

More Related Content

Viewers also liked

Practical Diversity at Thinking Digital Women
Practical Diversity at Thinking Digital Women Practical Diversity at Thinking Digital Women
Practical Diversity at Thinking Digital Women Meri Williams
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean ArchitectureBadoo
 
Awesome People Management with Agile at Agile North East
Awesome People Management with Agile at Agile North EastAwesome People Management with Agile at Agile North East
Awesome People Management with Agile at Agile North EastMeri Williams
 
Brilliant People Management in an Agile Setting
Brilliant People Management in an Agile SettingBrilliant People Management in an Agile Setting
Brilliant People Management in an Agile SettingMeri Williams
 
Clean architecture
Clean architectureClean architecture
Clean architectureandbed
 
Creating Space to Be Awesome at QCon London
Creating Space to Be Awesome at QCon LondonCreating Space to Be Awesome at QCon London
Creating Space to Be Awesome at QCon LondonMeri Williams
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean ArchitectureMattia Battiston
 
Metriche Kanban in pratica a Sky UK [ITA]
Metriche Kanban in pratica a Sky UK [ITA]Metriche Kanban in pratica a Sky UK [ITA]
Metriche Kanban in pratica a Sky UK [ITA]Mattia Battiston
 
Kanban Metrics in practice for leading Continuous Improvement
Kanban Metrics in practice for leading Continuous ImprovementKanban Metrics in practice for leading Continuous Improvement
Kanban Metrics in practice for leading Continuous ImprovementMattia Battiston
 

Viewers also liked (9)

Practical Diversity at Thinking Digital Women
Practical Diversity at Thinking Digital Women Practical Diversity at Thinking Digital Women
Practical Diversity at Thinking Digital Women
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Awesome People Management with Agile at Agile North East
Awesome People Management with Agile at Agile North EastAwesome People Management with Agile at Agile North East
Awesome People Management with Agile at Agile North East
 
Brilliant People Management in an Agile Setting
Brilliant People Management in an Agile SettingBrilliant People Management in an Agile Setting
Brilliant People Management in an Agile Setting
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Creating Space to Be Awesome at QCon London
Creating Space to Be Awesome at QCon LondonCreating Space to Be Awesome at QCon London
Creating Space to Be Awesome at QCon London
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 
Metriche Kanban in pratica a Sky UK [ITA]
Metriche Kanban in pratica a Sky UK [ITA]Metriche Kanban in pratica a Sky UK [ITA]
Metriche Kanban in pratica a Sky UK [ITA]
 
Kanban Metrics in practice for leading Continuous Improvement
Kanban Metrics in practice for leading Continuous ImprovementKanban Metrics in practice for leading Continuous Improvement
Kanban Metrics in practice for leading Continuous Improvement
 

Recently uploaded

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 

Recently uploaded (20)

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

Clean Architecture @ 1º Hanami Sao Paulo meetup

  • 3. Why? • Independent of Frameworks • Testable • Independent of UI • Independent of Database 3 https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
  • 8. The Dependency Rule "...source code dependencies can only point inwards." 8 https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
  • 9. Show me the code! At least some of it. 9
  • 10. This user knows too much class User < ActiveRecord::Base # ... def password_required? # Password is required if it is being set, but not for new records if !persisted? false else !password.nil? || !password_confirmation.nil? end end end 10 https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation
  • 11. Interactors for the rescue class EmailSignUp < Interactor def call(request, response) # ... validate data without password # ... application rules through domain objects # ... build response end end class PasswordSignUp < Interactor def call(request, response) # ... validate data, password included # ... application rules through domain objects # ... build response end end 11
  • 12. What is this? def update with_unconfirmed_confirmable do if @confirmable.has_no_password? @confirmable.attempt_set_password(params[:user]) if @confirmable.valid? and @confirmable.password_match? do_confirm else do_show @confirmable.errors.clear #so that we wont render :new end else @confirmable.errors.add(:email, :password_already_set) end end if !@confirmable.errors.empty? self.resource = @confirmable render 'devise/confirmations/new' #Change this if you don't have the views on default path end end 12 https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation
  • 13. Cleaning it up! class Controller def update @presenter = build_response interactor.call(user_params, @presenter) if @presenter.valid? redirect_to dashboard_path else render :new end end end class SetPassword < Interactor def call(request, response) # ... validate data and fetch user user.set_password(request[:password]) # ... save user and other application rules end end class User def set_password(new_password) raise PasswordAlreadySetError unless encrypted_password.blank? self.encrypted_password = encrypt(new_password) end end 13
  • 14. Business Object class User def set_password(new_password) raise PasswordAlreadySetError unless encrypted_password.blank? self.encrypted_password = encrypt(new_password) end end 14
  • 15. Use case class SetPassword < Interactor def call(request, response) # ... validate data and fetch user user.set_password(request[:password]) # ... save user and other application rules end end 15
  • 16. Delivery mechanism class Controller def update @presenter = build_response interactor.call(user_params, @presenter) if @presenter.valid? redirect_to dashboard_path else render :new end end end Interface adapter 16
  • 18. Wrap up • Your application should be framework agnostic • Your application should be infrastructure agnostic • Web is a delivery mechanism (so is a rake task, the console) • Testing should be fast 18
  • 20. And what about Hanami? • Web delivery: Web::Controllers • Use cases: Hanami::Interactor • Infrastructure: Hanami::Repository • Business objects: Hanami::Entity 20
  • 21. Thank you very much! Questions? 21

Editor's Notes

  1. Frameworks are tools, focus on biz rules Test without UI, Databases ... UI can change, biz rule stays Biz rules are not bound to database
  2. Separated by layers
  3. No FRAMEWORK
  4. No FRAMEWORK
  5. FRAMEWORK
  6. A modular framework that you can choose what to use.