Performance of using static methods vs instantiating the class containing the methods

后端 未结 8 1688
别跟我提以往
别跟我提以往 2020-11-30 07:31

I\'m working on a project in C#. The previous programmer didn\'t know object oriented programming, so most of the code is in huge files (we\'re talking around 4-5000 lines)

8条回答
  •  时光取名叫无心
    2020-11-30 08:09

    I have dealt with a similar problem where i work. The programmer before me created 1 controller class where all the BLL functions were dumped.

    We are redesigning the system now and have created many Controller classes depending on what they are supposed to control e.g.

    UserController, GeographyController, ShoppingController...

    Inside each controller class they have static methods which make calls to cache or the DAL using the singleton pattern.

    This has given us 2 main advantages. It is slightly faster(around 2-3 times faster but were talking nanoseconds here ;P). The other is that the code is much cleaner

    i.e

    ShoppingController.ListPaymentMethods()
    

    instead of

    new ShoppingController().ListPaymentMethods()
    

    I think it makes sense to use static methods or classes if the class doesn't maintain any state.

提交回复
热议问题