什么是ASP.NET MVC好人选基础类? [英] What are good candidates for base controller class in ASP.NET MVC?

查看:119
本文介绍了什么是ASP.NET MVC好人选基础类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多人谈论自己的ASP.NET MVC项目使用基地控制器。我见过的典型事例进行记录或者CRUD脚手架做到这一点。什么是一个基础类的其他一些很好的用途?

I've seen a lot of people talk about using base controllers in their ASP.NET MVC projects. The typical examples I've seen do this for logging or maybe CRUD scaffolding. What are some other good uses of a base controller class?

推荐答案

有一个基础类的没有很好的用途。

There are no good uses of a base controller class.

现在听我说。

Asp.Net MVC,尤其是MVC 3具有可扩展性吨的挂钩提供更解耦的方式将功能添加到所有控制器。由于你的控制器类是非常重要的,核心的应用程序的真正重要的,让他们光,敏捷,松散耦合一切。

Asp.Net MVC, especially MVC 3 has tons of extensibility hooks that provide a more decoupled way to add functionality to all controllers. Since your controllers classes are very important and central to an application its really important to keep them light, agile and loosely coupled to everything else.


  • 日志基础设施属于一个
    构造和应注射
    通过DI框架。

  • Logging infrastructure belongs in a constructor and should be injected via a DI framework.

CRUD脚手架应处理
code生成或自定义
ModelMetadata提供商。

CRUD scaffolding should be handled by code generation or a custom ModelMetadata provider.

全球异常处理应该是
通过定制ActionInvoker处理。

Global exception handling should be handled by an custom ActionInvoker.

全局视图数据和授权
应采取行动的过滤器进行处理。
即使全球行动过滤器更容易
在MVC3。

Global view data and authorization should be handled by action filters. Even easier with Global action filters in MVC3.

常量可以在其他类/文件名为ApplicationConstants或东西去了。

Constants can go in another class/file called ApplicationConstants or something.

基地控制器通常用于经验不足的开发者MVC谁不知道所有MVC的不同扩展部分。现在不要误会我的意思,我不是判断和谁使用他们的所有错误的原因人们的工作。它是为您提供更多的工具只是经验来解决共同问题。

Base Controllers are usually used by inexperienced MVC devs who don't know all the different extensibility pieces of MVC. Now don't get me wrong, I'm not judging and work with people who use them for all the wrong reasons. Its just experience that provides you with more tools to solve common problems.

我几乎可以肯定的没有,你不能与其他的可扩展性挂钩不是一个基础类解决一个单一的问题。不要在不采取耦合(继承)的紧密形式,除非是有显著的生产力理由,你不违反Liskov的。我宁愿走在< 1秒到20次在我的控制器输入了一个属性如公共ILogger记录仪{搞定;组; } 不是引入一个紧耦合这效应更为显著的方式申请。

I'm almost positive there isn't a single problem you can't solve with another extensibility hook than a base controller class. Don't take on the the tightest form of coupling ( inheritance ) unless there is a significant productivity reason and you don't violate Liskov. I'd much rather take the < 1 second to type out a property 20 times across my controllers like public ILogger Logger { get; set; } than introduce a tight coupling which effects the application in much more significant ways.

即使像用户标识或者多租户键可以在的ControllerFactory而不是基本的控制器去。一个基础类的偶合成本是不值得的。

Even something like a userId or a multitenant key can go in a ControllerFactory instead of a base controller. The coupling cost of a base controller class is just not worth it.

这篇关于什么是ASP.NET MVC好人选基础类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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