我应该将我的ASP.NET MVC控制器操作虚拟化吗?

时间:2021-12-20 04:13:36

File -> New Project for ASP.NET MVC projects used to generate controllers with virtual actions. I'm not sure if that stopped with MVC 2 or MVC 3, but is this no longer a best practice?

文件 - >用于生成具有虚拟操作的控制器的ASP.NET MVC项目的新项目。我不确定MVC 2或MVC 3是否停止了,但这不再是最佳做法吗?

4 个解决方案

#1


7  

T4MVC Does make action methods virtual. If you are using it, it should make action methods virtual, no other way it can work

T4MVC确实使动作方法成为虚拟的。如果您正在使用它,它应该使操作方法成为虚拟,没有其他方式可以工作

#2


2  

The current documentation for ASP.NET MVC 3 does not show virtual methods. I'm not really sure what making them virtual would gain you, as I've never subclassed controllers to override actions.

ASP.NET MVC 3的当前文档未显示虚拟方法。我不确定是什么让它们变得虚拟会让你获益,因为我从未将控制器子类化为覆盖动作。

#3


1  

If you make them virtual then it will make the controllers easier to mock if you are using them in tests.

如果你将它们设为虚拟,那么如果你在测试中使用它们,它将使控制器更容易被模拟。

#4


0  

Generated code may be made virtual for various reasons. It's not good practice to make your own code virtual unless required by some tool. See Liskov Substitution Principle and Open/Closed Principle. I think some frameworks do this to facilitate creating proxies but I can't imagine any reason to make all of your methods virtual. To me it screams copy/paste or cargo cult programmer.

由于各种原因,生成的代码可以是虚拟的。除非某些工具需要,否则将自己的代码设置为虚拟是不好的做法。见Liskov替代原则和开放/封闭原则。我认为有些框架可以帮助创建代理,但我无法想象有什么理由让你的所有方法都是虚拟的。对我来说,它尖叫复制/粘贴或货物崇拜程序员。

#1


7  

T4MVC Does make action methods virtual. If you are using it, it should make action methods virtual, no other way it can work

T4MVC确实使动作方法成为虚拟的。如果您正在使用它,它应该使操作方法成为虚拟,没有其他方式可以工作

#2


2  

The current documentation for ASP.NET MVC 3 does not show virtual methods. I'm not really sure what making them virtual would gain you, as I've never subclassed controllers to override actions.

ASP.NET MVC 3的当前文档未显示虚拟方法。我不确定是什么让它们变得虚拟会让你获益,因为我从未将控制器子类化为覆盖动作。

#3


1  

If you make them virtual then it will make the controllers easier to mock if you are using them in tests.

如果你将它们设为虚拟,那么如果你在测试中使用它们,它将使控制器更容易被模拟。

#4


0  

Generated code may be made virtual for various reasons. It's not good practice to make your own code virtual unless required by some tool. See Liskov Substitution Principle and Open/Closed Principle. I think some frameworks do this to facilitate creating proxies but I can't imagine any reason to make all of your methods virtual. To me it screams copy/paste or cargo cult programmer.

由于各种原因,生成的代码可以是虚拟的。除非某些工具需要,否则将自己的代码设置为虚拟是不好的做法。见Liskov替代原则和开放/封闭原则。我认为有些框架可以帮助创建代理,但我无法想象有什么理由让你的所有方法都是虚拟的。对我来说,它尖叫复制/粘贴或货物崇拜程序员。