KyelJmD
KyelJmD

Reputation: 4732

Building Services Layer with Hibernate

I am working on a medium size application using Struts2 ,Hibernate, and Spring to manage the lifecycle of the sessionFactory. My question is that should I create one big service class that will provide all services for my whole application? or Should create one Service class per Model?

Let's say i have this One big service class

@Transactional
public class Services {
    // So Spring can inject the session factory
    SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory value) {
        sessionFactory = value;
    }

    // Shortcut for sessionFactory.getCurrentSession()
    public Session sess() {
        return sessionFactory.getCurrentSession();
    }

    public Event getEventById(long id) {
        return (Event) sess().load(Event.class, id);
    }

    public Person getPersonById(long id) {
        return (Person) sess().load(Person.class, id);
    }

    public void deleteEventById(long id) {
        sess().delete(getEventById(id));
    }

    public void deletePersonById(long id) {
        sess().delete(getPersonById(id));
    }

    public void createEvent(String name) {
        Event theEvent = new Event();
        theEvent.setName(name);
        sess().save(theEvent);
    }

    public void createPerson(String name) {
        Person p = new Person();
        p.setName(name);
        sess().save(p);
    }

    @SuppressWarnings("unchecked")
    public List<Event> getEvents() {
        return sess().createQuery("from Event").list();
    }

    @SuppressWarnings("unchecked")
    public List<Person> getPeople() {
        return sess().createQuery("from Person").list();
    }

    public void removePersonFromEvent(int personId, int eventId) {
        getEventById(eventId).getPeople().remove(getPersonById(personId));
    }

    public void addPersonToEvent(int personId, int eventId) {
        getEventById(eventId).getPeople().add(getPersonById(personId));
    }

      .....Some more services methods here
}

What would be the best approach for this? One Big Service Class for the whole application? or Different Service Class per model?

Upvotes: 1

Views: 580

Answers (2)

Carlos Gavidia-Calderon
Carlos Gavidia-Calderon

Reputation: 7253

Martin Fowler's book on Enterprise Patterns has some nice guidelines on building a Service Layer. In an extract of the book you can find this:

For a sufficiently small application, it may suffice to have but one abstraction, named after the application itself. In my experience larger applications are partitioned into several “subsystems,” each of which includes a complete vertical slice through the stack of architecture layers.

So there's no stone-written law in the Service Layer pattern. It feels to me that you're not in the case of the small application (your Services Class has too many methods), so everything reduces to identifying subsystems on your application. The book also recommend Service Layers associated to mayor abstractions (maybe EventService or PeopleService) or application behaviors (like EventManagementService); at final is up to you to determine the best code organization for your project.

Upvotes: 3

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Creating one big service for complete application will not make such sense and it will start creating a readability as well maintenance issue with the time as your application will grow.

Its always better to follow modular approach.As suggested by @Asag:

Create different services for different module and define your method related to service there.This will help you to separate the functionality as well will help anyone in future who will work on same application.

Upvotes: 0

Related Questions