|

楼主 |
发表于 11-5-2007 07:19 AM
|
显示全部楼层
CakePHP
首先先谢谢大家的指教,这几天我还是有去看看别人怎么执行MVC Pattern的~可是有一部分跟我所知道的有点出入,比如以下的:
Controller
A controller is used to manage the logic for a certain section of your application. Most commonly, controllers are used to manage the logic for a single model. For example, if you were building a site that manages a video collection, you might have a VideoController and a RentalController managing your videos and rentals, respectively. In Cake, controller names are always plural.
出处:http://manual.cakephp.org/chapter/controllers
这里的logic是不是domain/business logic,可是business logic不是应该在model里面的吗? |
|
|
|
|
|
|
|
发表于 11-5-2007 12:31 PM
|
显示全部楼层
这里的logic并不是Business Logic。Controller是处理Request的角色,所以logic是指Controller本身如何处理Request的logic,并不是所谓的Business Logic。例如,一个用户登入一个网站时,LoginController就会检查请求是否正确;检查Session/Cookie里的数据;在决定用户的权限;将用户资料交给model作处理;决定view;检查是否用缓存;等等。这些都是所谓的Controller Logic。 |
|
|
|
|
|
|
|
发表于 12-5-2007 04:12 PM
|
显示全部楼层
Model Class 通长没有任何逻辑的。自有 insert/update/delete/retrieve Data
UserDetailView.java (extends ViewObject) Controller Class
public void processView (HttpServletRequest request, HttpServletResponse response)
{
UserValidator userValidator = newUserValidator();
userValidator.validateInput(request ); //validateInsert, validateUpdate, validateDelete
UserModel userModel = new UserModel(); Model Class
UserDetail userDetail = userModel.findByPrimaryKey(request.getParameter("userId" ) );
request.setAttribute("UserName", userDetail.getUsername() );
request.setAttribute("DateOfBirth", userDetail.getDateOfBirth );
//process others logic here or call others class/method to process the logic
//you can also set the result page here if needed
request.setAttribute("jspPage" "UserDetailView.jsp" );
}
View.jsp 请看回上部的Code
<%
pageContext.include(request.getAttribute("jspPage" );
%>
UserDetail.java (Entity Class / POJO (plain old java object) )
private String UserName = "";
private String DateOfBirth = "";
//getter & setter (e.g getUserName, SetUserName, getDateOfBirth, setDateOfBirth)
UserModel.java (Model Class)
public UserDetail findByPrimaryKey(userName)
{
String sql = " select * from UserDetail where UserName = " + userName;
//connect to database, open result set
UserDetail userDetail = new UserDetail();
userDetail.setUserName(rs.getString("UserName")); //get result from database
userDetail.setDateOfBirth(rs.getString("DateOfBirth"));
return userDetail;
}
//updateByPrimaryKey
//deleteByPrimaryKey
//listAllUsers
[ 本帖最后由 hkloke2000 于 12-5-2007 04:45 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 12-5-2007 06:46 PM
|
显示全部楼层
苦瓜汤
这里的logic并不是Business Logic。Controller是处理Request的角色,所以logic是指Controller本身如何处理Request的logic,并不是所谓的Business Logic。例如,一个用户登入一个网站时,LoginController就会检查请求是否正确;检查Session/Cookie里的数据;在决定用户的权限;将用户资料交给model作处理;决定view;检查是否用缓存;等等。这些都是所谓的Controller Logic。
基本上我做的跟你差不多一样
原帖由 hkloke2000 于 12-5-2007 04:12 PM 发表
Model Class 通长没有任何逻辑的。自有 insert/update/delete/retrieve Data
UserDetailView.java (extends ViewObject) Controller Class
public void processView (HttpServletRequest request, ...
基本上我做的,跟你目前的差不多一样(也是有entity class,然后model 透过data access object connect database,view 透过model拿content),有些不同的是,我的validation都在我的model内
原因:
出处:http://manual.cakephp.org/chapter/models
What is a model?
What does it do? It separates domain logic from the presentation, isolating application logic.
A model is generally an access point to the database, and more specifically, to a certain table in the database. By default, each model uses the table who's name is plural of its own, i.e. a 'User' model uses the 'users' table. Models can also contain data validation rules, association information, and methods specific to the table it uses. Here's what a simple User model might look like in Cake:
还有这个:http://java.sun.com/blueprints/patterns/MVC-detailed.html
- Model - The model represents enterprise data and the business rules that govern access to and updates of this data. Often the model serves as a software approximation to a real-world process, so simple real-world modeling techniques apply when defining the model.
- View -The view renders the contents of a model. It accesses enterprise data through the model and specifies how that data should be presented. It is the view's responsibility to maintain consistency in its presentation when the model changes. This can be achieved by using a push model, where the view registers itself with the model for change notifications, or a pull model, where the view is responsible for calling the model when it needs to retrieve the most current data.
- Controller - The controller translates interactions with the view into actions to be performed by the model. In a stand-alone GUI client, user interactions could be button clicks or menu selections, whereas in a Web application, they appear as GET and POST HTTP requests. The actions performed by the model include activating business processes or changing the state of the model. Based on the user interactions and the outcome of the model actions, the controller responds by selecting an appropriate view.
我的未完成script的page execution time已经差不多.1x秒,觉得有点慢……
[ 本帖最后由 V4ndrake 于 12-5-2007 06:47 PM 编辑 ] |
|
|
|
|
|
|
| |
本周最热论坛帖子
|