Understanding the same method in the repository and service

1

Why in the service and in the controller do the objects point to the same function? I'll detail:

Repository:

@RepositoryRestResource(exported = false)
public interface AtividadeRepository extends CrudRepository<Atividade, Integer> {
    Atividade findByNome(String nome); //até aqui tudo bem.
}

service:

public Iterable<Atividade> findAll() {
   return atividadeRepository.findAll();
}

//Retorna todas as atividades, até aqui tudo bem.

Controller:

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public @ResponseBody ResponseEntity<Iterable<Atividade>> listAll() {
    Iterable<Atividade> atividades;
    atividades = atividadeService.findAll();
    return new ResponseEntity<Iterable<Atividade>>(atividades, HttpStatus.OK);
}

Why in addition to using the same method, does it also have the return?

Can not the controller get the object from the repository and send it to the view instead of the "search" and return? Or the line:

    return new ResponseEntity<Iterable<Atividade>>(atividades, HttpStatus.OK);

Does it only return the status?

I know you have the question of reusing code, but doing the same search, I find it redundant.

Or am I totally out of the way?

    
asked by anonymous 10.09.2015 / 16:20

1 answer

1

Why in addition to using the same method, does it also have the return?

Not quite the same method. Since you are using a multi-tier application, for this example, it may seem like the same thing, but it is not.

The repository is used to manipulate collections in some way. The service harmonizes the relationships between different repositories.

The controller can not take the object from the repository and send it to view instead of doing the "fetch" / h1>

You can even delete the service layer for this step and eliminate a long-winded part, but this would probably make your project out of the norm, since the service layer can be used at other stages of the system.

I know you have the question of reusing code, but doing the same search, I find it redundant.

It's not just you.

    
10.09.2015 / 16:41