带有 ASP.NET MVC 的 C# 中的通知系统/服务

Ner*_*erf 5 c# asp.net-mvc events delegates solid-principles

我需要构建通知/警报系统的计划。

我有一个名为“Campaign”的对象,它有“Status”。状态可以是接受、拒绝、补充、工作等。我想在状态更改时发送通知/警报。

在我的门户中发送电子邮件通知和警报。

我不想在我操作 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. 
}
Run Code Online (Sandbox Code Playgroud)

我该如何处理事件?尽可能多的自动。当然,我可以手动调用 AlertsService 并发出警报。但这很糟糕;)

我正在考虑向 Campaign 添加委托和事件。

class Campaign {
    public delegate void CampaignStatusChange(object sender, EventArgs e);
    public event CampaignStatusChange OnCampaignStatusChange;
}
Run Code Online (Sandbox Code Playgroud)

并将事件与:

class CampaignStatusChangeHandler {
    public CampaignStatusChangeHandler(IRepository<bla bla> repository, INotificationsService notificationService) {
        // these will be inject via ctor
    }

    // 
}
Run Code Online (Sandbox Code Playgroud)

我想尽可能多地使用 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
}
Run Code Online (Sandbox Code Playgroud)

当我想向用户发送警报时,我想有时发送电子邮件并发出警报。有时我只想发送电子邮件,只发送警报。

And*_*rei 5

我不会在这里使用委托和事件。调用方法更加透明,使用委托和事件不会有任何好处。

我的结构看起来像这样:

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();
}
Run Code Online (Sandbox Code Playgroud)

也可以(如果有不同类型的通知)

// 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);
}
Run Code Online (Sandbox Code Playgroud)

这完全取决于您的应用程序可扩展性要求。SOLID 非常重要,但请确保避免过度设计(您提到了 KISS :))。