Home Page Icon
Home Page
Table of Contents for
Hands-On Dependency Injection in Go
Close
Hands-On Dependency Injection in Go
by Corey Scott
Hands-On Dependency Injection in Go
Title Page
Copyright and Credits
Hands-On Dependency Injection in Go
Dedication
About Packt
Why subscribe?
Packt.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Never Stop Aiming for Better
Technical requirements
Why does DI matter?
So, how do I define DI?
Code smells that indicate you might need DI
Code bloat
Resistance to change
Wasted effort
Tight coupling
Healthy skepticism
A quick word about idiomatic Go
Leave your baggage at the door
Summary
Questions
Further reading
SOLID Design Principles for Go
Technical requirements
Single responsibility principle (SRP)
How does this relate to DI?
What does this mean for Go?
Go interfaces, structs, and functions
Go packages
Open/closed principle (OCP)
How does this relate to DI?
What does this mean for Go?
Liskov substitution principle (LSP)
How does this relate to DI?
What does this mean for Go?
Interface segregation principle (ISP)
How does this relate to DI?
What does this mean for Go?
Dependency inversion principle (DIP)
How does this relate to DI?
What does this mean for Go?
Summary
Questions
Further reading
Coding for User Experience
Technical requirements
Optimizing for humans
What does user experience mean for Go code?
Start with simple – get complicated only when you must
Apply just enough abstraction
Follow industry, team, and language conventions
Export only what you must
Aggressively apply the single responsibility principle
Discovering a good user experience
Who is the user?
What are your users capable of?
Why do users want to use your code?
How do they expect to use it?
When to compromise
Final thoughts on coding for user experience
A security blanket named unit tests
So why do I write unit tests?
What should I test?
Table-driven tests
Stubs
Mocks
Test-induced damage
Warning signs of test-induced damage
Parameters, config options, or outputs that only exist because of tests
Parameters that cause or are caused by leaky abstractions
Publishing mocks in production code
Excessive test coverage
Visualizing your package dependencies with Godepgraph
Installing the tools
Generating a dependency graph
Interpreting the dependency graph
Summary
Questions
Introduction to the ACME Registration Service
Technical requirements
Goals for our system
High readability
High testability
Low coupling
Final thoughts on goals
Introduction to our system
Software architecture
Known issues
Testability
Duplication of effort
Lack of isolation in tests
High coupling between the data and REST packages
High coupling with the config package
Downstream currency service
Summary
Questions
Dependency Injection with Monkey Patching
Technical requirements
Monkey magic!
Advantages of monkey patching
Applying monkey patching
Introducing SQLMock
Monkey patching with SQLMock
Testing error handling
Reducing test bloat with table-driven tests
Monkey patching between packages
When the magic fades
Summary
Questions
Further reading
Dependency Injection with Constructor Injection
Technical requirements
Constructor injection
Addressing the duck in the room
Advantages of constructor injection
Applying constructor injection
Decoupling from the dependency
Building the constructor
Improving test scenario coverage
Validating our improvements with the dependency graph
Disadvantages of constructor injection
Summary
Questions
Dependency Injection with Method Injection
Technical requirements
Method injection
Advantages of method injection
Applying method injection
A quick recap
Stopping short
Applying method injection to the data package
Applying method injection to the exchange package
Applying method injection to the model layer (the Get, List, and Register packages)
Applying the method injection of context to the REST package
Latency budgets
Disadvantages of method injection
Summary
Questions
Dependency Injection by Config
Technical requirements
Config injection
Advantages of config injection
Applying config injection
Applying config injection to the model layer
Applying config injection to the data package
Applying config injection to the exchange package
Boundary tests
Disadvantages of config injection
Summary
Questions
Just-in-Time Dependency Injection
Technical requirements
JIT injection
Advantages of JIT injection
Applying JIT injection
Private dependencies
Unit test coverage
Coverage and dependency graph
Chasing away the monkeys
Optional public dependencies
Disadvantages of JIT injection
Summary
Questions
Off-the-Shelf Injection
Technical requirements
Off-the-shelf injection with Wire
Introducing providers
Understanding injectors
Adopting provider sets
Advantages of off-the-shelf injection
Applying off-the-shelf injection
Adopting Google Wire
API regression tests
Disadvantages of off-the-shelf injection
Summary
Questions
Curb Your Enthusiasm
Technical requirements
DI induced damage
A long constructor parameter list
Injecting an object when config would do
Needless indirection
Service locator
Premature future-proofing
Mocking HTTP requests
Mocking an HTTP request with DI
Mocking HTTP requests with config
Unnecessary injection
Summary
Questions
Reviewing Our Progress
Technical requirements
Overview of the improvements
Global singletons
High coupling with the config package
Removing the dependence on upstream service
Stopping short and latency budgets
Simplified dependency creation
Coupling and extensibility
Review of the dependency graph
Review of test coverage and testability
Test coverage
Starting a new service with DI
The user experience
Code structure
Cross-cutting concerns
Designing from outside-in
Summary
Questions
Assessment
Chapter 1, Never Stop Aiming for Better
Chapter 2, SOLID Design Principles for Go
Chapter 3, Coding for User Experience
Chapter 4, Introduction to the ACME Registration Service
Chapter 5, Dependency Injection with Monkey Patching
Chapter 6, Dependency Injection with Constructor Injection
Chapter 7, Dependency Injection with Method Injection
Chapter 8, Dependency Injection by Config
Chapter 9, Just-in-Time Dependency Injection
Chapter 10, Off-the-Shelf Injection
Chapter 11, Curbing Your Enthusiasm
Chapter 12, Reviewing Our Progress
Other Books You May Enjoy
Leave a review - let other readers know what you think
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
Title Page
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset