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 11, Curbing Your Enthusiasm

1. What form of dependency injection-induced damage do you see most often?

For me, this is absolutely excessive parameters. After learning dependency injection and getting excited about it, it's easy to want to abstract and inject everything. It tends to make testing a lot easier as the responsibility of each object decreases. The downside is a lot of objects and too much injection.

If I find myself with too many dependencies, I will try to step back and check my object design, looking for single responsibility principle issues in particular.

 

2. Why is it important not to blindly apply dependency injection all the time?

Just because something is cool or new, doesn't mean it's the best tool for the job. We should always strive to fix the solution to the problem and avoid cargo cult programming when we can...