Last active
July 17, 2022 02:05
-
-
Save jay-babu/f9d14ba3ce9bf1414b987dbcda45029f to your computer and use it in GitHub Desktop.
Field Vs. Constructor Injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
) | |
type FieldInjection struct { | |
foo http.Server | |
bar http.Server | |
} | |
func NewFieldInjection() FieldInjection { | |
return FieldInjection{ | |
foo: http.Server{}, | |
bar: http.Server{}, | |
} | |
} | |
// client using above struct | |
func client() { | |
// client can't pass in their own server | |
_ = NewFieldInjection() | |
} | |
type ConstructorInjection struct { | |
Foo http.Server | |
Bar http.Server | |
} | |
// client using above struct | |
func client() { | |
// client can determine the dependencies. | |
// ConstructorInjection is unopinionated. | |
_ = ConstructorInjection{ | |
Foo: http.Server{}, | |
Bar: http.Server{}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment