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)

Chapter 12, Reviewing Our Progress

1. What was the most important improvement made to our sample service?

This is subjective, and as such, there is no right answer. For me, it's either the decoupling or the removal of the globals. When the code becomes decoupled, it becomes easier for me test and each piece becomes a bite-sized chunk, which means it's easy to work on. Basically, I don't have to think too hard or remember too much context.

As to the globals, I've been bitten by this in the past, particularly the data races that happen during testing. I can't stand it when my tests are not reliable.

 

2. In our dependency graph, why isn't the data package under main?

We could refactor to make it this way, but at the moment we are using JIT injection between the model and data layers. This means the UX of the code is improved, but the dependency...