模型中的 Display 属性是否违反了视图和模型的关注点分离

发布于 2024-12-19 17:44:16 字数 95 浏览 1 评论 0原文

可能是个愚蠢的问题。 但是使用 Display 属性在模型中指定标题是否违反了关注点分离原则?标题不应该属于视图吗?

如果没有,有人可以解释为什么它属于模型吗?

Might be a stupid question.
But doesn't specifying the caption in the model using the Display attribute violate the separation of concerns principle? Shouldn't the caption belong in the view?

If it does not, could somebody explain why it belongs in the model?

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

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

发布评论

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

评论(3

雪落纷纷 2024-12-26 17:44:16

MVC 框架允许您将 DataAnnotations 放入模型中,但严格来说,DataAnnotations 应该放在 ViewModel 中,而不是放在映射到数据库的 Model 中。

The MVC framework allows you to put the DataAnnotations in a model, but strictly talking DataAnnotations should be placed in a ViewModel and not in a Model mapped to the database.

鸢与 2024-12-26 17:44:16

不,如果它是您的 ViewModel,则不会违反模式。

ViewModel 通常是一个简单的 POCO(普通旧 CLR 对象),
不包含任何业务逻辑,大多只包含属性。

@Html.DisplayFor(x => x.MyProperty) 方法将查找 Display 属性
并使用该信息。因此,正确的方法是使用属性而不是视图中的任何类型的硬编码文本。

希望这有帮助

No, it does not violate the pattern if it is your ViewModel.

A ViewModel is, usually, a simple POCO (Plain old CLR Object) that
contains no business logic, mostly only properties.

The @Html.DisplayFor(x => x.MyProperty) method will look for the Display attribute
and use that information. So the right way is to use the attribute instead of any kind of hard coded text in your view.

hope this helps

甜点 2024-12-26 17:44:16

将域模型与视图隔离和拥有贫乏的域模型 (http://en.wikipedia.org/wiki/Anemic_domain_model) 之间有一条微妙的界限。我个人认为可以在某些地方将域模型公开为视图模型上的属性。

我个人对贫乏领域模型的不满是它的责任“促进事务脚本和类似用例之间的代码重复,减少代码重用。”

There's a fine line between isolating your domain model from the view and having an anemic domain model (http://en.wikipedia.org/wiki/Anemic_domain_model). I personally think it's ok to expose your domain model in certain places as properties on the view model.

My personal peeve with an anemic domain model is namely the liability that it "Facilitates code duplication among transactional scripts and similar use cases, reduces code reuse."

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