开发者

How are Models (in MVC) and DAOs supposed to interact?

How are models and DAOs supposed to interact? I'm in the process of putting together a simple login module and I'm unsure where to put the "business logic." If I put the logic with the data in the model, how would I access the logic?

Currently, I have:

  1. A controller that receives form data
  2. A model that is a simple reflection of a db table
  3. 开发者_JS百科
  4. A DAO that using hibernate performs a simple select query, based on the form parameters received by the controller.


The controller has to find/load the business object matching the request and execute it. The Strategy Pattern is useful in this. The business object on its turn has to get a handle of the DAO and the model to do the process.

E.g. (pseudo, inside a front controller servlet)

public void process(request, response) {
    View view = new View(request, response);
    Action action = ActionFactory.getAction(request);
    if (action != null) action.execute(view);
    view.navigate();
}


Put it in the controller. Controller is like a heart of your application where most of the logic is written.

It's not good to put the business logic at Model Level.


You might want to take a look at some of the answers to Best designs for breaking down business logic and data layer when they seem to overlap?


A typical layering would be like this:

  • Controller
  • Model
    • Business Layer
    • Data Layer (DAO)

The model would contain both the business and data layers while keeping those layers loosely coupled to the extent that makes sense. The controller would access the model, which in most cases should be the business layer. It's preferable that the controller not have direct access to the data layer, but it happens quite a bit in smaller apps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜