Don't Use "Static" in C#?
Posted
by
Joshiatto
on Programmers
See other posts from Programmers
or by Joshiatto
Published on 2012-08-13T21:53:13Z
Indexed on
2012/12/19
17:13 UTC
Read the original article
Hit count: 535
I submitted an application I wrote to some other architects for code review. One of them almost immediately wrote me back and said "Don't use "static". You can't write automated tests with static classes and methods. "Static" is to be avoided."
I checked and fully 1/4 of my classes are marked "static". I use static when I am not going to create an instance of a class because the class is a single global class used throughout the code.
He went on to mention something involving mocking, IOC/DI techniques that can't be used with static code. He says it is unfortunate when 3rd party libraries are static because of their un-testability.
Is this other architect correct?
update: here is an example:
APIManager - this class keeps dictionaries of 3rd party APIs I am calling along with the next allowed time. It enforces API usage limits that a lot of 3rd parties have in their terms of service. I use it anywhere I am calling a 3rd party service by calling Thread.Sleep(APIManager.GetWait("ProviderXYZ")); before making the call. Everything in here is thread safe and it works great with the TPL in C#.
© Programmers or respective owner