From: AMP on
Hello,
If I pass an interface as a parameter to a constructor, where does the
implementation come from? I'm just reading about this and they are
saying it help seperation of concerns, but not sure how.
From Apress Book:
public class PasswordResetHelper
{
private IEmailSender _emailSender;
private ILogWriter _logWriter;
// Constructor
public PasswordResetHelper(IEmailSender emailSender, ILogWriter
logWriter)
{
// This is the Inversion-of-Control bit. The constructor demands
instances
// of IEmailSender and ILogWriter, which we save and will use later.
this._emailSender = emailSender;
this._logWriter = logWriter;
}
// Rest of code uses _emailSender and _logWriter
}
From: Arne Vajhøj on
On 03-05-2010 15:22, AMP wrote:
> If I pass an interface as a parameter to a constructor, where does the
> implementation come from? I'm just reading about this and they are
> saying it help seperation of concerns, but not sure how.
> From Apress Book:
> public class PasswordResetHelper
> {
> private IEmailSender _emailSender;
> private ILogWriter _logWriter;
> // Constructor
> public PasswordResetHelper(IEmailSender emailSender, ILogWriter
> logWriter)
> {
> // This is the Inversion-of-Control bit. The constructor demands
> instances
> // of IEmailSender and ILogWriter, which we save and will use later.
> this._emailSender = emailSender;
> this._logWriter = logWriter;
> }
> // Rest of code uses _emailSender and _logWriter
> }

Whatever the caller passes that implements that
interface.

It can be hardcoded or it can be based on configuration (the
subject line says IoC so this is a possibility).

Arne