I have a function - TriggerNotifications
- that accepts a NotificationType
. The Type determines which settings I should pull from the User
.
The code looks like this:
public void TriggerNotification(DbContext db, User user, NotificationType type, string content) {
switch (type) {
case NotificationType.Followed:
CreateNotification(db, user, user.Followed, content);
return;
case NotificationType.Unfollowed:
CreateNotification(db, user, user.Unfollowed, content);
return;
case NotificationType.Messaged:
CreateNotification(db, user, user.Messaged, content);
return;
default:
throw new InvalidDataContractException("Invalid Notification Type");
}
}
So the only purpose of this method is to determine which settings should be used. Every User
has a property matching each of the enum NotificationTypes
.
I think this is a good use case for reflection, but want to ask if there's any other way to reduce redundancy here. I don't like reflection because it's not as straightforward to debug, and it's slow (right?).
UPDATE
CreateNotification:
private void CreateNotification(DbContext db, User user, NotificationSetting setting, string content) {
// send email, notification, or both
if (setting.Equals(NotificationSetting.None))
return;
var notification = new Notification() {
UserId = user.userId
Description = content,
Email = email,
};
if (setting.Equals(NotificationSetting.Email) || setting.Equals(NotificationSetting.Both)) {
// tell worker role to pick it up
notification.PendingEmail = true;
}
// signalR stuff here
db.Add(notification);
}
User:
public class User : Settings {
// other stuff
}
Settings:
public abstract class Settings {
// notifications
public NotificationSetting Followed { get; set; }
public NotificationSetting Unfollowed { get; set; }
public NotificationSetting Messaged { get; set; }
}
NotificationSetting:
// values stored in the db for each setting property - messaged, unfollowed, etc
public enum NotificationSetting {
Navbar,
Email,
Both,
None
}
NotificationType:
// values used in logic only to tell the notification builder which setting to pull
public enum NotificationType {
Followed,
Unfollowed,
Messaged
}