SlideShare a Scribd company logo
1 of 14
Download to read offline
Services, Dependencies, and
You
A guide to writing maintainable code in Go
(and other languages, really)
Sean Kelly
@StabbyCutyou
Obligatory "Who am I" slide
● Sean Kelly (Hi!)
○ But everyone calls me Stabby
● I work for Tapjoy
○ We serve hundreds of millions of ads a day on a global scale
● I have a new puppy and a new kitty
○ Some of these slides might accidentally veer into tips on Crate Training
● I maintain a bunch of open source libraries
○ That literally nobody uses
○ It's actually kind of fun, since I can break things with less repurcussions
What am I here to talk about?
● Service Oriented Architecture
● Defining Dependencies
● Separation of Concerns
● Practical example of applying the above in a Go webservice
Service Oriented Architecture
You mean like, microservices and stuff?
(Spoiler Alert: Not really)
Service Oriented Architecture
● Many ways to define and apply this
● Internally architecting your application for SOA is a great pattern for maintaining
application code
● Keep crucial business logic out of Models and Controllers
○ Helpers like combining First + Last into FullName can live on a Model
○ Try to avoid ivory-towering yourself into code no one wants to maintain
● Centralize it in reusable Service objects
● These objects define domain boundaries in your systems
● These objects also define dependency maps between the parts of your systems
● Eventually, if you do break your app up into microservices, internal SOA has
already got your code in a "ready to extract" state
Dependencies
Ugh, is he gonna tell us to use some interface{} abusing DI
framework?
(Spoiler Alert: Nah)
Dependencies
● External resources you rely upon to execute code
● Common examples:
○ MySQL
○ Memcached
○ Riak
● Less Common examples:
○ Configuration data
○ Other code modules
● "Hmm, I need access to X here…"
○ That is probably a dependency
● Passing dependencies down via a constructor makes for more testable, reliable
code.
● Avoid "new-ing up" a dependency unless it's in your main() and handed to others
Separation of Concerns
Is that like when your code isn't a jumbled mess?
(Spoiler Alert: Yes!)
Separation of Concerns
● Modules of code should have 1 purpose
● Don't lump things together out of convenience
● Each distinct piece of your domain should get it's own Service object
● You can compose Service objects to create "Higher Order" Services
○ If Service A needs Service B, but Service B needs Service A…
○ You need Service C, composed of A and B
● So, if you had some basic Services like these:
○ UserService
○ ErrorService
○ ThirdPartyMailerService
● You might compose some Services like these:
○ AccountManagementService(UserService, ThirdPartyMailerService)
○ AlertingService(AccountService, ErrorService, ThirdPartyMailerService)
Testing
Ugh, Booooriiiing
(I know, but these designs will make testing easier)
Testing
● Service Objects with their dependencies declared in their New method: 80% of
the way there
● The other 20%: Interfaces!
● If instead of this…
○ NewAlertingService(*AccountService, *ErrorService, *SomeEmailProvider) *AlertingService
● You did this…
○ NewAlertingService(IAccountService, IErrorService, IEmailService) IAlerteringService
● You could replace any of the critical dependencies in a unit test with mocks or no-
op implementations
● You can now use tools like `gomock` to generate test implementations for you
○ Or, just do it yourself if your services are small / have specialized testing needs
● Conversely, you could mock out the dependencies of those Services
○ But usually, whole-mocking is easier and more straightforward
Wrapping Up
Wait, already?
(The rest of the talk is some code examples)
Services, Dependencies, and You
● Primary focus is to keep code "clean" and DRY
● Each service does one thing, and does it well
● They define dependencies in the constructor / New method
● Create "Higher Order" Services by combining existing Services
● More easily testable with both unit and integration tests
○ Especially if you use Interfaces
● Lays the groundwork for extraction into microservices, should you choose to
○ If the code is cleanly separated, and has well defined dependencies, it's more straightforward to port
● And Now… Sample code!
Thanks!
Services, Dependencies, and
You
A guide to writing maintainable code in Go
(and other languages, really)
Sean Kelly
@StabbyCutyou

More Related Content

What's hot

What's hot (19)

Microservices - opportunities, dilemmas and problems
Microservices - opportunities, dilemmas and problemsMicroservices - opportunities, dilemmas and problems
Microservices - opportunities, dilemmas and problems
 
Php : Why and When!
Php : Why and When!Php : Why and When!
Php : Why and When!
 
Green Custard Friday Talk 5: React-Native Performance
Green Custard Friday Talk 5: React-Native PerformanceGreen Custard Friday Talk 5: React-Native Performance
Green Custard Friday Talk 5: React-Native Performance
 
Share the insight of ServiceInsight
Share the insight of ServiceInsightShare the insight of ServiceInsight
Share the insight of ServiceInsight
 
Document-Driven transactions
Document-Driven transactionsDocument-Driven transactions
Document-Driven transactions
 
The Economics of Microservices (2017 CraftConf)
The Economics of Microservices  (2017 CraftConf)The Economics of Microservices  (2017 CraftConf)
The Economics of Microservices (2017 CraftConf)
 
The Next Generation of Microservices
The Next Generation of MicroservicesThe Next Generation of Microservices
The Next Generation of Microservices
 
Architecture of web servers
Architecture of web serversArchitecture of web servers
Architecture of web servers
 
The Next Generation of Microservices — YOW 2017 Brisbane
The Next Generation of Microservices — YOW 2017 BrisbaneThe Next Generation of Microservices — YOW 2017 Brisbane
The Next Generation of Microservices — YOW 2017 Brisbane
 
Advanced web application architecture Way2Web
Advanced web application architecture Way2WebAdvanced web application architecture Way2Web
Advanced web application architecture Way2Web
 
Dynamo Amazon’s Highly Available Key-value Store
Dynamo Amazon’s Highly Available Key-value StoreDynamo Amazon’s Highly Available Key-value Store
Dynamo Amazon’s Highly Available Key-value Store
 
Kong Ingress Controller - Fullstaq Show N Tell
Kong Ingress Controller - Fullstaq Show N TellKong Ingress Controller - Fullstaq Show N Tell
Kong Ingress Controller - Fullstaq Show N Tell
 
NATS: A Central Nervous System for IoT Messaging - Larry McQueary
NATS: A Central Nervous System for IoT Messaging - Larry McQuearyNATS: A Central Nervous System for IoT Messaging - Larry McQueary
NATS: A Central Nervous System for IoT Messaging - Larry McQueary
 
Introduction to Reactjs
Introduction to ReactjsIntroduction to Reactjs
Introduction to Reactjs
 
Introduction to MDC Logging in Scala.pdf
Introduction to MDC Logging in Scala.pdfIntroduction to MDC Logging in Scala.pdf
Introduction to MDC Logging in Scala.pdf
 
OSMC 2019 | How to improve database Observability by Charles Judith
OSMC 2019 | How to improve database Observability by Charles JudithOSMC 2019 | How to improve database Observability by Charles Judith
OSMC 2019 | How to improve database Observability by Charles Judith
 
How blockchain could give us a cleaner grid
How blockchain could give us a cleaner gridHow blockchain could give us a cleaner grid
How blockchain could give us a cleaner grid
 
Blockchain technology for the grid
Blockchain technology for the gridBlockchain technology for the grid
Blockchain technology for the grid
 
Reactive Principles and Microservices
Reactive Principles and MicroservicesReactive Principles and Microservices
Reactive Principles and Microservices
 

Similar to Services, dependencies, and you

Similar to Services, dependencies, and you (20)

The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор Турский
 
The working architecture of node js applications open tech week javascript ...
The working architecture of node js applications   open tech week javascript ...The working architecture of node js applications   open tech week javascript ...
The working architecture of node js applications open tech week javascript ...
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...
 
Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"
 
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...
Laptop Devops: Putting Modern Infrastructure Automation to Work For Local Dev...
 
Create first android app with MVVM Architecture
Create first android app with MVVM ArchitectureCreate first android app with MVVM Architecture
Create first android app with MVVM Architecture
 
Keeping business logic out of your UIs
Keeping business logic out of your UIsKeeping business logic out of your UIs
Keeping business logic out of your UIs
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO Design
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
IBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New TricksIBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New Tricks
 
Intro to ember.js
Intro to ember.jsIntro to ember.js
Intro to ember.js
 
"The working architecture of NodeJs applications" Viktor Turskyi
"The working architecture of NodeJs applications" Viktor Turskyi"The working architecture of NodeJs applications" Viktor Turskyi
"The working architecture of NodeJs applications" Viktor Turskyi
 
Building Microservices with .NET (speaker Anton Vasilenko, Binary Studio)
Building Microservices with .NET (speaker Anton Vasilenko, Binary Studio)Building Microservices with .NET (speaker Anton Vasilenko, Binary Studio)
Building Microservices with .NET (speaker Anton Vasilenko, Binary Studio)
 
Evolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoEvolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand Rao
 
Dust.js
Dust.jsDust.js
Dust.js
 
Structuring the code
Structuring the codeStructuring the code
Structuring the code
 
Itroducing Angular JS
Itroducing Angular JSItroducing Angular JS
Itroducing Angular JS
 
Serverless - DevOps Lessons Learned From Production
Serverless - DevOps Lessons Learned From ProductionServerless - DevOps Lessons Learned From Production
Serverless - DevOps Lessons Learned From Production
 
Drupal 8 Vocab Lesson
Drupal 8 Vocab LessonDrupal 8 Vocab Lesson
Drupal 8 Vocab Lesson
 
From prototype to production - The journey of re-designing SmartUp.io
From prototype to production - The journey of re-designing SmartUp.ioFrom prototype to production - The journey of re-designing SmartUp.io
From prototype to production - The journey of re-designing SmartUp.io
 

More from Sean Kelly

More from Sean Kelly (6)

Go testunderthehood
Go testunderthehoodGo testunderthehood
Go testunderthehood
 
Broadcasting
BroadcastingBroadcasting
Broadcasting
 
Embedding: It sure is weird
Embedding: It sure is weirdEmbedding: It sure is weird
Embedding: It sure is weird
 
Comments: Why not What
Comments: Why not WhatComments: Why not What
Comments: Why not What
 
Don't Fear Failure
Don't Fear FailureDon't Fear Failure
Don't Fear Failure
 
Messaging
MessagingMessaging
Messaging
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Services, dependencies, and you

  • 1. Services, Dependencies, and You A guide to writing maintainable code in Go (and other languages, really) Sean Kelly @StabbyCutyou
  • 2. Obligatory "Who am I" slide ● Sean Kelly (Hi!) ○ But everyone calls me Stabby ● I work for Tapjoy ○ We serve hundreds of millions of ads a day on a global scale ● I have a new puppy and a new kitty ○ Some of these slides might accidentally veer into tips on Crate Training ● I maintain a bunch of open source libraries ○ That literally nobody uses ○ It's actually kind of fun, since I can break things with less repurcussions
  • 3. What am I here to talk about? ● Service Oriented Architecture ● Defining Dependencies ● Separation of Concerns ● Practical example of applying the above in a Go webservice
  • 4. Service Oriented Architecture You mean like, microservices and stuff? (Spoiler Alert: Not really)
  • 5. Service Oriented Architecture ● Many ways to define and apply this ● Internally architecting your application for SOA is a great pattern for maintaining application code ● Keep crucial business logic out of Models and Controllers ○ Helpers like combining First + Last into FullName can live on a Model ○ Try to avoid ivory-towering yourself into code no one wants to maintain ● Centralize it in reusable Service objects ● These objects define domain boundaries in your systems ● These objects also define dependency maps between the parts of your systems ● Eventually, if you do break your app up into microservices, internal SOA has already got your code in a "ready to extract" state
  • 6. Dependencies Ugh, is he gonna tell us to use some interface{} abusing DI framework? (Spoiler Alert: Nah)
  • 7. Dependencies ● External resources you rely upon to execute code ● Common examples: ○ MySQL ○ Memcached ○ Riak ● Less Common examples: ○ Configuration data ○ Other code modules ● "Hmm, I need access to X here…" ○ That is probably a dependency ● Passing dependencies down via a constructor makes for more testable, reliable code. ● Avoid "new-ing up" a dependency unless it's in your main() and handed to others
  • 8. Separation of Concerns Is that like when your code isn't a jumbled mess? (Spoiler Alert: Yes!)
  • 9. Separation of Concerns ● Modules of code should have 1 purpose ● Don't lump things together out of convenience ● Each distinct piece of your domain should get it's own Service object ● You can compose Service objects to create "Higher Order" Services ○ If Service A needs Service B, but Service B needs Service A… ○ You need Service C, composed of A and B ● So, if you had some basic Services like these: ○ UserService ○ ErrorService ○ ThirdPartyMailerService ● You might compose some Services like these: ○ AccountManagementService(UserService, ThirdPartyMailerService) ○ AlertingService(AccountService, ErrorService, ThirdPartyMailerService)
  • 10. Testing Ugh, Booooriiiing (I know, but these designs will make testing easier)
  • 11. Testing ● Service Objects with their dependencies declared in their New method: 80% of the way there ● The other 20%: Interfaces! ● If instead of this… ○ NewAlertingService(*AccountService, *ErrorService, *SomeEmailProvider) *AlertingService ● You did this… ○ NewAlertingService(IAccountService, IErrorService, IEmailService) IAlerteringService ● You could replace any of the critical dependencies in a unit test with mocks or no- op implementations ● You can now use tools like `gomock` to generate test implementations for you ○ Or, just do it yourself if your services are small / have specialized testing needs ● Conversely, you could mock out the dependencies of those Services ○ But usually, whole-mocking is easier and more straightforward
  • 12. Wrapping Up Wait, already? (The rest of the talk is some code examples)
  • 13. Services, Dependencies, and You ● Primary focus is to keep code "clean" and DRY ● Each service does one thing, and does it well ● They define dependencies in the constructor / New method ● Create "Higher Order" Services by combining existing Services ● More easily testable with both unit and integration tests ○ Especially if you use Interfaces ● Lays the groundwork for extraction into microservices, should you choose to ○ If the code is cleanly separated, and has well defined dependencies, it's more straightforward to port ● And Now… Sample code!
  • 14. Thanks! Services, Dependencies, and You A guide to writing maintainable code in Go (and other languages, really) Sean Kelly @StabbyCutyou