0
votes

Well, I have this DB Model "Book"

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

And I have implemented repository pattern where my GetBook(int id) method returns a Book which looks like this:

public Book GetBook(int id) {
    return db.Books.Find(id);
}

However, my BookViewModel needs to query some other things as well. It looks like this:

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

Currently, my service layer is mapping binding models to DB models and passing them to repository. Now my question is where should I query this additional (view-specific) data? Should I write separate repository methods for CommentsCount, FeedbacksCount, ViewsCount etc. and call them from my service layer to prepare my view model or should I write a new repository method with return type BookViewModel where I query all the required data in a single query?

Any help is highly appreciated.

1

1 Answers

0
votes

The repository methods should map and return or recive DTO's, DAL layer should not know about MVC project, it should only know about DTO's.