• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Injecting Repositories vs. Services into Controllers?

 
Greenhorn
Posts: 11
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks,
;ted

 
Saloon Keeper
Posts: 14692
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ted Young
Greenhorn
Posts: 11
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

;ted
 
Stephan van Hulst
Saloon Keeper
Posts: 14692
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you share some links? I'm very curious about the reasons not to interact with the repository directly.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic