4

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?

11
  • Performance-wise you are not likely to notice any difference between the three approaches. What do you mean by "better"? For what? Usability? Readability? Maintenance? Something else? Commented Apr 21, 2012 at 8:30
  • @Oded I don't know in web, which one is usual or any difference in performance or page load time or server performance Commented Apr 21, 2012 at 8:32
  • As I said, when in comes to performance you are not going to get any appreciable differences. Commented Apr 21, 2012 at 8:34
  • 1
    I would go with option 3, not for performance but because you could then extract an interface from that class and mock it when unit testing your controller actions. That said the method itself feels a bit off, it's not clear why you need it in the first place and accessing FormCollection is usually bad practice. Commented Apr 21, 2012 at 8:37
  • @Betty why accessing FormCollection is bad? Commented Apr 21, 2012 at 8:44

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.