What is the best practice for service-dao pattern?
duplication, genericdao, service, spring
Solution
For my projects i use these service and DAO layers. I don't know it's a best practice or not.
This is a sample create operation levels:
[View Layer]
* Simple HTML form or AJAX request
|
| User submits create form. Browser sends POST data
| to controller.
|
[User Controller]
* Authentication and authorization. @Security annotations can be for method security.
* Controller tries to bind POST data to UserCreateForm. If can't Validation exception occurs.
* Validates bind data according to validation annotations. @Required ...
|
| (UserCreateForm) is POJO class which has validation annotations.
| It is similar but different from domain objects.
|
[User Controller]
* Logs errors via Logging API (logback, slf4j, log4j ...)
* Copies form values from UserCreateForm to User domain object
* Calls service methods.
* Passes messages and model objects to desired view page.
|
| (User) is POJO class. called domain object, contains ORM annotations if using JPA
| or hibernate. It is similar but different from form/command objects. It can be
| generated automatically by tools (IDE, hibernate tools ...)
|
[UserService & UserServiceImpl]
* Calls multiple DAO methods in one transaction. If an error occurs. rolls back.
* Contains business logic
* Doesn't know the database technology.
|
| (User) domain object.
|
[UserDAO & HibernateUserDAOImpl || JpaUserDAOImpl || OracleJdbcDAOImpl ...]
* DAO layer knows the persistence technology
* Operations are atomic
Problem
let's think about a simple User insert operation. My Spring related classes to do this job are `UserController`, `UserService`, `UserServiceImpl`, `UserDAO`, `UserDAOImpl`. At controller side i call `userService.insert(new User())` and in `userService.insert()` method i call `userDAO.insert(user)`. I think there is a method duplication at this pattern. Is there a way to avoid method duplication? May be my coding is faulty. I wait your replies, experiences... Thank in advance...