Breaking and Declaring Dependencies
First part
There are two parts to the DI pattern. The first is that I remove any dependencies on concrete classes from my component—in this case PasswordResetHelper. I do this by creating a class constructor that accepts implementations of the interfacesI need as arguments, like this:
- Remove
- any dependencies from my component—in this case PasswordResetHelper
- Create
- a class constructor that accepts implementations of the interfaces
public class PasswordResetHelper {
private IEmailSender emailSender;
public PasswordResetHelper(IEmailSender emailSenderParam) {
emailSender = emailSenderParam;
}
public void ResetPassword() {
// ...call interface methods to configure e-mail details...
emailSender.SendEmail();
}
}
By creating a class constructor that accept implementations of the interfaces I can remove any dependency.
Second part
The second part of the DI pattern is to inject the dependencies declared by the PasswordResetHelper class when I
create instances of it, hence the term dependency injection.
- create
- instances
- declared
- By the PasswordResetHelper
- inject
- the dependencies
All this really means is that I need to decide which class that implements the IEmailSender interface I am going to use,
create an object from that class and then pass the object as an argument to the PasswordResetHelper constructor.
No comments:
Post a Comment