0
嗯,我有这个DB模式“尚书”我应该在哪里做映射的东西?存储库或服务层?
public class Book {
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public bool IsSubmitted { get; set; }
public bool IsCompleted { get; set; }
public bool IsDeleted { get; set; }
}
而且我已经实现了存储库模式在我的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; }
}
目前,我的服务层映射模型绑定到DB模式并将它们传递给仓库。 现在我的问题是我应该在哪里查询这个额外的(视图特定的)数据?我是否应该为CommentsCount, FeedbacksCount, ViewsCount
等编写单独的存储库方法,并从我的服务层调用它们以准备我的视图模型,还是应该编写一个新的存储库方法,返回类型为BookViewModel
,其中我查询单个查询中所有必需的数据?
任何帮助,高度赞赏。
在这种情况下,我应该去以前的解决方案,我反复查询数据库的CommentsCount,FeedbacksCount等?我的意思是在大量数据的情况下效率不高?这个问题没有解决吗? – Aneeq
在你的仓库你应该有一个方法,如GetComments, 因此CommentsCount你可以在服务层上调用GetComments方法,并用.Count()计数? 你不应该真的在存储库上做一个方法来为你准备一个viewModel。 – waterdev