Created
October 15, 2013 15:26
-
-
Save Boggin/6993401 to your computer and use it in GitHub Desktop.
Notification service with email as an example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Notification | |
{ | |
using System.Net.Mail; | |
public class EmailService : IEmailService | |
{ | |
public void Notify(NotificationDto notification) | |
{ | |
var msg = new MailMessage | |
{ | |
Body = notification.Message, | |
From = new MailAddress(notification.ReplyTo), | |
IsBodyHtml = notification.IsHtml, | |
Subject = notification.Title | |
}; | |
foreach (string addressee in notification.Addressees) | |
{ | |
msg.Bcc.Add(addressee); | |
} | |
using (var smtpClient = new SmtpClient { Host = "localhost" }) | |
{ | |
// Use Papercut [papercut.codeplex.com] for test server. | |
smtpClient.Send(msg); | |
} | |
msg.Dispose(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Business.DTOs | |
{ | |
public class NotificationDto | |
{ | |
/// <summary> | |
/// Gets or sets multiple recipients' addresses. | |
/// </summary> | |
/// <remarks> | |
/// These could be email addresses or mobile phone numbers or | |
/// whatever fits with the <c>NotificationMedium</c>. | |
/// </remarks> | |
public string[] Addressees { get; set; } | |
/// <summary> | |
/// Gets or sets a value indicating whether the message is marked up with HTML. | |
/// </summary> | |
public bool IsHtml { get; set; } | |
public string Message { get; set; } | |
public string ReplyTo { get; set; } | |
public string Title { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Notification | |
{ | |
public class NotificationService : INotificationService | |
{ | |
public NotificationService(IEmailService emailService) | |
{ | |
this.EmailService = emailService; | |
} | |
protected IEmailService EmailService { get; set; } | |
public void NotifyIssueSaved(int issueId, string issueName, IEnumerable<PersonDto> users) | |
{ | |
if (!this.Configuration.Enabled) | |
{ | |
return; | |
} | |
var addresses = users.Select(user => user.Mail).ToArray(); | |
// create a notification. | |
var notificationDto = new NotificationDto | |
{ | |
Message = "Updated.", | |
ReplyTo = "[email protected]", | |
Addressees = addresses, | |
Title = "Update" | |
}; | |
// create a task (TPL) for each type of notification that the user's interested in. | |
Task.Factory.StartNew(() => this.EmailService.Notify(notificationDto)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment