Question

I have an application written using the Spring and Hibernate frameworks. Everything works correctly but I do have one question: if controllers invoke business logic by calling service layer methods, where should certain code go, e.g. in the following code, should the code for setting up a new Person's Role and password be in the controller method processing the AddPerson page's POST request, or in a service layer method?

// Saves addPerson.jsp.
 @RequestMapping(value = "/add", method = RequestMethod.POST)
 public String postAdd(@ModelAttribute("person") Person person) {  
      logger.debug("PersonController.postAdd called");

      // Create random number for new Person's password.
      person.setPassword(String.valueOf(Java_Utils.getRandomNumber()));

      // Create role for new Person.
      person.setRole("PERSON");

      // Add Person.
      personService.add(person);

      // Set records.jsp
      return "redirect:/demo/main/record/list";
 }

This is of course only a simple example but I'm curious. The role of PERSON is to satisfy a constraint using Tomcat to prevent user's reaching an authenticated page.

Was it helpful?

Solution

It should be in the service layer. The Controller layer is for handling and translating GUI stuff. But the creation of an user and configure it correct is not GUI stuff it is a business (or technical) use case, therefore one should place it in a service.

`personService.createPersonWithRandomPassword();`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top