使用 ASP.NET MVC 在 C# 中通知 system/service
Notification system/service in C# with ASP.NET MVC
我需要构建 Notifications/Alerts 系统的计划。
我有一个名为 "Campaign" 的对象,它有 "Status"。状态可以是接受、拒绝、补充、工作等。
我想在状态更改时发送 Notifications/Alerts。
E.q。我的门户中的电子邮件通知和警报。
我不想在我操作 Campaign 的一个控制器中完成所有操作。所以我在考虑代表和事件。但最后我不知道该怎么做。
我在想什么:
领域模型:
class Campaign {
CampaignStatus Status { get; set;}
}
abstract class Notification {
// properties
}
class EmailNotification {
// properties specific for email
}
class Alert {
// properties specific for alerts
}
class CampaignAlert {
// properties specific for campaign alerts
}
Services:
INotificationsService {
Send();
}
IAlertsService : INotificationsService {
Get(); // I need showing list of alerts too
GetAll();
Update(); // for updating info if alert was viewed.
Save(); // I need saving alerts in db.
}
我该如何处理事件?尽可能自动。当然,我可以手动调用 AlertsService 并发出警报。但这很糟糕 ;)
我正在考虑向 Campaign 添加委托和事件。
class Campaign {
public delegate void CampaignStatusChange(object sender, EventArgs e);
public event CampaignStatusChange OnCampaignStatusChange;
}
并将事件连接到:
class CampaignStatusChangeHandler {
public CampaignStatusChangeHandler(IRepository<bla bla> repository, INotificationsService notificationService) {
// these will be inject via ctor
}
//
}
我想尽可能多地用 SOLID、KISS 和 DRY 制作它。当然使用 TDD,我使用 IoC 来注入对象 ;)
总结我需要通知服务,我可以独立发送电子邮件和提醒。我需要在前端显示警报。
我的警报域模型是这样的:
public abstract class Notification
{
public string Title { get; set; }
public string Content { get; set; }
public DateTime Created { get; set; }
public NotificationType Type { get; set; }
}
public enum NotificationType
{
Email,
Alert
}
public class EmailNotification : Notification
{
public string From { get; set; }
public ICollection<string> To { get; set; }
public ICollection<string> Bcc { get; set; }
}
public class Alert : Notification
{
public object LinkedObject { get; set; }
public bool WasSeen { get; set; }
}
public class CampaignAlert : Alert
{
public CampaignAlertType CampaignAlertType { get; set; }
}
public enum CampaignAlertType
{
Accepted,
Rejected,
Active,
Finished
}
当我想向用户发送警报时,我想有时发送电子邮件并发出警报。
有时我只想发送电子邮件和警报。
我不会在这里使用委托和事件。调用方法更加透明,使用委托和事件不会给您带来任何好处。
我的结构应该是这样的:
interface ICampaignService
{
// It's business logic
// 1. Updates campaign
// 2. Creates notification using builder
// 3. Uses notification sender to send notification
// (4. creates alert object for notification)
void UpdateCampaignStatus(int campaignId, Status status);
}
// Builds different notifications based on different
// campaign statuses. For instance assign different
// email templates and use different text.
interface INotificationBuilder<TNotification> where TNotification : Notification
{
TNotification Build();
}
interface INotificationSender
{
Send(Notification notification);
}
interface IAlertsRepository
{
Get();
GetAll();
Update();
Create();
}
也可以(如果有不同类型的通知)
// If you want to send different types of notifications like
// Email, Push, SMS etc. Each notification type requires different
// logic for sending notification. Strategy pattern is perfect here.
interface INotificationStrategy : INotificationSender
{
Send(Notification notification);
}
这完全取决于您的应用程序可扩展性要求。 SOLID 非常重要,但一定要避免过度设计(你提到了 KISS :))。
我需要构建 Notifications/Alerts 系统的计划。
我有一个名为 "Campaign" 的对象,它有 "Status"。状态可以是接受、拒绝、补充、工作等。 我想在状态更改时发送 Notifications/Alerts。
E.q。我的门户中的电子邮件通知和警报。
我不想在我操作 Campaign 的一个控制器中完成所有操作。所以我在考虑代表和事件。但最后我不知道该怎么做。
我在想什么:
领域模型:
class Campaign {
CampaignStatus Status { get; set;}
}
abstract class Notification {
// properties
}
class EmailNotification {
// properties specific for email
}
class Alert {
// properties specific for alerts
}
class CampaignAlert {
// properties specific for campaign alerts
}
Services:
INotificationsService {
Send();
}
IAlertsService : INotificationsService {
Get(); // I need showing list of alerts too
GetAll();
Update(); // for updating info if alert was viewed.
Save(); // I need saving alerts in db.
}
我该如何处理事件?尽可能自动。当然,我可以手动调用 AlertsService 并发出警报。但这很糟糕 ;)
我正在考虑向 Campaign 添加委托和事件。
class Campaign {
public delegate void CampaignStatusChange(object sender, EventArgs e);
public event CampaignStatusChange OnCampaignStatusChange;
}
并将事件连接到:
class CampaignStatusChangeHandler {
public CampaignStatusChangeHandler(IRepository<bla bla> repository, INotificationsService notificationService) {
// these will be inject via ctor
}
//
}
我想尽可能多地用 SOLID、KISS 和 DRY 制作它。当然使用 TDD,我使用 IoC 来注入对象 ;)
总结我需要通知服务,我可以独立发送电子邮件和提醒。我需要在前端显示警报。
我的警报域模型是这样的:
public abstract class Notification
{
public string Title { get; set; }
public string Content { get; set; }
public DateTime Created { get; set; }
public NotificationType Type { get; set; }
}
public enum NotificationType
{
Email,
Alert
}
public class EmailNotification : Notification
{
public string From { get; set; }
public ICollection<string> To { get; set; }
public ICollection<string> Bcc { get; set; }
}
public class Alert : Notification
{
public object LinkedObject { get; set; }
public bool WasSeen { get; set; }
}
public class CampaignAlert : Alert
{
public CampaignAlertType CampaignAlertType { get; set; }
}
public enum CampaignAlertType
{
Accepted,
Rejected,
Active,
Finished
}
当我想向用户发送警报时,我想有时发送电子邮件并发出警报。 有时我只想发送电子邮件和警报。
我不会在这里使用委托和事件。调用方法更加透明,使用委托和事件不会给您带来任何好处。
我的结构应该是这样的:
interface ICampaignService
{
// It's business logic
// 1. Updates campaign
// 2. Creates notification using builder
// 3. Uses notification sender to send notification
// (4. creates alert object for notification)
void UpdateCampaignStatus(int campaignId, Status status);
}
// Builds different notifications based on different
// campaign statuses. For instance assign different
// email templates and use different text.
interface INotificationBuilder<TNotification> where TNotification : Notification
{
TNotification Build();
}
interface INotificationSender
{
Send(Notification notification);
}
interface IAlertsRepository
{
Get();
GetAll();
Update();
Create();
}
也可以(如果有不同类型的通知)
// If you want to send different types of notifications like
// Email, Push, SMS etc. Each notification type requires different
// logic for sending notification. Strategy pattern is perfect here.
interface INotificationStrategy : INotificationSender
{
Send(Notification notification);
}
这完全取决于您的应用程序可扩展性要求。 SOLID 非常重要,但一定要避免过度设计(你提到了 KISS :))。