dimanche 8 mai 2016

How to improve class object initialization in C#?

I have a class in an existing app that is initialized every time an event is fired, using XMLConfigManager objXMLConfig = new XMLConfigManager();.

Obviously it makes no sense to do so, and there are better ways to have a static class. But the same class reads the system registry to find the appropriate database to access. Below is the code from the constructor of the class.

    public XMLConfigManager()
    {
        RegistryKey objRKConfigGroup = Registry.LocalMachine.OpenSubKey("Software\\......");
        strFilePath = (string)objRKConfigGroup.GetValue("XMLFilePath");
        objRKConfigGroup.Close();
        string strApplicationName = (string)CacheHandler.GetCache().GetCachedElement("APPLICATION");
        this.ApplicationName = strApplicationName;
    }

I am trying to find ways to improve this, so that we don't go to registry all the time. Instead we can refresh the IIS cache to update the registry value if it ever changes (maybe once in a year, if database IP changes, or new version is released).

Now the question is:

Is static class a good choice? Or should I use singleton? Or is there any other better option I have missed?

Secondly, will it require me to change the XMLConfigManager objXMLConfig = new XMLConfigManager(); line in every place (about 2k+ event handlers in total).

P.S. I am considering the fact that reducing registry access events should be faster. I hope I am right about the idea. Also, if the question belongs to a better stack exchange section, do let me know and I will move it there.

Aucun commentaire:

Enregistrer un commentaire