我应该将 asp.net-mvc 强类型视图数据放在哪里?
我一直将视图数据类嵌套在控制器中,随着它们数量的增长,我开始问自己这是否是一个好主意。 话又说回来,用这些东西污染 /Views 和 /Controllers 目录似乎是不对的。
我在这里缺少一个约定吗? 也许是 /ViewData 目录? idk,我的 viewdata 类有哪些好的位置?
I've been nesting my viewdata classes inside my controllers and, as their numbers grow, I'm starting to ask myself if this is a good idea. Then again, something about polluting the /Views and /Controllers directories with these things seems off.
Is there a convention I'm missing here? Maybe a /ViewData directory? idk, what are some good locations for my viewdata classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道有什么约定。 我只是将我的放在
/Model/ViewModel/BlahViewModel.cs
下,等等。除非有特定需要,否则我不会将它们放在单独的项目中。 如果需要的话,稍后移动它们并不困难。I don't know of a convention. I just put mine under
/Model/ViewModel/BlahViewModel.cs
, etc. I wouldn't put them in a separate project until there was a specific need for that. It wouldn't be difficult to move them later on if needed.我完全按照你的建议做了,我的强类型视图数据位于 /ViewData 中。 我考虑过将其放在 \Model 目录中,但我不喜欢我的项目有太多嵌套目录。 \ViewData 也是 Kigg 所做的事情。
I did exactly what you're suggesting, I have my strongly typed viewdata living in /ViewData. I thought about putting it in the \Model directory but I don't like my projects having too many nested directories. the \ViewData is also what Kigg does.
我将视图数据类放入一个专门用于此目的的项目中。 它们是 DTO,将它们放入自己的项目中可以强制它们不依赖于架构层之上的任何内容。
使用它们作为 DTO 来传递到视图只是使用它们的一种方式。 我可能会在某个时间通过线路发送它们,在服务总线上的消息内或其他什么地方。
I put my view data classes in a project dedicated to just that. They are DTO's, and putting them in their own project enforces that they do not depend on anything above in the architectural layers.
Using them as DTO's to deliver to views is just one way of putting them to use. I might send them over the wire some time, inside a message on a service bus or whatever.
由于您使用的是 MVC,并且文件夹结构应代表代码的命名空间结构,因此我建议您将每个对象域的控制器、模型和服务分组到单独的文件夹中,
我们将使用
DomainName
<前><代码>控制器
模型
服务
since you are using MVC and the folder structure should represent the namespace structure of your code I would recommend for each of your object domains you should group your controllers, models and services into seperate folders
we would use
DomainName