MVC中数据访问层和模型的区别

时间:2023-01-03
本文介绍了MVC中数据访问层和模型的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经在几个 Web 应用程序中实现了我认为相当不错的 MVC 表示,但是自从加入了 Crackeroverflow,我发现我最初的定义可能有点简单,因此我真的很想澄清一下数据访问层与网络应用程序的模型或领域层之间的差异.

I have implemented what I thought was a pretty decent representation of MVC in several web applications but since having joined crackoverflow, I'm finding that perhaps my initial definitions were a bit simplistic and thus I'd really like some clarification on the differences between the Data Access Layer and the Model or Domain Layer of a web application.

对于上下文,我目前使用数据访问对象为对象表示的表中的单个记录实现 CRUD 函数,以及返回一个对象的 get() 函数,该对象允许我遍历所有对象满足 get() 函数的标准.

For context, I currently use data access objects that implement the CRUD functions for a single record in the table that the object represents as well as a get() function that returns an object that allows me to iterate through all of the objects that met the criteria for the get() function.

这些数据访问对象直接从包含我的业务逻辑的控制器脚本中引用.

These data access objects are referenced directly from the controller scripts which contain my business logic.

如果重要的话,我正在使用 PHP 和 MySQL,但对可能用其他语言编码的建议感兴趣.

If it matters, I'm working in PHP and MySQL but am interested in suggestions that might be coded in other languages.

更新: 对于更具体的示例,我有一个名为 user 的表(这里的约定是单数表名),其中包含诸如电子邮件地址、活动状态、用户名、密码、哪个公司等信息它们属于等等.这个基本对象在代码中看起来像这样:

UPDATE: For a more specific example, I have table called user (the convention here is singular table names) which holds such information as email address, active state, user name, password, which company they belong to, etc. This basic object would look like this in code:

class User implements DataAccessObject
{
     protected $user_id;
     protected $email;
     protected $username;
     protected $password;
     protected $company_id;
     protected $active // Bool that holds either a 0 or 1

     public function __construct ( $user_id ) // Uses Primary Key to know which record to construct
     {
          $sql = //Sql to get this information from the database.

          // Code necessary to assign member variables their values from the query.
     }

     public function insert(){}
     public function update(){}
     public function delete(){}
     public static function get($filters, $orderVals, $limit){}

     // An object such as user might also contain the following function definition
     public static function login($username, $password){}
}

听起来我可能已经将 DAO 层和模型层混为一谈,将任何现实世界类型的功能(例如用户登录)与数据访问功能结合在一起.

It sounds like I might have bastardized the DAO Layer and Model layer into a simplified form that combines both any real-world type functions (such as login for a user) with the data access functions.

推荐答案

model 类作为真实世界实体的良好、干净、高保真模型独立存在.如果它是一个业务领域,它们可能是客户、计划、产品、付款等等.您的应用程序使用这些类.这个想法是你的应用程序是一个真实世界处理域对象的模型.您的应用程序可以具有看起来像人们真正使用的动词的方法函数,而这些方法函数的实现看起来像是对真实世界对象的真实描述.

The model classes stand alone as a good, clean, high-fidelity model of real-world entities. If it's a business domain, they might be customers, plans, products, payments, all that kind of stuff. Your application works with these classes. The idea is that your application is a model of real-world handling of the domain objects. Your application can have method functions that look like verbs people really do, and the implementation of those method functions looks like a real-world description of real-world objects.

重要:这(理想情况下)独立于大多数技术考虑.它是您可以定义的域对象的最纯粹模型.[是的,你确实有外键查找问题,是的,你必须让你的模型对象知道一些数据访问组件,以便模型对象可以找到仅给定外键而不是实际对象的其他对象.一个好的 ORM 层会为您处理这个导航问题.]

Important: This is (ideally) independent of most technical considerations. It's the purest model of the domain objects you can define. [Yes, you do have foreign-key lookup issues, and yes, you do have to make your model objects aware of some data access components so that a model object can find each other objects given just foreign keys instead of the actual object. A good ORM layer handles this navigation issue for you.]

一个充满 SQL 的模型不是一个好的模型.现实世界也不全是 SQL.发票是包含一些名称、地址和项目、发货日期以及诸如此类的一堆内容的文档.

A model full of SQL isn't a good model. The real world isn't full of SQL, either. An invoice is a document with some names and addresses and items, and a shipping date, and a bunch of stuff like that.

access 类处理持久存储.这通常包括将模型对象映射到关系数据库表.面向 SQL 的数据访问层将从关系数据库重建您的模型,并将您的模型保存在关系数据库中.YAML 数据访问层将从您的模型读取和写入 YAML 文件.

The access classes handles persistent storage. This usually includes mapping your model objects to relational database tables. A SQL-oriented data access layer would reconstruct your model from a relational database and persist your model in a relational database. A YAML data access layer would read and write YAML files from your model.

有时,对象关系映射 (ORM) 设计模式用于在 SQL 的世界和您的模型之间进行清晰的分离.有时,数据访问对象 (DAO) 会处理 SQL 和模型之间的这种分离.一个 ORM 或 DAO 对象可以装满 SQL.

Sometimes, the object-relational mapping (ORM) design pattern is used to make a clean separation between SQL's world and your model. Sometimes a Data Access Object (DAO) handles this separation between SQL and model. A ORM or DAO object can be packed full of SQL.

确实,当您更改数据库产品时,唯一的更改是在 DAO 或 ORM 中.模型永远不会改变,因为它独立于 SQL、YAML、JSON、XML 或其他一些序列化技术.

Indeed, when you change database products, the only change is in the DAO or ORM. The model never changes, because it is independent of SQL, YAML, JSON, XML or some other serialization technique.

如果您的 DAO 创建并保留模型对象,我认为您已经相当好地实现了 MVC 的模型部分.您可以查看 ORM 包以获取有关最新技术的更多想法.我本人是 iBatis 的粉丝.

If your DAO creates and persists model objects, I think you've got the model parts of MVC implemented reasonably well. You can look at ORM packages to get additional ideas of the state of the art. I'm a fan of iBatis, myself.

但仅占整个 MVC 世界观的 1/3.而且——当然——纯粹主义者会告诉你 MVC 只是桌面或只是闲聊或与 MVC 的常见 Web 实现不同.

But's only 1/3 of the whole MVC world-view. And -- of course -- purists will tell you that MVC is only desktop or only smalltalk or different from the common web implementation of MVC.

这篇关于MVC中数据访问层和模型的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:MVC 控制器的示例 下一篇:基本 MVC (PHP) 结构

相关文章

最新文章