【问题标题】:What should my model look like for a simplified Stack Overflow questionn&answers page对于简化的 Stack Overflow 问答页面,我的模型应该是什么样子
【发布时间】:2011-12-23 18:32:56
【问题描述】:

我是第一次尝试 MVC,我正在尝试构建一个非常简化的 StackOverflow 版本。我有模型/数据库表,例如:

Users
Questions
Answers
Comments

以及我用于视图的这些模型的一些视图模型。
模型之间的关系是:

Users 1:m  with Questin, Answers, Comments
Questions 1:m with Answers, Comments
Answers 1:m with Comments

Users m:m Questions
Users m:m Answers

投票。

问题页面的 ViewModel 应该是什么样子,这与您当前正在阅读的页面相同? 该页面应类似于:

-----------
问题

回答
回答
...
回答

仅适用于登录用户的回答问题的表格
------------------------------------
以及问题和答案下的 cmets 列表

我想出了这个:

public class QuestiongView
{
   public QuestionShort question { get; set;}
   public IEnumerable<AnswerShort> answers { get; set;}
   public AnswerWritabelByUser answerByUser {get; set;}  // only for logged in usesr. This is where you type your answer
}

QuestionShortAnswerShort 是其中包含List&lt;Comments&gt; 的类。
对于投票,我会使用 $.ajax 调用。

到目前为止,我认为这是正确的方法,但它似乎也很混乱。也许我会使用局部视图来清理视图中的代码。

那么,这是实现 MVC 的正确方法吗?您有一些建议可以改进我针对这个特定问题的整体方法吗?我知道我在说什么还是我错过了 MVC 的整个设计模式/概念?

【问题讨论】:

    标签: asp.net-mvc-3 model-view-controller design-patterns view model


    【解决方案1】:

    我会做一些非常接近的事情:

    模型

    public QuestionViewModel : IQuestionViewModel
    {
      public QuestionModel : Question { get; set; }
    }
    
    public IQuestionViewModel
    {
      QuestionModel: Question { get; }
    }
    
    public QuestionModel : IAnswersViewModel, ICommentsViewModel, IUserViewModel
    {
      public string Title { get; set; }
      public string Summary { get; set; }
      public IENumerable<Tag> Tags { get; set; }
      public UserModel User { get; set; }
    
      public IEnumerable<AnswerModel> Answers { get; set; }
    
      public IEnumerable<CommentModel> Comments { get; set; }
    }
    
    public IAnswersViewModel
    {
      IEnumerable<AnswerModel> Answers { get; }
    }
    
    public ICommentsViewModel
    {
      IEnumerable<CommentModel> Comments { get; }
    }
    
    public IUserViewModel
    {
      UserModel User { get; }
    }
    
    public AnswerModel : ICommentsViewModel, IUserViewModel
    {
      public Summary { get; set; }
      public UserModel { get; set; }
    
      public IEnumerable<CommentModel> Comments { get; set; }
    }
    
    public CommentModel : IUserViewModel
    {
      public string Summary { get; set; }
      public UserModel User { get; set; }
    }
    
    public UserModel 
    {
      public string Name { get; set ; }
    }
    

    控制器

    public class QuestionController : Controller
    {
      public ActionResult Details(int QuestionID)
      {
        // Populate the model as you see fit
        // I normally have my models populate themselves
        // So I don't duplicate code in my controllers
    
        QuestionViewModel model = QuestionViewModel.Get(QuestionID)
    
        if (model == null)
        {
          return this.UnavailableQuestion()
        }
    
        return this.View(model);
      }
    
      // Questions that don't exist
      public ActionResult UnavailableQuestion
      {
        return this.View();
      }
    }
    

    ** 观看次数(全部非常简化)**

    问题\Details.cshtml

    @Model IQuestionViewModel
    
    @model.Question.Title
    
    @model.Question.Summary
    
    @html.Partial("partial-UserComplex", model.Question)
    
    @html.Partial("partial-Comments", model.Question)
    
    @html.Partial("partial-Answers", model.Question);
    

    Shared\partial-UserComplex.cshtml

    @Model IUserViewModel   
    
    //Complex might display details like points, etc etc
    @model.User.Name
    

    Shared\partial-UserSimple.cshtml

    @Model IUserViewModel   
    
    //Simple would just have a name with a link to profile
    @model.User.Name
    

    Shared\partial-Comments.cshtml

    @Model ICommentsViewModel
    
    @foreach (CommentModel comment in model.Comments)
    {
      @comment.Summary
      @Html.Partial("partial-UserSimple", comment)
    }
    

    问题\partial-Answers.cshtml

    @Model IAnswersViewModel
    
    @foreach (AnswerModel answer in model.Answers)
    {
      @answer.Summary
    
      @Html.Partial("partial-UserComplex", answer)
    
      @Html.Partial("partial-Comments", answer)
    }
    

    优点

    • 每个 View/PartialView 都使用一个界面,因此我可以创建任意数量的不同模型并重复使用相同的视图。
    • 由于显示评论(也许?)和用户将遍布整个网站,我可以轻松地重用共享目录中的部分内容
    • 显示答案、评论和用户完全相互分离。
    • 非常可扩展。

    缺点

    • 在视图中使用接口有点复杂,因为您想要添加的任何新内容都必须添加到接口以及继承该接口的任何类。
    • 根据子类的数量和模型的重用,这相当复杂。

    【讨论】:

    • 感谢 Erik,这是一个很好的答案,它确实加快了开发速度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2015-12-09
    • 2017-12-14
    • 2013-01-27
    相关资源
    最近更新 更多