I read a lot of theories about the differences between the service level and the controller, and I have some questions about how to put this into practice. One answer to Service Level and Controller: who cares what? He speaks:
I am trying to limit the controllers to doing work related to checking http parameters, which determine which service method to call with which parameters, what to put in httpsession or request, what kind of redirection or forwarding, or similar materials related to the website.
and http://www.bennadel.com/blog/2379-a-better-understanding-of-mvc-model-view-controller-thanks-to-steven-neiland.htm :
Red Flags: my controller architecture may degrade if:
The controller makes too many requests for the service level. The controller makes a series of service level requests that do not return data. The controller requests a level of service without passing arguments.
I am currently developing a web application with Spring MVC, and I have this way of saving user modified email:
@RequestMapping(value = "/saveEmail",method = RequestMethod.POST) public ModelAndView saveEmail( @Valid @ModelAttribute("changeEmailBean") ChangeEmailBean changeEmailBean, BindingResult changeEmailResult, Principal user, HttpServletRequest request){ if(changeEmailResult.hasErrors()){ ModelAndView model = new ModelAndView("/client/editEmail"); return model; } final String oldEmail = user.getName(); Client client = (Client) clientService.getUserByEmail(oldEmail); if(!clientService.isPasswordRight(changeEmailBean.getCurrentPassword(), client.getPassword())){ ModelAndView model = new ModelAndView("/client/editEmail"); model.addObject("wrongPassword","Password doesn't match to real"); return model; } final String newEmail = changeEmailBean.getNewEmail(); if(clientService.isEmailChanged(oldEmail, newEmail)){ if(clientService.isEmailUnique(newEmail)){ clientService.editUserEmail(oldEmail, newEmail); refreshUsername(newEmail); ModelAndView profile = new ModelAndView("redirect:/client/profile"); return profile; }else{ ModelAndView model = new ModelAndView("/client/editEmail"); model.addObject("email", oldEmail); model.addObject("emailExists","Such email is registered in system already"); return model; } } ModelAndView profile = new ModelAndView("redirect:/client/profile"); return profile; }
You can see that I have many requests for the service level, and I redirect from the controller - this is the business logic. Please show the best version of this method.
And one more example. I have this method that returns a user profile:
@RequestMapping(value = "/profile", method = RequestMethod.GET) public ModelAndView profile(Principal user) throws UnsupportedEncodingException{ Client clientFromDB = (Client)clientService.getUserByEmail(user.getName()); ModelAndView model = new ModelAndView("/client/profile"); model.addObject("client", clientFromDB); if(clientFromDB.getAvatar() != null){ model.addObject("image", convertAvaForRendering(clientFromDB.getAvatar())); } return model; }
The convertAvaForRendering method (clientFromDB.getAvatar ()) is placed in the superclass of this controller, this is the correct placement of this method, or it should be placed in the service level
Help, this is really important for me.