2016-07-13 7 views
1

ビルド通知/アラートシステムの計画が必要です。C#の通知システム/サービス(ASP.NET MVCあり)

「キャンペーン」という名前のオブジェクトがあり、ステータスは「ステータス」です。ステータスは、承認済み、拒否済み、補足、勤務先などが可能です。 ステータスが変更されたときに通知/警告を送信します。

E.q.私のポータルに電子メール通知と警告が表示されます。

私はキャンペーン上で動作する1つのコントローラにすべてを作りたくありません。だから私はデリゲートとイベントについて考えていました。しかし、ついに私はそれを行うのに十分なことを知らない。

私がについて考える:

ドメインモデル:

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を呼び出して警告することができます。しかし、これは悪いです;)

私はキャンペーンに代理人とイベントを追加することを考えていました。

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とIcを使用してオブジェクトを注入する)

要約通知サービスでは、電子メールとアラートを送信する必要があります。フロントエンドに警告を表示する必要があります。そのような

マイアラートのドメインモデル:

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 
} 

私は、ユーザーにアラートを送信したい場合は、私は時々、アラートメールを送信したいです。 時にはメールのみを送信し、アラートのみを送信したい場合があります。

答えて

3

ここでは代議員やイベントは使用しません。メソッドを呼び出す方がはるかに透過的で、デリゲートやイベントを使用するメリットはありません。

私の構造は、次のようになります。

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に言及しました:))。

+0

なぜここにビルダーと戦略がありますか? – Nerf

+0

@Nerfキャンペーンステータス(異なるメールテンプレートやテキスト)に基づいて異なる通知を作成するには、通知ビルダが必要な場合があります。通知方法は、さまざまな種類の通知がある場合などに便利です。電子メール通知、SMS、プッシュなど – Andrei

+0

Ok thx。私はここでそれらを見ないので、私はこれらのパターンについてもっと何かを読む必要があります。 – Nerf

関連する問題