ASP.NET MVC 控制器静态方法 [英] ASP.NET MVC controllers static methods

查看:29
本文介绍了ASP.NET MVC 控制器静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近在工作中讨论了为什么 ASP.NET MVC 不对其控制器方法使用静态方法.虽然我对使用静态方法持反对,但对于非静态操作方法,我能想到的唯一两个论点是继承和模拟能力(继承给了你).>

Microsoft 对非静态操作/方法的设计选择是什么而不是静态的?

解决方案

虽然我不知道那些设计 ASP.NET MVC 框架的人的想法,但这里对我来说很重要:

每个请求实例化一个实例控制器,多个请求可以同时发生.如果控制器是静态的,那么控制器上的任何状态都会在所有请求中同时共享.你可能不想那样.更新共享状态会成为锁定争用的雷区,可能会导致死锁,并且如果锁定未正确实施,则很难跟踪错误.

简而言之,使用静态控制器将是一场噩梦.

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 two 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?

解决方案

While I don't know minds of those that designed the ASP.NET MVC Framework here is the big one for me:

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屋!

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