I have an object that's responsible for sending email, so I create an EmailSender
, then tell it to SendEmail
, passing in some EmailDetails
:
string diagnostics;
EmailSender sender = new EmailSender();
try
{
sender.SendEmail(details);
//sender.SendEmail(details, out diagnostics);
}
catch(Exception e)
{
logger.log(sender.CurrentError);
}
diagnostics = sender.Diagnostics;
If I add an out parameter to SendEmail
, does that add a business responsibility in terms of SOLID design principles since I'm now saying: "You must attempt to send an email, and you're also responsible for initializing and populating diagnostic data"
Perhaps responsibility isn't the right word, but is one pattern better than the other?
You're not violating the Single Responsibility Principle - SRP doesn't mean an object shouldn't know how to talk to its collaborators. That's part of its contract, it's a natural responsibility. If EmailSender
wasn't responsible for reporting diagnostics about the mail delivery, who would be ?
The only thing you need to ensure is that Diagnostics
remains on a relevant granularity level with regard to EmailSender
. EmailSender
doesn't depend on its consumers, consumers depend on EmailSender
, so EmailSender
shouldn't adopt the consumers formalism but rather impose its own semantics on them.