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,其中我查詢單個查詢中所有必需的數據?

任何幫助,高度讚賞。

回答

0

存儲庫方法應該映射和返回或recive DTO的,DAL層不應該瞭解MVC項目,它應該只知道DTO的。

+0

在這種情況下,我應該去以前的解決方案,我反覆查詢數據庫的CommentsCount,FeedbacksCount等?我的意思是在大量數據的情況下效率不高?這個問題沒有解決嗎? – Aneeq

+0

在你的倉庫你應該有一個方法,如GetComments, 因此CommentsCount你可以在服務層上調用GetComments方法,並用.Count()計數? 你不應該真的在存儲庫上做一個方法來爲你準備一個viewModel。 – waterdev

相關問題