English 中文(简体)
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. 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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签