模型中的 Display 属性是否违反了视图和模型的关注点分离
可能是个愚蠢的问题。 但是使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MVC 框架允许您将 DataAnnotations 放入模型中,但严格来说,DataAnnotations 应该放在 ViewModel 中,而不是放在映射到数据库的 Model 中。
The MVC framework allows you to put the
DataAnnotations
in a model, but strictly talkingDataAnnotations
should be placed in a ViewModel and not in a Model mapped to the database.不,如果它是您的 ViewModel,则不会违反模式。
@Html.DisplayFor(x => x.MyProperty)
方法将查找 Display 属性并使用该信息。因此,正确的方法是使用属性而不是视图中的任何类型的硬编码文本。
希望这有帮助
No, it does not violate the pattern if it is your ViewModel.
The
@Html.DisplayFor(x => x.MyProperty)
method will look for the Display attributeand 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
将域模型与视图隔离和拥有贫乏的域模型 (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."