在 MVC 中,ORM 是否代表模型? [英] In MVC, does an ORM represent the model?

查看:17
本文介绍了在 MVC 中,ORM 是否代表模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MVC 中,ORM 与模型相同还是只是模型设计的一种方式?换句话说,只要你得到数据,模型"并不关心你如何得到数据.或者,模型"是否意味着我的代码中不再像表单背后的代码那样有一堆 SQL 语句?还有什么?

In MVC, is the ORM the same as the model or just a way the model can be designed? In other words, the "model" doesn't care how you get data as long as you get it. Or, does "model" imply that I no longer have a bunch of SQL statements in my code like in code behind forms? Something else?

谢谢.

推荐答案

不,ORM 是将基于代码的模型映射到数据库的东西,反之亦然.

No, the ORM is the thing that maps a code-based model to your database and vice versa.

对于基本的 CRUD 应用程序,您的代码模型实际上只是 DTO,代表数据库,您正在加载、编辑和保存它们,这就是您使用它的方式.如果你有一个适当的"域模型,那么它有点复杂,因为理想情况下,您希望将来自数据库表形状的领域模型类.

For basic CRUD apps, where your model in code is literally just DTOs that represent the database and you're loading, editing, and saving them, that's how you'd use it. If you do have a "proper" Domain Model, then it's a bit more complex because ideally you'd want to decouple the shape of the Domain Model classes from the shape of the database tables.

详细来说,您将在代码中创建模型来表示域模型(即问题域的各种元素),构建某种纪念品";是纯 DTO 的类,您可以将领域模型类从/转换为.然后配置一个 ORM(对象关系映射器)将这些备忘录 DTO 映射到数据库.IE.生成 SQL 语句,根据您提供的模型对象更新数据库.

To elaborate, you would create your model in your code to represent the Domain Model (i.e. the various elements of your problem domain), build some sort of "memento" classes that are pure DTOs that you can convert your Domain Model classes from/into. Then configure an ORM (object relational mapper) to map those memento DTOs to a database. I.e. Generate SQL statements that will update the database based on the model objects you give to it.

我能理解一些混淆,因为有工具(LINQ to SQL 是一个),它实际上为您在设计器中生成模型类.这不是纯粹的 ORM,例如 NHibernate,您可以在其中提供 ORM 纯旧对象和一些它使用的映射配置(通常与反射结合使用)为数据库自动生成 SQL 语句.您可以使用 EF Code First 将域模型直接映射到数据库,但我认为最终当您尝试对其中一个进行更改时,它可能会变得混乱.

I can understand some confusion, because there are tools (LINQ to SQL being one) that actually generate model classes in a designer for you. This isn't pure ORM, like NHibernate, where you provide the ORM plain old objects and some mapping configuration that it uses (often in conjunction with reflection) to automatically generate the SQL statements for the database. You could possibly get away with using EF Code First to map a Domain Model directly to the database, but I think in the end it may get messy as you try to make changes to one or the other.

这篇关于在 MVC 中,ORM 是否代表模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆