Email notifications. In a domain object or service?

I am looking for recommendations on how to approach the next design problem (using a dummy example based on stackoverflow). I would try to avoid the anemic domain model and look for general β€œbest practical” advice for this type of case.

Scenario:

Suppose a new feature is being developed for stackoverflow, which sends an email notification to the question owner whenever 10 upvotes receive his / her question.

The domain object model looks 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 the parameter IEmailerServiceand execute the logic in the method AddUpvote().

    public void AddUpvote(Vote upvote, IEmailerService emailer)
    {
        Upvotes.Add(upvote);
        if ( Upvotes.Count == 10 )
        {
            emailer.Send(Owner.EmailAddr);
        }
    }
    
  • AddUpvote() AddUpvote() IEmailService IoC ( IEmailerService ).

  • , question.AddUpvote().

    public void UpvoteClickHandler(Question question)
    {
        question.AddUpvote(new Upvote());
        if ( question.Upvotes.Count == 10 )
        {
            _emailer.Send(question.Owner.EmailAddr);
        }
    }
    
  • !

+3
4

, . , , 10 20 100 ...

, . , Question , . , SOLID .

public class Question
{
    public string Description { get; set; }
    public Int32 Votes { get; set; }
    public User Owner { get; set; }

    public event EventHandler<QuestionEventArgs> OnUpvote;

    private void RaiseUpvoteEvent(QuestionEventArgs e)
    {
        var handler = OnUpvote;
        if (handler != null) handler(this, e);
    }

    public void Upvote()
    {
        Votes += 1;

        RaiseUpvoteEvent(new QuestionEventArgs(this));
    }
}

public class MessageService
{
    private Question _question;

    public MessageService(Question q)
    {
        _question = q;

        q.OnUpvote += (OnUpvote);
    }

    private void OnUpvote(object sender, QuestionEventArgs e)
    {
        if(e.Question.Votes > 10)
            SendMessage(e.Question.Owner);
    }
}

public class QuestionEventArgs: EventArgs
{
    public Question Question { get; set; }

    public QuestionEventArgs(Question q)
    {
        Question = q;
    }
}

, . , - , , , .

+5

1) 2) - . "" :

  • , .
  • , .

, , , . (, ServerFault, StackOverflow)

, , . , :

NotificationManager ( , ).

public Class NotificationManager
{
    public void NotificationManager(NotificationPolicy policy, IEmailService emailer)
    {
    }
}

- (UpvoteClickHandler NotificationManager):

public void UpvoteClickHandler(Question question)
{
    question.AddUpvote(new Upvote());
    _notificationManager.Notify(Trigger.UpvoteAdded, question);
}

, UpvoteClickHandler, NotificationManager, ​​ NotificationManager , .

+2

. ( ) , . , , -. . , . , . ( 1 2) , . , , . , , . , , C. , .

+1

HI all,

, " , 10 upvotes" - , , .

(.. SMTP-), .

, , 1 . , , IEmailerService.

,

+1

Source: https://habr.com/ru/post/1709420/


All Articles