I have project DAL, BLL and Web. Where should I execute query using ToList()?
Right now I execute query using ToList() in controller - is it ok? For example - from the beginning:
DAL - my method in NotesRepository class:
public IQueryable<Notes> GetAllNotes()
{
return context.Notes.Include(x => x.Comments).OrderByDescending(x => x.CreateDate);
}
BLL - my method in NotesService class:
public IEnumerable<Notes> GetNotes()
{
return _unitOfWork.NotesRepository.GetAllNotes();
}
Web - my action in controller (execution query using ToList()):
public ActionResult Index()
{
var notes = _notesService.GetNotes().ToList();
return View(notes);
}
In my personal opinion, that's a data concern so the decision to call .ToList() should happen in the DAL. There may be a case to be made for flexibility to perhaps chain things together or refine the query, so you could justify putting the call in the BLL, but you build that flexibility into the DAL anyway and overall the DAL return data, not query objects.
Not that it ever happens in real life, but this also gives you more flexibility to swap out the DAL with some other data source (such as a web api). If you bleed IQueryable into your DAL, that is made harder and layer/tier encapsulation is broken.
I would not recommend letting IQueryable into the Controller.