When is it best to use static functions in ASP.NET?

后端 未结 3 2134
走了就别回头了
走了就别回头了 2021-02-10 01:34

I have been wondering, when to use static functions, and when not to in ASP.NET?

What are the advantages and disadvantages in using them, in various aspects like perform

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-10 02:17

    Cons:

    • threading issues (static functions don't require an instance to be called on, so it is easy to invoke them from different parts of the code and if they read/write to a shared state this state might be corrupted in a multi-threaded environment such as ASP.NET)
    • difficult to unit test (as static functions don't require an object instance, constructor injection is impossible meaning that the only way to inject dependencies is by passing them as arguments to the function itself)

    Pros:

    • performance (this is questionable - in most cases performance gains will be completely negligible compared to other parts of the code)

提交回复
热议问题