Ideally, the code in my controller method should be:
public ActionResult GetRolesByUserGridData(MvcJqGrid.GridSettings gs, int userId)
{
var json = _userRoleService
.GetRolesByUser()
.ToOrderedList(“Id”, “Asc”)
.ToPagedList(gs.PageIndex, gs.PageSize)
.ToJsonData();
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
where ToOrderedList(), ToPagedList() and ToJsonData() would be extension methods to IQueryable<>, placed outside the Web project.
But it's still not clear to me where should the ViewModels live in this scenario:
A. Have them in the Service layer, which is in a different project - but then they lose all the logic related to Data Annotation, or I need to reference MVC in the Service layer, which seems weird.
B. Have them in the Web project - but why shouldn't the service be aware of the ViewModel and only retrieve the data that the ViewModel needs? Is it really necessary to create a new DTO object only to pass data from the service to the controller?