MVC 模型与 WCF 数据契约设计

发布于 2024-12-11 05:25:29 字数 290 浏览 0 评论 0原文

我是 MVC 新手,我想向那些可能有一些经验的人咨询一下。

如果我理解正确的话,MVC 中的模型用于封装视图逻辑,即您希望在视图级别发生的行为。对于相同类型的数据(例如 EF 模型、WCF DataContract),您实际上可能有 2 或 3 个不同的模型来支持该数据的编辑和/或呈现。

我担心 DRY,但我现在认为 WCF 合约将“翻译”为给定视图的模型对象是正确的。实际上,模型将具有来自 WCF DataContract 的构造函数,并且它们将具有从模型中的数据生成 DataContract 的方法?

建议?

I'm new to MVC and I wanted to check with those who might have some experience here.

If I understand correctly, the model in MVC is used to encapsulate view logic, behaviour that you want to occur at the view level. For the same kind of data (eg. EF model, WCF DataContract) you may actually have 2 or 3 different models to support editing and/or presentation of that data.

I was worried about DRY, but I'm now thinking that it's correct that the WCF contract will be "translated" into the model objects for the given view. Effectively the models would have a constructor from a WCF DataContract and they would have a method to produce the DataContract from the data in the model?

Advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亣腦蒛氧 2024-12-18 05:25:29

“MVC 中的模型用于封装视图逻辑,以及您希望在视图级别发生的行为” - 我不同意这一点。该模型应该是您的业务概念的表示。 视图涉及表示并呈现您的模型(例如作为 HTML 页面)。

模型的形式可能是具有行为的域对象,或者,如果这是由另一个服务封装的,则可能是该服务返回的数据契约。视图本身应该很大程度上不知道传递给它的模型的“类型”。它应该只关心生成渲染所需的相关数据是否存在。

请注意,MVC 还具有 ViewModel 的概念,它是模型中项目的表示,专门设计用于包含特定 View 渲染所需的所有信息,请参阅我的答案 此处了解更多相关信息。

"the model in MVC is used to encapsulate view logic, behaviour that you want to occur at the view level" - I would disagree with this. The Model should be the representation of your business concepts. The View is concerned with presentation and renders your Model (e.g. as an HTML page).

The form of your model may be domain objects with behaviours or, if this is encapsulated by another service, the data contracts which that service returns. The view itself should be largely agnostic to what 'type' of model is being passed to it. It should only care that the relevant data it requires to produce the rendering is present.

Note that MVC also has the notion of a ViewModel, which is a representation of items from the model specifically designed to contain all the information required by a specific View for rendering, see my answer here for some more on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文