Book Image

Hands-On Dependency Injection in Go

By : Corey Scott
Book Image

Hands-On Dependency Injection in Go

By: Corey Scott

Overview of this book

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.
Table of Contents (15 chapters)

What this book covers

Chapter 1, Never Stop Aiming for Better, aims to define dependency injection, outline why dependency injection is important for Go development, and introduce several code smells that may be addressed with dependency injection.

Chapter 2, SOLID Design Principles for Go, introduces the SOLID software design principles and how they relate to both dependency injection and programming in Go.

Chapter 3, Coding for User Experience, addresses often overlooked concepts in programming, namely testing and the code's user experience. It also introduces many other concepts, including mocks, stubs, test-induced damage and the dependency graph, that we will use throughout the book.

Chapter 4, Introduction to the ACME Registration Service, introduces a small, fake service that forms the basis for many of our examples in later chapters. It highlights the issues with the service's current implementation and outlines the goals we are hoping to achieve by applying dependency injection.

Chapter 5, Dependency Injection with Monkey Patching, examines monkey patching as a way to swap out dependencies during our tests. This chapter applies monkey patching to our sample service to decouple our tests from the database, and to decouple the different layers from each other, all without resorting to significant refactoring.

Chapter 6Dependency Injection with Constructor Injection, introduces perhaps the most traditional form of dependency injection – constructor injection. This chapter will examine its many advantages, its disadvantages, and show how to successfully apply constructor injection.

Chapter 7Dependency Injection with Method Injection, introduces the second most common form of dependency injection – method injection. This chapter discusses the advantages and disadvantages of method injection and shows how to successfully apply the method for request-scoped dependencies.

Chapter 8Dependency Injection by Config, introduces config injection. Config injection is an extension of constructor and method injection that intends to improve the usability of the code by reducing the number of parameters.

 Chapter 9, Just-in-Time Dependency Injection, discusses another unusual form of dependency injection – just-in-time injection. Just-in-time (JIT) injection is a strategy that gives us many of the benefits of dependency injection, such as decoupling and testability, without adding parameters to our constructors or methods.

Chapter 10, Off-the-Shelf Injection, introduces the final dependency injection method – dependency injection using a framework. This chapter outlines the advantages and disadvantages related to adopting a dependency injection framework and also introduces and applies Google Go Cloud's wire framework to our sample service.

Chapter 11, Curb Your Enthusiasm, examines some of the ways in which dependency injection can go wrong. It offers many examples where applying dependency injection is either unnecessary or detrimental to the code.

Chapter 12, Reviewing Our Progress, contrasts the state of our sample service after applying dependency injection with the state it was in when it was introduced. It also discusses the steps we could have taken if we were starting a new service with dependency injection.