与视图模型相比,将某些内容传递到 viewdata 之间的性能是否有所不同
在 asp.net mvc 中,如果我将字典或对象数组放入 ViewData 中,并在我看来与创建具有相同数据结构的视图模型类相比,是否存在性能差异或其他考虑因素,或者我应该期望相同的响应时间?
in asp.net mvc , if i shove a dictionary or an array of objects in ViewData and read that in my view compared to creating a view model class that has that same data structure, is there a performance difference or other consideration or should i expect the same response time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
响应时间会很短,并且不会真正对性能产生重大影响。
它甚至会降低您的性能,并且非常
hacky
,因为您必须广泛使用装箱和拆箱最终会失败。然而,视图模型是向视图提供模型数据的
适当
方式,因为它为您提供了更多使用的可能性:它提供:
最重要的是。整个框架构建在这些小工具之上,为您的旅程提供支持。
现在就去破解一些代码吧! :-]
The response time would be minimal and wouldn't really make a significant impact on the performance.
It even decreases your performance and is very
hacky
because you'll have to make extensive use of boxing and unboxing which will fail eventually.Yet the View Model is the
appropriate
way to provide model data to the view because it gives you so many more possibilities to work with:It provides:
Html.EnableClientSideValidation()
)And on top of that. The entire framework is built on top of these little gadget which support you on your journey.
Now go and break some code! :-]
如果您在视图端将 ViewData 转换回字典,那么性能将会受到影响。
另外,您现在正在将代码引入视图中。视图应该几乎没有代码。
由于某种原因,您的视图可以从类继承。使用它。 ViewData 只能用于一次小字符串,例如 mayby 页面标题或其他内容。
我只是在这里修复一个项目,其中没有视图继承模型,而是使用 ViewData 进行列表等。有时在一个视图中可以显示 15 个。该死的事情太慢了,但是通过用模型替换 ViewData,我们正在恢复速度。
If you are converting, at the view end, from ViewData back to your dictionary then yeah there will be a performance hit.
Also, you are now introducing code into the view. Views should have little to no code.
Your View can inherit from a class for a reason. use it. ViewData should only be used for once of little strings like mayby page titles or something.
I'm just fixing up a project here where no view inherits from a model and instead uses ViewData for Lists and the like. Sometimes 15 of them in a single view. The damn thing is so slow but by replacing ViewData with models we are clawing back the speed.