In my ASP.net mvc application, I use the service level and repositories so that my controllers are thin. A typical read-only view is as follows:
public ActionResult Details(int id) { var project = _projectService.GetById(id); return View(Mapper.Map<Project, ProjectDetails>(project)); }
Service Level:
public class ProjectService : IProjectService { public Project GetById(int id) { var project = _projectRepository.GetProject(id);
Moving from a service level to a presentation model is quite simple because of the automapper, which can easily smooth things out. Moving another direct one from the view model to go to my service level is where I struggle to find a good solution.
In a situation like the Create action, what is a good approach for this?
[HttpPost] public ActionResult Create(CreateProjectViewModel model) { if(!ModelState.IsValid) { return View(model); }
I am sure that the service level should not know anything about presentation models, but I also do not think AutoMapper works well in this scenario, since it is not very good at accepting a flat model and turning it into a complex object.
How should my dispatcher interact with the service level? I want the code in the controller to be as light as possible.
source share