Decoupling from the dependency

The first step is to identify the dependency we wish to inject. For our handler, this is not the database or the exchange rate call. We wish to inject the next software layer, which in this case is the model layer.

Specifically, we want to inject this line from our register method:

registerer := &register.Registerer{}

Following the same process we used easier, we first promote the object to a member variable, as shown in the following code:

// RegisterHandler is the HTTP handler for the "Register" endpoint
type RegisterHandler struct {
registerer *register.Registerer
}

As this does nothing to decouple our code from the dependency, we then define our requirements as a local interface and update the member variable, as shown in the following code:

// RegisterModel will validate and save a registration
type RegisterModel interface {
Do(in *data.Person) (int, error)
}

// RegisterHandler is the HTTP handler for the "Register" endpoint
type RegisterHandler struct {
registerer RegisterModel
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.71.211