Last active
February 19, 2020 09:21
-
-
Save adhamankar/45082fa78cd9e99011423a86c8a0efff to your computer and use it in GitHub Desktop.
Notification framework
This file contains 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
private void NotifyWelcomeToTheHub(User user) | |
{ | |
var notificationDto = new AppNotificationDto | |
{ | |
User = user.ProjectedAs<IdTitle>(), | |
InviteeEmail = user.Email, | |
SenderEmail = user.Email, | |
Title = string.Format("Welcome to the Hub"), | |
Template = "<<template>>.xsl", | |
Type = NotificationTypes.<<Type>> | |
}; | |
DispatchNotifications(new AppApiContext { Id = user.Id }, notificationDto); | |
} | |
protected void DispatchNotifications(AppApiContext apiContext, NotificationDto notificationDto) | |
{ | |
var notifiers = _notifierProvider.GetNotifiers<NotificationDto>(apiContext); | |
foreach (var notifier in notifiers) | |
{ | |
notifier.Context.Data = notificationDto; | |
try { notifier.Notify(); } | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Error in DispatchNotifications: {0}", ex.Message); | |
} | |
} | |
} |
This file contains 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
public class EmailNotifier<T> : INotifier<T> where T : NotificationDto | |
{ | |
public NotifierContext<T> Context { get; private set; } | |
public EmailNotifier(NotifierContext<T> context) | |
{ | |
Context = context; | |
} | |
public void Notify() | |
{ | |
var localContext = Context as EmailNotifierContext<T>; | |
var localConfig = localContext.Configuration as EmailNotifierConfiguration; | |
string xml = localContext.Data.Serialize(); | |
localContext.Body = xml.Transform(Path.Combine(localConfig.TemplatePath, localContext.Data.Template)); | |
localContext.To = localContext.Data.InviteeEmail; | |
if (ConfigurationManager.AppSettings["MODE"] != null && string.Compare("Debug", ConfigurationManager.AppSettings["MODE"], true) == 0) | |
{ | |
localContext.To = "recepient"; | |
} | |
localContext.Subject = localContext.Data.Title; | |
localContext.From = localContext.Data.SenderEmail; | |
//SendEmail(localContext); | |
} | |
private void SendEmail(EmailNotifierContext<T> context) | |
{ | |
var configurations = context.Configuration as EmailNotifierConfiguration; | |
using (MailMessage message = new MailMessage()) | |
{ | |
message.To.Add(context.To); | |
if (string.IsNullOrEmpty(context.CC) == false) | |
{ | |
var list = context.CC.Split(','); | |
foreach (string s in list) | |
{ | |
message.CC.Add(s); | |
} | |
} | |
if (string.IsNullOrEmpty(context.BCC) == false) message.Bcc.Add(context.BCC); | |
//message.From = new MailAddress(configurations.User); | |
message.From = new MailAddress("sender"); | |
message.Subject = context.Subject; | |
message.Body = context.Body; | |
message.IsBodyHtml = true; | |
using (SmtpClient smtpClient = new SmtpClient(configurations.Server)) | |
{ | |
smtpClient.Port = Convert.ToInt32(configurations.Port); | |
smtpClient.Credentials = new System.Net.NetworkCredential(configurations.User, configurations.Password); | |
smtpClient.Send(message); | |
} | |
} | |
} |
This file contains 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
public class EmailNotifierConfiguration : INotifierConfiguration | |
{ | |
public string TemplatePath { get; set; } | |
public string Server { get; set; } | |
public string Port { get; set; } | |
public string User { get; set; } | |
public string Password { get; set; } | |
} | |
public class EmailNotifierContext<T> : NotifierContext<T> where T : NotificationDto | |
{ | |
public string To { get; set; } | |
public string From { get; set; } | |
public string CC { get; set; } | |
public string BCC { get; set; } | |
public string Subject { get; set; } | |
public string Body { get; set; } | |
} |
This file contains 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
public interface INotifier<T> where T : NotificationDto | |
{ | |
NotifierContext<T> Context { get; } | |
void Notify(); | |
} |
This file contains 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
public interface INotifierConfiguration | |
{ | |
} |
This file contains 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
public interface INotifierProvider | |
{ | |
List<INotifier<T>> GetNotifiers<T>(AppApiContext apiContext) where T : NotificationDto; | |
} |
This file contains 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
public class NotificationDto | |
{ | |
public string Title { get; set; } | |
public string Template { get; set; } | |
public string SenderEmail { get; set; } | |
public IdTitle CreatedBy { get; set; } | |
public DateTime CreatedOn { get; set; } | |
public IdTitle User { get; set; } | |
public IdTitle Entity { get; set; } | |
public string InviteeEmail { get; set; } | |
public string InviteeName { get; set; } | |
public string InviteeEntityName { get; set; } | |
public string VerificationCode { get; set; } | |
public DateTime VerificationCodeExpiry { get; set; } | |
public string AppUrl { get; set; } | |
public string EmailContent { get; set; } | |
public bool HasEmailContent { get; set; } | |
} |
This file contains 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
public class NotifierContext<T> where T : NotificationDto | |
{ | |
public T Data { get; set; } | |
public INotifierConfiguration Configuration { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment