Why hide DAO behind the service?

Having looked at a bunch of MVC-style web applications, I noticed that it is very important for them to have a very large service interface located in front of the business layer. The implementation of this interface usually includes a DAO group. So you have something like:

public class FoodServiceImpl implements FoodService {

  private FruitDAO fruitDAO;
  private VegetableDAO vegetableDAO;
  private MeatDAO meatDAO;

  // ... DAO injection stuff

  public List<Meat> getMeat() {
    return meatDAO.getMeat();
  }

  public void addMeat(Meat meat) {
    meatDAO.add(meat);  
  }

  public List<Fruit> getFruit() {
    return fruitDAO.getFruit();
  }

  // ... tons of methods that mostly just delegate to DAOs

}

Assuming your DAOs are not specific, first of all, why not just expose the DAO to the next level?

So, instead of:

// assume foodService is injected at runtime
public void controllerMethod() {
  foodService.getFruit();
  foodService.getMeat();
}

You just

// assume DAOs are injected at runtime
public void controllerMethod() {
  fruitDAO.getFruit();
  meatDAO.getMeat();
}

On the one hand, it would be nice to have whole guts of the application wrapped in one interface ... on the other hand, you can find a gigantic interface whose implementation does nothing more than delegate the DAO.

, DAO . , , DAO .

" " - , ?

+3
5

, , . DAO , "" .

, - . WS- *; -.

+8

, - CRUD API, -, . - (a.k.a. "Anemic Domain Model" ), - , / DAO ( "Rich Domain Model" ). , , , . , , , - ().

+6

: .. . FoodService , DAO. , ?

:

  • , .
  • .

, , , . DAO - - , Google BigTable - , .

, , HealthAndBeautyAids, . ( , , , , , , , ?)

+3

MVC , , , , , - . DAO, - - . , , :

if (meat.expirationDate-today >= 5 days)
   FridgeDAO.store(meat)
else if (meat.expirationDate-today <5 days)
   FreezerDAO.store(meat)
else 
   discard(meat)

DAO, - , - , ( -). - -.

+2

- .

, , API. , - (.. ..), , .

Remember that a programmer’s time is worth more than the hardware in a larger picture.

0
source

Source: https://habr.com/ru/post/1707822/


All Articles