ilovetolearn
ilovetolearn

Reputation: 2060

struts controller and service layer?

The existing application I am working on, the Action classes will interact with the DAO classes directly.

Would it be better the Action classes, will invoke Service layer classes(BusinessFacade) in turn which will invoke the DAO?

Is it better to develop a layer between the Action classes and the DAO layer?

Upvotes: 0

Views: 2007

Answers (2)

Jason
Jason

Reputation: 4130

The purpose of the Service layer between your controller and DAO layers is to encapsulate more complex business logic. This allows your controller to be dumb and your DAO to concentrate on the task of interacting with the backend data repository(be that database, flat file, etc).

Here's an example of a service-layer method using Spring Security annotations:

@PreAuthorize("hasRole('ROLE_ADMIN'));
public Person getPerson(long personId) {
    Person person = personDAO.getById(personId);
    if (person.getPosition().equals("MANAGER") {
       log.debug("Manager's information requested");
    }
}

This is doing two things related to business logic:

  1. Making sure that the user has a role necessary to run the function.
  2. Logging that the information for a manager has been accessed.

These activities have no place in a controller, and add unnecessary complexity to a DAO.

Upvotes: 0

Andrea Colleoni
Andrea Colleoni

Reputation: 6021

It's better because in that way you can separate business logic (the logic not related to naviagtion matters) for a possible reuse.

If you separate you'll be able to reuse business logic even in a non web application.

The data access layer is intended as the layer where objects are persisted to, and restored from, an abstract storage and actually, specially in case of RDBMS persistence, may be implemented well by ORMs, so often you can see a business logic layer with an ORM to persist objects. I prefer to merge business logic with an ORM, in this case rather then with a front end framework.

Upvotes: 2

Related Questions