ASP.NET MVC:组织 ViewModel 的约定

发布于 2024-08-03 07:55:25 字数 604 浏览 3 评论 0原文

正如各种 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 技术交流群。

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

发布评论

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

评论(3

把昨日还给我 2024-08-10 07:55:25

我在控制器和视图旁边使用“模型”文件夹。您的项目中是空的(我不使用区域)。

“MVC”模型中的“M”位于单独的程序集中。 Web 装配体中唯一的模型是演示/编辑模型。

在 Models 文件夹内,像往常一样按名称空间划分了子文件夹。所以我有:

Vertex.Data(与存储库等组装)

顶点网络

控制器
  酒吧控制器
  Foo控制器
型号
  酒吧
    条形列表项
  富
    FooDetail
    FooListItem
意见
  酒吧
  富
  共享

...等。

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:

Vertex.Data (Assembly with repositories, etc.)

Vertex.Web

Controllers
  BarController
  FooController
Models
  Bar
    BarListItem
  Foo
    FooDetail
    FooListItem
Views
  Bar
  Foo
  Shared

...etc.

烂柯人 2024-08-10 07:55:25

我通常为每个视图创建一个模型。即使它只是我可以使用的项目中其他某个对象的精确地图。这样我的视图及其所需的模型就与应用程序的其余部分解耦了。通过扩展视图的模型,将来向视图添加数据也变得非常容易。

这需要预先做更多的工作,有时看起来像是重复的对象,但我只是更喜欢分离。

我将所有视图模型存储在 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.

被翻牌 2024-08-10 07:55:25

我认为(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.

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