为什么 MVC 在将对象传递给视图时倾向于将对象转换为数组?
为什么 MVC 在将对象传递给视图时倾向于将对象转换为数组?
只是好奇这是否有原因。
Why do MVCs tend to convert objects into arrays when passing them off to a view?
Just curious if there is a reason for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为您不应该能够从视图内更改对象。我想这只是与代码纯度和帮助用户遵循 MVC 模式有关。
Probably because you should not be able to alter the object from within the view. I guess it just has to do with code purity and helping the user follow the MVC pattern.
如果数组指的是集合,那么原因是无法保证您只想将一个对象传递给视图。
当然,您可以有一个上下文对象,它将所有数据传递到视图,但框架宁愿提供一种机制,不需要您聚合数据,特别是因为它可能采用截然不同的形状。
If by arrays, you mean sets, then the reason is because there is no guarantee that you only want to pass one object to the view.
Sure, you could have a context object that does pass all of your data to the view, but the frameworks would rather provide a mechanism for doing so that doesn't require you to aggregate your data, especially since it could take vastly different shapes.