aspose file tools*
The moose likes Spring and the fly likes How design the service classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How design the service classes?" Watch "How design the service classes?" New topic
Author

How design the service classes?

Daniel Näslund
Greenhorn

Joined: Jul 14, 2009
Posts: 11
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

Joined: Apr 08, 2004
Posts: 1367
Why you are using Hibernate and JPA both for Persistance ?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You should have only UserRespository. User is an aggregate root.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
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

Joined: Jul 14, 2009
Posts: 11
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

Joined: Jul 05, 2005
Posts: 1936
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How design the service classes?
 
Similar Threads
Front controller and DAO, design question!
How to implement it with Acegi?
What else is Spring good for?
Use of Servlet Context?
Newbie Question: Java EE vs Tomcat+Hibernate+Struts