Have you ever written an object and injected a dependency that you knew was only going to have one implementation? Perhaps you have injected the database handling code into the business logic layer, as shown in the following code:
func NewMyLoadPersonLogic(ds DataSource) *MyLoadPersonLogic {
return &MyLoadPersonLogic{
dataSource: ds,
}
}
type MyLoadPersonLogic struct {
dataSource DataSource
}
// Load person by supplied ID
func (m *MyLoadPersonLogic) Load(ID int) (Person, error) {
return m.dataSource.Load(ID)
}
Have you ever added a dependency to your constructor for the sole purpose of mocking it out during testing? This is shown in the following code:
func NewLoadPersonHandler(logic LoadPersonLogic) *LoadPersonHandler {
return &LoadPersonHandler{
businessLogic: logic,
}
}
type LoadPersonHandler struct {
businessLogic LoadPersonLogic
}
func...