创建博客评论,并使用ASP.NET MVC 4(嵌套集合)回复部 [英] Creating a Blog Comments and Reply section using ASP.NET MVC 4 (nested collections)

查看:183
本文介绍了创建博客评论,并使用ASP.NET MVC 4(嵌套集合)回复部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧..即时通讯建立一个博客评论和回复部分,我必须映射到我的数据库这三大类。第一类持有的相关意见收集到的文章。第二类持有相关言论的集合评论..

Ok.. Im building a Blog Comment and Reply section and I have these three classes mapped to my DB.. The first class holds a collection of related comments to an article.. The second class holds a collection of related remarks to the comments..

public class Article
{
    public int ArticleID { get; set; }
    public byte[] Image { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public DateTime DatePublished { get; set;  }
    public string Author { get; set; }
    public CategoryTyp Category { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
}

public class Comment
{
    public int CommentID { get; set; }
    public int ArticleID { get; set; }
    public int CategoryID { get; set; }
    public int UserID { get; set; }
    public string Description { get; set; }
    public DateTime CommentDate { get; set; }

    public virtual ICollection<Remark> Remarks { get; set; }
}

public class Remark
{
    public int RemarkID { get; set; }
    public int CommentID { get; set; }
    public int ArticleID { get; set; }
    public string RemarkDetail { get; set; }
    public DateTime RemarkTime { get; set;  }
}

和我的控制器内...

And inside my Controller...

public ActionResult GetArticle(int id)
{
    var article = db.Articles.Include("Comments").Where(a => a.ArticleID == id).SingleOrDefault();

    return View(article);
}

我明白预先加载的基础,但我的问题是:

I understand the basis of eager loading but my questions are:

1)如何实现它时,从多个相关表中的提取数据?

1) How do you implement it when your pulling data from multiple related tables?

2)这是什么填充到视图的最佳做法?有一次,我创建一个视图模型我如何的东西相关的收藏品?请举例..

2) What is the best practice of populating it to the View? Once I create a View Model how do i stuff the related collections? Example please..

感谢

推荐答案

1)如果有多个相关的表可以有两种方案:

1) With multiple related tables you can have two scenarios:

一)多顶级的关系:你只需添加多个Include语句(我会建议使用拉姆达前pressions,而不是为这个字符串,以避免输入错误)。

a) Multiple top level relations: you simply add multiple Include statements (I would suggest using lambda expressions instead of strings for this, to avoid typos).

db.Articles
   .Include(a=>a.Comments)
   .Include(a=>a.SomethingElse)
   .FirstOrDefault(a=>ArticleID==id); // Side note: I would suggest this instead of your Where plus SingleOrDefault

有关这些情景我总是用一个辅助方法,如这个

For these scenarios I always use a helper method like this one.

二)多重嵌套相关实体:

b) Multiple nested related entities:

db.Articles
    .Include(a=>a.Comments.Select(c=>c.Remarks)
    .FirstOrDefault(a=>ArticleID==id);

2)这是一个有点取决于你如何将数据传递给意见。一个最佳实践,我可以告诉你的是,你不应该让美景延迟加载任何相关实体或集合。所以,你包括的使用是正确的,但我甚至会建议删除虚拟(禁用延迟加载),以避免丢失意外的包含。

2) It's a bit up to you how you pass the data to the views. One best practice I can tell you is that you shouldn't let views lazy load any dependant entities or collections. So your use of Include is correct, but I would even suggest to remove the virtual (deactivate lazy loading) to avoid missing an Include by accident.

关于你提到,你实际上是没有使用视图模型的ViewModels,但你的数据模型。这是在大多数情况下确定的,除非你需要将数据以某种方式格式化或添加额外的信息。然后,你需要创建一个视图模型,并从EF数据来映射。

Regarding the ViewModels you mention, you are actually not using view models, but your data models. This is OK in most cases, unless you need to format the data somehow or add extra information. Then you would need to create a View Model and map it from the data coming from EF.

另一种情况是,如果你使用的WebAPI或一个Ajax动作。在这种情况下,我会建议使用DTO(相当于一个视图模型)能够更好地控制返回的数据及其系列化。

Another scenario would be if you used WebAPI or an Ajax Action. In that case, I would suggest to use a DTO (equivalent to a ViewModel) to be able to better control the data returned and its serialization.

有关的ViewModels最后一个意见是,如果你有重实体,但你只需要几个属性,一个很好的选择是使用预测,指示EF只加载所需的性能,而不是完整的对象。

One last comment about ViewModels is that if you have heavy entities but you only need a few properties, a good choice is to use Projections, to instruct EF to only load the required properties, instead of the full object.

db.Articles
      .INCLUDE(一个=> a.Comments)
      。选择(A =>新ArticleDto {ID = a.ArticleID,标题= a.Title})
      .ToListAsync();

db.Articles .Include(a=>a.Comments) .Select(a=>new ArticleDto { Id = a.ArticleID, Title = a.Title }) .ToListAsync();

这将转化为一个SELECT条款ArticleID,标题FROM章程,避免返回文章的身体和你可能不需要其他的东西。

This will translate to a "SELECT ArticleID, Title FROM Articles", avoiding returning the article bodies and other stuff that you might not need.

这篇关于创建博客评论,并使用ASP.NET MVC 4(嵌套集合)回复部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆