ASP.NET MVC控制器的静态方法 [英] ASP.NET MVC controllers static methods
问题描述
有一个讨论,在工作中最近想出了为什么ASP.NET MVC不使用静态方法及其控制的方法。虽然我是在围栏的对的侧面采用静态方法,只有2个参数,我能想到的非静态的操作方法是传承与嘲笑(它的传承给你)的能力。
A discussion came up at work recently about why ASP.NET MVC doesn't use static methods for its controller methods. Whilst I was on the side of the fence against using static methods, the only 2 arguments I could think for non-static action methods were inheritence and the ability to mock (which inheritence gives you).
什么是微软的设计选择超过静态非静态动作/方法?
What was Microsoft's design choice for non-static actions/methods over static?
推荐答案
虽然我不知道那些在这里设计了ASP.NET MVC框架的心目中是最大的一个对我来说:
While I don't know minds of those that designed the ASP.NET MVC Framework here is the big one for me:
一个实例控制器实例化一次每个请求,多个请求可以同时发生。如果控制器是静态的则控制器上的任何状态在所有同时请求共享。你可能不希望出现这种情况。该共享状态更新变得锁定争,可能死锁和很难跟踪的bug,如果锁定未正确实施的雷区。
An instance controller is instantiated once per request, multiple requests can be happening simultaneously. If a controller is static then any state on the controller is shared across all requests simultaneously. You probably don't want that. Updating that shared state becomes a minefield of locking contention, possible deadlocks and very hard to track bugs if locking isn't implemented properly.
在短期,静态的控制器将一起工作的噩梦。
In short, a static controller would be a nightmare to work with.
这篇关于ASP.NET MVC控制器的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!