对于简化的 Stack Overflow 问题和答案页面,我的模型应该是什么样子

发布于 2024-12-22 16:42:56 字数 1210 浏览 0 评论 0原文

我是第一次尝试 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 应该是什么样子? 该页面应该类似于:

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

回答
回答
...
回答

仅用于登录用户回答问题的表单
-----------------------
以及问题和答案下的评论列表

我想出了这个:

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的类代码> 在其中。
对于投票,我将使用 $.ajax 调用。

根据我到目前为止所学到的知识,我认为这是正确的方法,但它看起来也很混乱。也许我会使用分部视图来清理视图中的代码。

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

I am trying out MVC for the first time and I am trying to build a very simplified version of StackOverflow. I have Models/database tables like:

Users
Questions
Answers
Comments

and some ViewModels of these Models which I use for the Views.
The relations between the models are:

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

and

Users m:m Questions
Users m:m Answers

for votes.

What should my ViewModel look like for the Question page, which is the same as this page that you are currently reading this from?
The page should be something like:

-----------------------
Question

Answer
Answer
...
Answer

form for answering the question only for logged in users
-----------------------
and list of comments under the Question and the answers

I come up with this:

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
}

and QuestionShort and AnswerShort are classes which have List<Comments> in them.
For voting i would use $.ajax calls.

With what i have learned so far i think this is the right way to do it, but it seems messy as well. Maybe i will use partial view to clean up the code in the View.

So, is this the correct way of implementing MVC, do you have some suggestions of improving my whole approach for this particular problem, do I know what am i talking about or have i missed the whole design pattern/ concept of MVC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2024-12-29 16:42:56

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

模型

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();
  }
}

**视图(所有都非常简化)**

Question\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);

共享\partial-UserComplex.cshtml

@Model IUserViewModel   

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

共享\partial-UserSimple.cshtml

@Model IUserViewModel   

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

共享\partial-Comments.cshtml

@Model ICommentsViewModel

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

Question\partial-Answers.cshtml

@Model IAnswersViewModel

@foreach (AnswerModel answer in model.Answers)
{
  @answer.Summary

  @Html.Partial("partial-UserComplex", answer)

  @Html.Partial("partial-Comments", answer)
}

优点

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

缺点

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

I would do something very close to:

Models

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 ; }
}

Controller

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();
  }
}

** Views (All VERY simplified)**

Question\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)
}

Question\partial-Answers.cshtml

@Model IAnswersViewModel

@foreach (AnswerModel answer in model.Answers)
{
  @answer.Summary

  @Html.Partial("partial-UserComplex", answer)

  @Html.Partial("partial-Comments", answer)
}

PROS

  • Each View/PartialView is using an interface so I can create as many different models I want and reuse the same view.
  • Since displaying Comments(Maybe?) and Users will be all over the site, I can easily reuse the partials in the shared directory
  • Displaying Answers, Comments, and Users is completely decoupled from each other.
  • Very extensible.

CONS

  • Using Interfaces in Views is a little complex because anything new you want to add you have to add to interface AND any class the inherits the interface.
  • This is fairly complex based on the number of subclasses and reuse of models.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文