-
Nov 28th, 2008, 09:55 AM
#1
Merging service layer and DAO layer into single layer
I would like to here some opinion on following topic.
I used to build my application with service layer and DAO layer - quite standard. Service layer contains interfaces (e.g. ProductService) and their implementations (e.g. ProductServiceImpl). It has dependency on DAO layer, consisting of interfaces (e.g. ProductDao) and implementations (e.g. ProductDaoImpl). This worked very well especially with JdbcTemplate-based DAOs, where the service sometimes had to call several DAOs for completing the task (call ProductDao, OrderDao, UserDao).
With Spring 2.5 stereotypes I would mark all service implementation classes with @Service, and all Dao implementation classes with @Repository, which gives me DataAccessExceptions translation for Dao classes.
However, now, when I'm usually using JPA-based persistence, I observe that in many cases the service classes simply pass all the calls to Dao, almost untouched. So I have the "saveProduct" method in service, which starts the transaction (by AOP) and simply calls the "saveProduct" method on Dao, which calls entityManager.persist(). This way separation of service and dao layer only complicates the life of developer, because two levels of inerfaces means more work to do to come to the source which really does the job, and service layer does not add any value.
So the solution would be to throw away one layer. Which one? I saw that the samples of Spring Web Flow do not have the separate Dao level, only service layer. Service has EntityManager injected, and directly calls entityManager.persist() in such a case. I would say that the EntityManager plays in fact the role of DAO here. This sounds reasonable and makes the code cleaner.
However, now I don't have the DataSourceExceptions translation, which was earlier done by @Repository adnotation on Dao layer. This is not crucial, but I like this DataSourceException abstraction offered by Spring. Can I combine it with this approach? I can mark my service layer with @Repository instead of @Service, but this is probably incorrect semantically. Or should I mark it with both @Service and @Repository?
What's your opinion on this?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules