且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 12:09:28

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.