MVC 命名空间组织
希望 SO 社区能够帮助解决办公室内的辩论。 目前我们的结论是“视情况而定”!
在 MVC 中,如何组织命名空间?
选项 A
您是否遵循 MS ASP.NET MVC 路线,拥有模型、控制器和视图命名空间?
选项 B
或者您是否将每个 MVC 三元组分成其逻辑“功能”,例如:
功能 A
- FeatureAModel
- FeatureAController
- 类型
- IFeatureAView(接口 - 以便控制器不会引用具体视图)
- )
- FeatureAView(具体)
我们可以看到两者的要点。 我喜欢选项 B,因为它可以更好地构建我在工作室中的项目 - 我一眼就能看到所有内容都在哪里,因为它是按功能分组的。 但是,例如,如果我们希望将来将所有模型移至服务层,选项 A 为我们提供了更简单的扩展选项。
欢迎所有想法!
Hoping the SO community can help to resolve a debate in the office. At the moment our conclusion is 'it depends' !
In MVC, how do you organise your namespaces?
Option A
Do you go down the MS ASP.NET MVC route of having a Models, Controllers and Views namespace?
Option B
Or do you separate each MVC triad into it's logical 'function', for example:
Feature A
- FeatureAModel
- FeatureAController
- Types
- IFeatureAView (interface - so that controller DOESN'T ref a concrete View)
- View
- FeatureAView (concrete)
We can see the point of both. I like Option B as it structures my projects in studio better - at a glance I can see where everything is as it's grouped by Feature. But Option A gives us an easier scaling option if we want to move all our Models out into a service layer in the future, for example.
All thoughts welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我更喜欢在层次结构中将两者结合起来。 你的选项 A 是我的最高级别,并且在该功能方面是明智的。
I prefer a combination of both in a hierarchy. Your option A is my top level and inside that feature wise.
我个人倾向于将所有控制器保留在同一个命名空间中,这仅仅是因为默认情况下路由不感知命名空间。
这意味着,如果我尝试将控制器命名为相同的名称,我会收到编译器时间错误,而不是有关路由的奇怪运行时错误。
无论如何,已经有很多博客文章,您可以在谷歌上搜索有关此的更多信息。
所以我会说
选项A
I personally tend to favour the idea of keeping all my controllers within the same namespace, solely due to the fact the by default the routing is not namespace aware.
This means that if I try to name my controllers the same name, I get a compiler time error rather than a weird runtime error about routing.
Anyway there have been quite a few blog posts that you can search on google for more information about this.
So I would say
Option A