Today, we need to write a factory class for authenticationproviderfactory. We need to provide external services for registration, elimination, and iauthenticationprovider. In addition, we can obtain and set some corresponding values in the configuration during construction.
Here I thought about whether to use a single piece or a static method to provide these three functions for a long time. Although the use of a single piece is well differentiated from the use of static in principle (if this class is a tool set, use static no, if it represents a concept using a single piece ), but it is not so easy to do.
So I thought it would be more convenient to use and maintain the static method, however, you must first obtain the configuration information, so you must write a static constructor to set the configuration value. If static structures are used for initialization, a call failure is triggered when the configuration information is incorrect, and this failure does not display clear information, and the entire application is also used.ProgramCrash. If a single piece is used, the Initialization Configuration will be called when the single piece is called. If the configuration is incorrect, the Initialization Configuration will be triggered here. If this method is not called, the program will not be faulty, and other parts can run normally, which is of great help to program fault tolerance.
In the end, I decided to use a single piece to solve this problem. It also reminds me of the specific use process.
Do not use a static constructor whenever possible. If necessary, ensure that the static constructor will not generate exceptions.