我们目前正在开发一个ASP.NET MVC 4项目,用户可以向政治家提问。当问题被问到时,它的状态正在等待中。主持人批准后,其状态将获得批准。
一个问题可以有几个答案,每个被问到这个问题的政治家可以给出一个答案。用户应该能够订阅问题。当这个问题得到一个新的答案时,他们应该会收到一封电子邮件,如果他们恰好在网站上在线,那么一点通知就是一个不错的选择。
所以基本上我需要一些方法来注册一个问题中的答案集合的计数已经改变。
我对Observer模式有基本的了解。我已经对此进行了更多研究,但我不确定如何将其应用于完整的Web应用程序。
我希望你们能给我一些关于如何以最简单的方式解决这个问题的建议/想法。我不知道我应该为这个系统创建哪些具体的类。
我们的问题模型如下所示:
public class Question
{
public Question()
{
AddressedPoliticians = new List<Politician>();
Topics = new List<Topic>();
Attachments = new List<Attachment>();
Answers = new List<Answer>();
QuestionSubscriptions = new List<QuestionSubscription>();
SupportingUsers = new List<Regular>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int QuestionId { get; set; }
[StringLength(250, ErrorMessageResourceType = typeof (Resources.Resources),
ErrorMessageResourceName = "MaxCharsQuestionAchieved")]
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "Question_GeneralQuestionRequired")]
public string GeneralQuestion { get; set; }
[StringLength(1000, ErrorMessageResourceType = typeof (Resources.Resources),
ErrorMessageResourceName = "MaxCharsExplanationAchieved")]
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "Question_ExplanationRequired")]
public string Explanation { get; set; }
//Whether the user is still writing the question
public bool IsTemplate { get; set; }
//Pending, Approved, Rejected
public QuestionState QuestionState { get; set; }
public DateTime DateSubmitted { get; set; }
public DateTime? JudgementDate { get; set; }
public virtual Regular Author { get; set; }
public virtual List<Politician> AddressedPoliticians { get; set; }
public virtual List<Topic> Topics { get; set; }
public virtual Moderator LastEditor { get; set; }
public virtual City City { get; set; }
public virtual List<Attachment> Attachments { get; set; }
public virtual List<Answer> Answers { get; set; }
public virtual List<QuestionSubscription> QuestionSubscriptions { get; set; }
public int FbShares { get; set; }
public int FbLikes { get; set; }
public int TwitterShares { get; set; }
public virtual List<Regular> SupportingUsers { get; set; }
[NotMapped]
public int SiteVotes
{
get { return SupportingUsers.Count; }
}
}
我们还有一个QuestionSubscription类。我们已经可以通过我们的网站创建它的实例,但此时它实际上还没有做任何事情。
public class QuestionSubscription : Subscription
{
public virtual Question Question { get; set; }
public QuestionSubscription()
{
LastMailSent = DateTime.Today;
LastUpdate = DateTime.Today;
WantsToReceiveEmail = true;
}
public void Update()
{
//Something has changed
//TODO Notify the user, don't know how yet ...
}
}
最后是超类订阅:
public abstract class Subscription
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SubscriptionId { get; private set; }
public DateTime LastUpdate { get; protected set; }
public DateTime LastMailSent { get; protected set; }
[Description("Do you want to receive emails?")]
public bool WantsToReceiveEmail { get; set; }
public NotificationFrequency NotificationFrequency { get; set; }
public virtual Regular Subscriber { get; set; }
}