我应该在哪里做映射的东西?存储库或服务层?

时间:2016-09-24 19:34:41

标签: entity-framework asp.net-web-api repository-pattern service-layer

好吧,我有这个DB模型“Book”

GetBook(int id)

我已经实现了存储库模式,我的Book方法返回public Book GetBook(int id) { return db.Books.Find(id); } ,如下所示:

BookViewModel

但是,我的public class BookViewModel { public int Id { get; set; } public string Title { get; set; } public string AuthorName { get; set; } public int CommentsCount { get; set; } public int FeedbacksCount { get; set; } public int ViewsCount { get; set; } } 也需要查询其他一些内容。它看起来像这样:

CommentsCount, FeedbacksCount, ViewsCount

目前,我的服务层将绑定模型映射到数据库模型并将它们传递给存储库。 现在我的问题是我应该在哪里查询这个额外的(视图特定的)数据?我应该为BookViewModel等编写单独的存储库方法,并从我的服务层调用它们来准备我的视图模型,还是应该编写一个返回类型为@Transactional public void execute() { for (int i = 0; i < 100000; i++) { carRespository.save(new Car()); } } 的新存储库方法,我在其中查询所有需要的数据单个查询?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

存储库方法应该映射并返回或重放DTO,DAL层不应该知道MVC项目,它应该只知道DTO的。