ASP.NET MVC:组织 ViewModel 的约定
正如各种 MVC 问题 和 blogposts,我们知道 ASP.NET MVC 项目布局非常注重约定。
我盲目地在Controllers
文件夹中创建了一个子目录。
这感觉不对。
替代文本 http://www.imagechicken.com/uploads/1252003002097179400.png
< strong>问题:在哪个目录中存储 ViewModel 的常见约定是什么? 您有什么建议或者既定惯例是什么?
As discussed throughout the various MVC questions and blogposts, we know that the ASP.NET MVC project layout is heavy on convention.
I blindly made a sub-directory in the Controllers
folder.
This doesn't feel right.
alt text http://www.imagechicken.com/uploads/1252003002097179400.png
Question: What's the common accepted convention on which directory to store your ViewModels? What are your suggestions or what is the established convention?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在控制器和视图旁边使用“模型”文件夹。您的项目中是空的(我不使用区域)。
“MVC”模型中的“M”位于单独的程序集中。 Web 装配体中唯一的模型是演示/编辑模型。
在 Models 文件夹内,像往常一样按名称空间划分了子文件夹。所以我有:
...等。
I use a "Models" folder alongside Controllers and Views. The same one which is empty in your project (I don't use Areas).
The "M" in "MVC" model goes in a separate assembly. The only models in the web assembly are presentation/edit models.
Inside the Models folder, there are subfolders by namespace as usual. So I have:
...etc.
我通常为每个视图创建一个模型。即使它只是我可以使用的项目中其他某个对象的精确地图。这样我的视图及其所需的模型就与应用程序的其余部分解耦了。通过扩展视图的模型,将来向视图添加数据也变得非常容易。
这需要预先做更多的工作,有时看起来像是重复的对象,但我只是更喜欢分离。
我将所有视图模型存储在 MVC 项目中创建的 models 目录中。这些“模型”与我的观点一一对应。如果视图模型不仅仅是基本数据持有者,我会使用 Models 文件夹中的子文件夹。子文件夹将包含表示该视图所需的所有内容。
I typically create a model for every view. Even if it is just an exact map of some other object in my project that I could be using. This way my view and it's required model is decoupled from the rest of the application. It also makes adding data to the view in the future very easy by just extending your view's model.
It takes a little more work up front and sometimes seems like your duplicating objects but I just prefer the seperation.
I store all my view models in the models directory created in an MVC project. Those 'models' map one to one to my views. I use subfolders within the Models folder if the view models become more than just basic data holders. The subfolders would contain all the bits and pieces required to represent that view.
我认为(View)Models 应该放在 Models 目录中(当您创建新的 ASP.NET MVC 项目时该目录为空)。
就我个人而言,对我来说,围绕功能而不是机制安排命名空间更有意义,但是虽然这对于模型来说没有什么影响,但对于控制器和视图来说,它会产生一些影响。
I think the idea is that (View)Models should go in the Models directory (which is empty when you create a new ASP.NET MVC project).
Personally, it makes more sense for me to arrange namespaces around features instead of mechanics, but while this is of no consequence with regards to Models, it would have some implications when it comes to Controllers and Views.