Hands-On Dependency Injection in Go

Book description

Explore various dependency injection methods in Go such as monkey patching, constructor injection, and method injection

Key Features

  • Learn to evaluate Code UX and make it better
  • Explore SOLID principles and understand how they relate to dependency injection
  • Use Google's wire framework to simplify dependence management

Book Description

Hands-On Dependency Injection in Go takes you on a journey, teaching you about refactoring existing code to adopt dependency injection (DI) using various methods available in Go.

Of the six methods introduced in this book, some are conventional, such as constructor or method injection, and some unconventional, such as just-in-time or config injection. Each method is explained in detail, focusing on their strengths and weaknesses, and is followed with a step-by-step example of how to apply it. With plenty of examples, you will learn how to leverage DI to transform code into something simple and flexible. You will also discover how to generate and leverage the dependency graph to spot and eliminate issues. Throughout the book, you will learn to leverage DI in combination with test stubs and mocks to test otherwise tricky or impossible scenarios.

Hands-On Dependency Injection in Go takes a pragmatic approach and focuses heavily on the code, user experience, and how to achieve long-term benefits through incremental changes.

By the end of this book, you will have produced clean code that's easy to test.

What you will learn

  • Understand the benefits of DI
  • Explore SOLID design principles and how they relate to Go
  • Analyze various dependency injection patterns available in Go
  • Leverage DI to produce high-quality, loosely coupled Go code
  • Refactor existing Go code to adopt DI
  • Discover tools to improve your code's testability and test coverage
  • Generate and interpret Go dependency graphs

Who this book is for

Hands-On Dependency Injection in Go is for programmers with a few year s experience in any language and a basic understanding of Go. If you wish to produce clean, loosely coupled code that is inherently easier to test, this book is for you.

Table of contents

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

Product information

  • Title: Hands-On Dependency Injection in Go
  • Author(s): Corey Scott
  • Release date: November 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789132762