▶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
▶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
▶Who This Book Is For
This book is designed for developers who wish that their code was easy to read, test, and maintain. It is intended for developers coming from an object-oriented background who want to get more out of Go, as well as for developers who believe that quality code is about more than delivering one particular feature.
After all, writing code is easy. Similarly, getting a single test case to pass is simple. Creating code whose tests continue to pass after months or years of adding additional features is heading toward the impossible.
For us to be able to deliver code at that level consistently, we require a lot of nifty tricks. This book hopes to not only equip you with those tricks, but also to give you the wisdom to apply them effectively.
▶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 6, Dependency 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 7, Dependency 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 8, Dependency 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.