I'm curious what folks who use Spring MVC (REST Controllers) with Repository whether you prefer to inject Repository directly, or put it behind an injected Service?
I've found it more natural to inject both, so that the Service only knows about "domain" objects (yes, that's the domain-driven design influence), but I've seen folks put everything behind a service and it'd be great to hear other opinions.
Well, isn't a repository really already a kind of service? Why would you add yet another layer of indirection if you can just let the controller interact with the repository directly?
This is my usual flow:
1) Request with data transfer objects comes in.
2) Controller translates data transfer objects to domain objects.
3) Controller performs logic with domain objects.
4) Controller passes domain objects to the injected repository.
5) Repository translates domain objects to entities (data transfer objects for the database).
6) Entities are serialized to database or deserialized from database.
7) Repository translates entities to domain objects and returns them to the controller.
8) Controller performs logic with domain objects.
9) Controller translates domain objects to data transfer objects.
10) Controller sends response with data transfer objects.
Thanks for the detailed reply, Stephen. That's pretty much how I do it, I've seen recommendations for not injecting the Repository into the Controller elsewhere and that's why I was asking what reasons people might have to only inject a Service.