We have asked to keep the MVC controller as thin, so we decided to move all the service calling/business logic into the ViewModel class.
But is it good approach to use a ViewModel class to make a service call?
Does this follows the OOPS/SOLID principle?
public class EmployeeController:Controller
{
private IEmployeeService EmpServ{get;set;}
public EmployeeController(IEmployeeService service)
{
EmpServ=service;
}
public ActionResult GetEmployee(int Id)
{
EmployeeViewModel vm=new EmployeeViewModel(EmpServ);
vm= vm.GetData(Id);
//some 10 lines of code for validation logic, etc
return View(vm);
}
}
The view model class is calling the business service method
public class EmployeeViewModel
{
public string Name{get;set;}
//...etc
private IEmployeeService EmpServ{get;set;}
public EmployeeViewModel(IEmployeeService service)
{
EmpServ=service;
}
public EmployeeViewModel GetData(int Id)
{
Employee e= EmpServ.GetEmpDetails(id);
EmployeeViewModel vm=ConvertEmpToViewModel(e);//another helper method
return vm;
}
}