I need a method that return me some parameters in controllers this is implementation of it:
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;
}
I use this method in all of controllers, So I think about 3 option that I have to define it:
1-For any controller class define it in that controller like this:
public class ProductController : Controller {
public List<Parameter> GetParameters(FormCollection collection) {
//
}
}
2-Define it in static class as static method:
public static class GeneralMethods {
public static List<Parameter> GetParameters(FormCollection collection) {
//
}
}
3-Define it as a None Static :
public class GeneralMethods {
public List<Parameter> GetParameters(FormCollection collection) {
//
}
}
which one is better? which one have better performance? or any other option for define methods that used in many controllers?
what is your suggestion?
There will be no performance impact in any of the three. (Though last approach will create separate object each time,it will be gracefully handled by GC).
approach 1: NO, as a standard practice we should not duplicate the code.
approach 2: YES, if your method depends only on the input parameter.
approach 3: YES, if you need to set up some instance variable and your method depends on them.
suggested approach: (approach 1+ approach 3)
If this method is common to all of your controller (or most), declare a base controller with this method and inherit all other controller from it.
Static methods will not be a problem as any variable declared with in a method are with in scope of the method.