Disadvantages of config injection

As we have seen, config injection can be used with both constructors and functions, It is, therefore, possible to build a system with only config injection. Unfortunately, config injection does have some disadvantages.

Passing config instead of abstract dependencies leaks implementation details—Consider the following code:

type PeopleFilterConfig interface {   DSN() string}func PeopleFilter(cfg PeopleFilterConfig, filter string) ([]Person, error) {   // load people   loader := &PersonLoader{}   people, err := loader.LoadAll(cfg)   if err != nil {      return nil, err   }   // filter people   out := []Person{}   for _, person := range people {      if strings.Contains(person.Name, filter) {         out = append(out, person)      }   }   return out,  ...

Get Hands-On Dependency Injection in Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.