0

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.

Aneeq
  • 416
  • 3
  • 15

1 Answers1

-1

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.

waterdev
  • 7
  • 1
  • In that case, I should go with the former solution where I query database again and again for CommentsCount, FeedbacksCount etc? I mean won't it be inefficient in case of large amounts of data? Isn't there a work around to this problem? – Aneeq Sep 24 '16 at 19:41
  • In your repository you should have a methods like GetComments, therefore CommentsCount you could invoke GetComments method on service layer and get a count with .Count() ? You should not really make a method on repository that will prepare a viewModel for you. – waterdev Sep 24 '16 at 20:03
  • Repository doesn't work with DTOs – SiZE May 23 '22 at 05:42