Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How design the service classes?

 
Daniel Näslund
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've started on a simple Spring Diary application that uses JPA and Hibernate for ORM.
I have a couple of models that depend on each other. For instance a User can have many Days which can have many Notes. So I created a service-class for each model. But when I want to create a Day I need access to the corresponding User object that must be set. I don't want to have multiple service classes injected in one controller. That feels bad somehow. Is it bad?

My question is basically: What is the best approach when designing service classes for Spring? Should I just put it all in one service class and persist or merge the User when I want to save my changes? That feels so wrong! I want every class to do one thing for ease of maintainability. One big bloated service class is harder to understand and maintain is my opinion. Should I have service classes that call other service classes? Should I start with the models or the use cases or is there some other approach?

I'm surprised that I can't find any examples on how to design this crucial part of the application.
 
kri shan
Ranch Hand
Posts: 1462
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why you are using Hibernate and JPA both for Persistance ?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have only UserRespository. User is an aggregate root.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:Why you are using Hibernate and JPA both for Persistance ?

JPA is specification/set of interfaces, Hibernate EntityManager is an implementation of JPA.
 
Daniel Näslund
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj Sathianpantarit wrote:You should have only UserRespository. User is an aggregate root.


Thank you for your quick feedback! That makes sense when I think about it. I'll go with a UserRepository.

 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Näslund wrote:
Kengkaj Sathianpantarit wrote:You should have only UserRespository. User is an aggregate root.


Thank you for your quick feedback! That makes sense when I think about it. I'll go with a UserRepository.

That's okay. Whenever you have time, I recommend to read Domain-Driven Design book.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj Sathianpantarit wrote:
Daniel Näslund wrote:
Kengkaj Sathianpantarit wrote:You should have only UserRespository. User is an aggregate root.


Thank you for your quick feedback! That makes sense when I think about it. I'll go with a UserRepository.

That's okay. Whenever you have time, I recommend to read Domain-Driven Design book.


A great book, and a great recommendation.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic