November 2018
Intermediate to advanced
346 pages
8h 12m
English
When an object requires a dependency to work, the easiest way to ensure that dependency is always available is to require all users to supply it as a parameter to the object's constructor. This is known as constructor injection.
Let's work through an example where we will extract a dependency, generalize it, and achieve constructor injection. Say we are we are building a website for an online community. For this site, we wish to send an email to new users when they sign up. The code for this could be like this:
// WelcomeSender sends a Welcome email to new userstype WelcomeSender struct { mailer *Mailer}func (w *WelcomeSender) Send(to string) error { body := w.buildMessage() return w.mailer.Send(to, body)}
We've made ...
Read now
Unlock full access