MVC - 一个模型应该承担多少责任?
MVC - How many responsibilities should a model possess?
我正在学习 MVC(实际上是 OOP)并在网上找到非常适合基本概述的示例,但是当我深入了解复杂项目的具体细节时,我有点不知所措。
我在下面列出了一个基本的 'User' 模型。我想知道什么时候模型有太多责任?当这种情况发生时,有哪些选择?如果你有子模型 - 例如UserLoginModel 而不是包罗万象的用户模型?
PS 似乎无法粘贴代码,因此它将全部保留在代码块中 - 对此感到抱歉。
//USER MODEL
class 用户模型
{
private $userId;
private $userName;
private $address
private $email
private $password;
/**
* Some instance of a DB class
*/
private $db;
/**
* Some encryption class which can generate cipher text
*/
private $encryption;
public function __construct($databaseClass){
//some database layer like a table gateway
$this->db = $databaseClass;
//some encryption class which can be used to test a password against a cipher
$this->enc = $encryptionClass;
}
/*
GETTERS/SETTERS...
*/
public function findUser($username){
// 1. Find user in database
// 2. Map database array to properties
// 3. Return boolean
}
public function validateLogin($password){
// 1. Turn $pasword into cipher text using $this->enc class
// 2. Match User object password
// 3. Return boolean
}
public function updateUser($data){
//some code for updating the user
}
public function deleteUser($user){
//code for deleting user
}
}
//认证控制器-接收请求/AuthController/doLogin
class 授权控制器
{
public function doLogin(){
$db = new databaseClass(); //e.g. a tablegateway
$encryptionClass = new encryptionClass(); //some class which generates ciphers
$user = new UserModel($db);
if($user->findUser($_POST['username'])){
$loginSuccess = $user->validateLogin($_POST['password']);
//do stuff
}
}
}
模型是设计模式的核心组成部分。它根据问题域表达应用程序的行为,独立于用户界面。它直接管理应用程序的数据、逻辑和规则。
模型中必须具备的所有功能和职责。
控制器接受输入并将其转换为模型或视图的命令。它将通过模型获得函数 and/or 方法。
View 可以是信息的任何输出表示,也可以是用户输入的数据。
Example of MVC Class Diagram
StudentView 将是一个视图 class,它可以在控制台上打印学生详细信息,而 StudentController 是控制器 class 负责将数据存储在 Student 对象中并相应地更新视图 StudentView。
我正在学习 MVC(实际上是 OOP)并在网上找到非常适合基本概述的示例,但是当我深入了解复杂项目的具体细节时,我有点不知所措。
我在下面列出了一个基本的 'User' 模型。我想知道什么时候模型有太多责任?当这种情况发生时,有哪些选择?如果你有子模型 - 例如UserLoginModel 而不是包罗万象的用户模型?
PS 似乎无法粘贴代码,因此它将全部保留在代码块中 - 对此感到抱歉。
//USER MODEL
class 用户模型 {
private $userId;
private $userName;
private $address
private $email
private $password;
/**
* Some instance of a DB class
*/
private $db;
/**
* Some encryption class which can generate cipher text
*/
private $encryption;
public function __construct($databaseClass){
//some database layer like a table gateway
$this->db = $databaseClass;
//some encryption class which can be used to test a password against a cipher
$this->enc = $encryptionClass;
}
/*
GETTERS/SETTERS...
*/
public function findUser($username){
// 1. Find user in database
// 2. Map database array to properties
// 3. Return boolean
}
public function validateLogin($password){
// 1. Turn $pasword into cipher text using $this->enc class
// 2. Match User object password
// 3. Return boolean
}
public function updateUser($data){
//some code for updating the user
}
public function deleteUser($user){
//code for deleting user
}
}
//认证控制器-接收请求/AuthController/doLogin
class 授权控制器 {
public function doLogin(){
$db = new databaseClass(); //e.g. a tablegateway
$encryptionClass = new encryptionClass(); //some class which generates ciphers
$user = new UserModel($db);
if($user->findUser($_POST['username'])){
$loginSuccess = $user->validateLogin($_POST['password']);
//do stuff
}
}
}
模型是设计模式的核心组成部分。它根据问题域表达应用程序的行为,独立于用户界面。它直接管理应用程序的数据、逻辑和规则。
模型中必须具备的所有功能和职责。
控制器接受输入并将其转换为模型或视图的命令。它将通过模型获得函数 and/or 方法。
View 可以是信息的任何输出表示,也可以是用户输入的数据。
Example of MVC Class Diagram
StudentView 将是一个视图 class,它可以在控制台上打印学生详细信息,而 StudentController 是控制器 class 负责将数据存储在 Student 对象中并相应地更新视图 StudentView。