与视图模型相比,将某些内容传递到 viewdata 之间的性能是否有所不同

发布于 2024-09-04 00:30:38 字数 104 浏览 6 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(2

十二 2024-09-11 00:30:38

响应时间会很短,并且不会真正对性能产生重大影响。
它甚至会降低您的性能,并且非常hacky,因为您必须广泛使用装箱和拆箱最终会失败。

然而,视图模型是向视图提供模型数据的适当方式,因为它为您提供了更多使用的可能性:

它提供:

  • ViewModels 的节省使用方法
  • 简单的模型验证(即使是带有 < code>Html.EnableClientSideValidation())
  • 强类型视图(拆箱后即可实现。)
  • 自动生成的视图,无需调整(当您使用 T4 模板时非常棒)->当您调整模型时,它会为您节省大量时间......

最重要的是。整个框架构建在这些小工具之上,为您的旅程提供支持。

现在就去破解一些代码吧! :-]

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:

  • Saver usage of ViewModels
  • Easy model validation (even the one with Html.EnableClientSideValidation())
  • Strongly typed Views (you'll achieve this after unboxing..)
  • Auto generated View which don't need to be adjusted (awesome when you're using T4 templates) -> it will save you so much time when you do adjustments to your model..

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! :-]

百合的盛世恋 2024-09-11 00:30:38

如果您在视图端将 ViewData 转换回字典,那么性能将会受到影响。

另外,您现在正在将代码引入视图中。视图应该几乎没有代码。

由于某种原因,您的视图可以从类继承。使用它。 ViewData 只能用于一次小字符串,例如 ma​​yby 页面标题或其他内容。

我只是在这里修复一个项目,其中没有视图继承模型,而是使用 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.

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