Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for service-dao pattern?

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...

like image 569
user1153321 Avatar asked Jul 14 '13 13:07

user1153321


1 Answers

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
like image 126
Fırat KÜÇÜK Avatar answered Oct 05 '22 23:10

Fırat KÜÇÜK