What are the best practices for implementing models in the MVC pattern. Specifically, if I have "Users" do I need to implement 2 classes. One to manage all the users and one to manage a single user. So something like "Users" and "User"?
在MVC模式中实现模型的最佳实践是什么?具体来说,如果我有“用户”,我需要实现2个类。一个用于管理所有用户,另一个用于管理单个用户。那么“用户”和“用户”之类的东西呢?
I'm writing a Zend Framework app in php but this is more a general question.
我正在用PHP编写一个Zend Framework应用程序,但这是一个更普遍的问题。
3 个解决方案
#1
The model should be driven by the needs of the problem. So if you need to handle multiple users, then a class representing a collection of Users might be appropriate, yes. However, if you don't need it, don't write it! You may find that a simple array of User objects is sufficient for your purposes.
该模型应该由问题的需要驱动。因此,如果您需要处理多个用户,那么表示用户集合的类可能是合适的,是的。但是,如果您不需要它,请不要写它!您可能会发现一个简单的User对象数组就足以满足您的需要。
#2
That's going to be application and MVC implementation specific. You might well define a class collecting logically related classes, or you could define a static register on the user class. This is more of an OO question than MVC.
这将是应用程序和MVC实现特定的。您可能会定义一个收集逻辑相关类的类,或者您可以在用户类上定义静态寄存器。这比MVC更像是一个OO问题。
#3
I'll second Giraffe by saying the use of included collections is almost always better than trying to write your own.
我将第二个长颈鹿说,使用包含的集合几乎总是比尝试编写自己的集合更好。
But I think your original question could be reworded a little differently... "Do I need a separate class to manage users other than the User class?
但我认为你的原始问题可能会有所改变......“我是否需要一个单独的类来管理User类以外的用户?
I use a static factory class to build all of my users and save them back to the database again. I'm of the opinion that your model classes need to be as dumbed down as possible and that you use heavy controller classes to do all of the work to the model classes.
我使用静态工厂类来构建我的所有用户并再次将它们保存回数据库。我认为你的模型类需要尽可能地愚蠢,并且你使用繁重的控制器类来完成模型类的所有工作。
#1
The model should be driven by the needs of the problem. So if you need to handle multiple users, then a class representing a collection of Users might be appropriate, yes. However, if you don't need it, don't write it! You may find that a simple array of User objects is sufficient for your purposes.
该模型应该由问题的需要驱动。因此,如果您需要处理多个用户,那么表示用户集合的类可能是合适的,是的。但是,如果您不需要它,请不要写它!您可能会发现一个简单的User对象数组就足以满足您的需要。
#2
That's going to be application and MVC implementation specific. You might well define a class collecting logically related classes, or you could define a static register on the user class. This is more of an OO question than MVC.
这将是应用程序和MVC实现特定的。您可能会定义一个收集逻辑相关类的类,或者您可以在用户类上定义静态寄存器。这比MVC更像是一个OO问题。
#3
I'll second Giraffe by saying the use of included collections is almost always better than trying to write your own.
我将第二个长颈鹿说,使用包含的集合几乎总是比尝试编写自己的集合更好。
But I think your original question could be reworded a little differently... "Do I need a separate class to manage users other than the User class?
但我认为你的原始问题可能会有所改变......“我是否需要一个单独的类来管理User类以外的用户?
I use a static factory class to build all of my users and save them back to the database again. I'm of the opinion that your model classes need to be as dumbed down as possible and that you use heavy controller classes to do all of the work to the model classes.
我使用静态工厂类来构建我的所有用户并再次将它们保存回数据库。我认为你的模型类需要尽可能地愚蠢,并且你使用繁重的控制器类来完成模型类的所有工作。