【发布时间】:2012-04-21 16:51:56
【问题描述】:
我需要一个在控制器中返回一些参数的方法,这是它的实现:
public List<Parameter> GetParameters(FormCollection collection) {
List<Parameter> parameters = new List<Parameter>();
List<string> parameterNames = new List<string>();
//Get Parameters Names and Values
return parameters;
}
我在所有控制器中都使用这种方法,所以我想了三个我必须定义它的选项:
1-对于任何控制器类,在该控制器中定义它,如下所示:
public class ProductController : Controller {
public List<Parameter> GetParameters(FormCollection collection) {
//
}
}
2-在静态类中定义为静态方法:
public static class GeneralMethods {
public static List<Parameter> GetParameters(FormCollection collection) {
//
}
}
3-将其定义为无静态:
public class GeneralMethods {
public List<Parameter> GetParameters(FormCollection collection) {
//
}
}
哪个更好?哪一个有更好的性能?或用于定义许多控制器中使用的方法的任何其他选项? 你有什么建议?
【问题讨论】:
-
就性能而言,您不太可能注意到这三种方法之间的任何差异。 “更好”是什么意思?为了什么?可用性?可读性?维护?还有什么?
-
@Oded 我不知道在网络中,哪一个是常见的,或者在性能或页面加载时间或服务器性能方面有任何差异
-
正如我所说,在性能方面你不会有任何明显的差异。
-
我会选择选项 3,不是为了性能,而是因为您可以从该类中提取一个接口并在对控制器操作进行单元测试时对其进行模拟。也就是说,该方法本身感觉有点不对劲,不清楚为什么首先需要它,并且访问 FormCollection 通常是不好的做法。
-
@Betty 为什么访问 FormCollection 不好?
标签: asp.net asp.net-mvc asp.net-mvc-3 static