I'm looking for recommendations on how to approach the following design problem (using a fictitious example based on stackoverflow). I'd trying to avoid an anemic domain model and seek general "best-practice" advice for this type of case.
Scenario:
Suppose a new feature is being developed for stackoverflow that sends an email notification to a question's owner whenever his/her question receives 10 upvotes.
The domain object model is something like this:
public class Question
{
string Question { get; set; }
IList<Votes> Upvotes { get; set; }
User Owner { get; set; }
public void AddUpvote(Vote upvote)
{
Upvotes.Add(upvote);
}
}
Potential Implementations:
Change
AddUpvote()
to take anIEmailerService
parameter and perform the logic within theAddUpvote()
method.public void AddUpvote(Vote upvote, IEmailerService emailer) { Upvotes.Add(upvote); if ( Upvotes.Count == 10 ) { emailer.Send(Owner.EmailAddr); } }
Detect this state within
AddUpvote()
and haveAddUpvote()
resolve an IEmailService from an IoC container (instead of passing the IEmailerService as a parameter).Detect this state in the external service object that invokes
question.AddUpvote()
.public void UpvoteClickHandler(Question question) { question.AddUpvote(new Upvote()); if ( question.Upvotes.Count == 10 ) { _emailer.Send(question.Owner.EmailAddr); } }
Your better solution here!