MVC控制器为什么要对他们的类名最后的'控制'惯例? [英] Why do MVC controllers have to have the trailing 'Controller' convention on their class name?

查看:197
本文介绍了MVC控制器为什么要对他们的类名最后的'控制'惯例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得可笑的是MVC无法识别控制器,除非它有控制器追加到类名。 这个答案提到了 ControllerDescriptor ControllerTypeCache 在MVC中的两个地方这个惯例设置。

I find it ridiculous that MVC doesn't recognize a controller unless it has 'Controller' appended to the class name. This answer mentions the ControllerDescriptor and ControllerTypeCache as the two places in MVC where this convention is set up.

我的问题是为什么呢?这显然​​不是优于配置的东西约定,如 IsControllerType ControllerTypeCache 检查该类:

My question is why? It's clearly not a convention over configuration thing, as IsControllerType in ControllerTypeCache checks that the class:


  • 是公共

  • 不是抽象

  • Implementes 一个IController

  • 控制器
  • 结束
  • Is public
  • Is not abstract
  • Implementes IController
  • Ends with "Controller"

有谁知道这样做的原因是什么?所有的控制器后,可能是在实际的MVC项目,一个名为控制器文件夹中,并在文件的简单双击会告诉我们,类继承控制器

Does anybody know the reason for this? After all controllers are likely to be in an actual MVC project, in a folder named 'Controllers', and a simple double click on the file will show us that the class inherits Controller.

只是看起来傻傻的我 - 但我想知道如果有一个实际原因,他们已经这样做了。

Just seems silly to me - but I was wondering if there is an actual reason they have done this.

修改

刚刚看到从昨天菲尔哈克的这篇博客文章,其中他讨论本公约的决定 - 他是一样的心态我的 - !可能有点意义

Just seen this blog post by Phil Haack from yesterday where he discusses the decision this convention - he is of the same mind of me - Probably a bit pointless!

推荐答案

您可以随时提供自定义控制器工厂,将不同的解决这些类。而且我不同意,控制器不需要的控制器的类型名称追加,因为毕竟他们只是像任何其他类。他们的祖先OOP类型将它们​​定义为控制器反正(一个IController 控制器 ...)

Custom controller factory

You can always provide a custom controller factory that will resolve these classes differently. And I do agree that controllers need no Controller type name appending because after all they're just like any other class. Their OOP ancestor type defines them as controllers anyway (IController, Controller...)

虽然它可能有一些做与Visual Studio。类似的属性的类。也许Visual Studio中不会以不与控制器的结尾的类提供额外的上下文菜单项。当控制器动作是你可以轻松地导航(或创建),以匹配视图。

Although it may have something to do with Visual Studio. Similar to Attribute classes. Maybe Visual Studio wouldn't provide additional context menu items to classes that don't end with Controller. When being in controller action you can easily navigate (or create) to the matching view.

专家,我也同意。还有其他的约定这样的:在.NET框架以及但是人们不要埋怨他们。

So say the experts and I do agree. There are other conventions like these in .net framework as well but people don't complain about them.

想到的收藏 词典 属性 列表和其它类型也使用类似的后缀没有特别原因。他们会工作无论哪种方式,但他们通过他们的用户更容易辨认 - 开发商 - 谁本能地知道自己应该如何工作以及何时使用它们。

Think of collections, dictionaries, attributes, lists and other types that also use similar suffixes without particular reason. They'd work either way, but they're much easier recognisable by their users - developers - who instinctively know how they should work and when to use them.

想象一下,有一个 ProductController的这可能把手产品应用程序模型实体实例。如果不具有的控制器的命名规则,我们就会有两种类型具有相同的名称,因此总是有提供命名空间,两者之间的区别。但是因为我们确实有这个约定,这是没有必要的,没有发生冲突类型

Imagine having a ProductController that likely handles Product application model entity instances. By not having the controller naming convention, we'd have two types with the same name hence would always have to provide namespaces to distinguish between the two. But because we do have this convention this is not necessary and no type clashes occur.

public class ProductController : Controller
{
    public ActionResult Index()
    {
        // we'd have to distinguish this Product type here
        IEnumerable<Product> result = GetProducts();
        return View(result);
    }
    ...
}

这篇关于MVC控制器为什么要对他们的类名最后的'控制'惯例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆