Using static constructors and using single pieces

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.