0

I am developing a Web App in Spring and hibernate. I am loading entities in Database.Authors,books,Publication etc are my entities which are getting loaded from excel. I have mode one Entity Load Service interface and then I have its Implementations for every entity. My Service calls DAO implementations. Now I am struggling to find if the below mentioned code violates SRP. Also I am always confused about how to decide responsibility of the class because any class can have many methods and each method can be performing something different.So should they be separated in different class?.take in my case I have 4 methods each performing different task.So I end up with 4 different class for each method.If I follow this approach(which I know is wrong) then I will always end up in classes having single method. Also,sometimes I feel that I going away from domain driven design because I am refracting the code on the basis of functionality.

Any suggestions on how to decide what the responsibility is from the perspective a class? SRP stands for single responsibility principle.And I am really confused in identifying this responsibility.

public interface EntitiesLoadService {

    public void loadEntities(Object o);
    public  void deleteEntities(Object o);
public List getEntities();
public Object getEntity(Object o);

}

Service Implementation

@Service("authorLoadService")
@Transactional
public class AuthorEntityLoadService implements EntitiesLoadService{

    private AuthorDAO authorDao;





    @Autowired
    @Qualifier("authorDAO")
    public void setAuthorDao(AuthorDAO authorDao) {
        this.authorDao = authorDao;
    }

    @Override
    public void deleteEntities(Object o) {
        // TODO Auto-generated method stub

    }

    @Override
    public void loadEntities(Object o) {
        Set<author_pojo> author=(Set<author_pojo>)o;
        Iterator<author_pojo> itr=author.iterator();

        while (itr.hasNext()) {
            author_pojo authorPojo = (author_pojo) itr.next();
            authorDao.save(authorPojo);

        }


    }

    @Override
    @Transactional(readOnly=true)
    public List getEntities() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    @Transactional(readOnly=true)
    public Object getEntity(Object o) {
        String author=(String)o;
    author_pojo fetAuthor=authorDao.findOneByName(author);

        return fetAuthor;
    }

}
beinghuman
  • 2,088
  • 7
  • 27
  • 36

2 Answers2

2

You have AuthorDAO which is the class that should be doing all interactions with the persistence layer, ex. a database.

It isn't obvious in your example because your AuthorEntityLoadService has similar methods which just delegate to the DAO layer.

As your project and requirements grow, you will see that more methods are required for this class. These methods will be responsible for doing more than just CRUD operations on the DAO layer. They might need to interact with other services, internal or external. They might need to do multiple DAO calls.

The Single Responsibility in this case is to provide services for interacting with AuthorEntity instances.

It is on of many correct ways of implementing what you are proposing.


More specifically, my opinion on

Also I am always confused about how to decide responsibility of the class because any class can have many methods and each method can be performing something different.So should they be separated in different class?

Just because you have many methods doing different things, doesn't mean the responsibility isn't shared. AuthorEntityLoadService which I would just call AuthorEntityService manages AuthorEntity instances at the service layer. Image if you had one Class with one method for each of create, update, retrieve, delete an AuthorEntity. That wouldn't make much sense.

And on

Any suggestions on how to decide what the responsibility is from the perspective a class?

As further reading, try http://java.dzone.com/articles/defining-class-responsibility

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • you have got the nerves correctly.The class is not evolved much at the moment.But,Could you please enlighten me on the text in bold in the question? – beinghuman Sep 16 '13 at 21:40
  • you said "The Single Responsibility in this case is to provide services for interacting with AuthorEntity instances."...So this means that if there any many methods in the class which are responsible for some kind of interaction with only authorEntity,then all those methods are confirming to one original responsibility(interacting with the class) and if later more methods are added to interact with other services,controller,external system etc..then those methods will be having different responsibility from the original responsibility of the Class.Am I right? – beinghuman Sep 16 '13 at 21:48
  • @Niks I've added some more content, but I feel I'm just repeating myself. As for your comment, although the class is interacting with other services, its goal is still the same: to manage some `AuthorEntity`. – Sotirios Delimanolis Sep 16 '13 at 22:15
2

Typically, in this type of n-tier architecture, your service layer is meant to provide an API of transactional (or otherwise resource-dependent) operations. The implementation of each service can use whatever resource-specific dependencies (like DAOs for a particular datasource) it needs, but it allows the service consumer to remain agnostic of these specific dependencies or resources.

So even if your service is just delegating to its resource-specific dependencies, it doesn't violate SRP because its responsibility is to define a resource-agnostic API (so that the consumer doesn't need to know all the resource-specific stuff) that specifies atomic operations (transactional if necessary).

superEb
  • 5,613
  • 35
  • 38
  • SuperEb..can you please have a look at the text in bold in the question and my comments in the answer of Sotirios. – beinghuman Sep 16 '13 at 21:49
  • In my opinion, your service layer should define the boundaries for transactions (so that each interface method defines an atomic operation); other than that, I would say how you choose to organize business logic into classes and methods in that layer is mostly subjective and should be based on making it as simple as possible to write application code. Typically, as Sotirios alludes, each service interface/class should be based on specific vertical modules/features of the application domain - so one service to handle `Author` operations, another for `Book` operations, and so on. – superEb Sep 17 '13 at 00:32