Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Model view controller 查询语言在MVC模式中位于何处?_Model View Controller_Design Patterns - Fatal编程技术网

Model view controller 查询语言在MVC模式中位于何处?

Model view controller 查询语言在MVC模式中位于何处?,model-view-controller,design-patterns,Model View Controller,Design Patterns,我会假设,由于查询语言(通常)位于控制器中,因此它属于该组件,但如果我扮演魔鬼代言人的角色,我会认为查询语言是在模型域内执行的,并且与该组件紧密耦合,因此它也可能是该组件的一部分 有人知道答案吗?有没有一个直截了当的答案,或者它是特定于技术的?这两种方法都是合法的实现方法。问题是您需要什么以及如何向用户公开您的应用程序。在(我再次指出,我真的很喜欢引用这本书;)中,它们提供了两种实现方法: 在模型中实现所有应用程序逻辑(以及查询语言)。这使得您的域模型非常特定于用例(您不能那么容易地重用它,因

我会假设,由于查询语言(通常)位于控制器中,因此它属于该组件,但如果我扮演魔鬼代言人的角色,我会认为查询语言是在模型域内执行的,并且与该组件紧密耦合,因此它也可能是该组件的一部分


有人知道答案吗?有没有一个直截了当的答案,或者它是特定于技术的?

这两种方法都是合法的实现方法。问题是您需要什么以及如何向用户公开您的应用程序。在(我再次指出,我真的很喜欢引用这本书;)中,它们提供了两种实现方法:

  • 在模型中实现所有应用程序逻辑(以及查询语言)。这使得您的域模型非常特定于用例(您不能那么容易地重用它,因为它已经具有一些应用程序逻辑和对正在使用的后端存储的依赖性),但它可能更适合于复杂的域逻辑
  • 将应用程序逻辑放在另一层(可以是MVC模式中控制器使用的逻辑,也可以是控制器直接使用的逻辑)。这通常使您的模型对象成为普通的数据容器,您不需要向用户公开整个(可能很复杂)域模型结构(您可以使界面尽可能简单)
作为代码示例:

// This can also be your controller
class UserService
{
    void save(User user)
    {
        user.password = md5(user.password)
        // Do the save query like "INSER INTO users"... or some ORM stuff
    }
}

class User
{
    String username;
    String password;

    // The following methods might be added if you put your application logic in the domain model
    void setPassword(String password)
    {
        // Creates a dependency to the hashing algorithm used
        this.password = md5(password)
    }

    void save()
    {
        // This generates a dependency to your backend even though
        // the user object doesn't need to know to which backend it gets saved
        // INSET INTO users
    }
}