简体   繁体   中英

How to manage mutiple Instances of Settings in C#?

I have drawing application, in which user can work on multiple drawings at same time. Lets say a user has opened 4 drawings on -> top left, top right, bottom left and bottom right respectively.

Previously, I had a static settings file and all the 4 drawings were using the same settings. Now I need to implement different settings file for all the 4 drawings. So user can have different tolerances in different drawings. The settings are read from Json.

If I create a settings() object then I will have to pass it to every variable which needs the value from settings.

Previously, I could just do

var dim = Settings.Tolerance; 

This is making the program really ugly and I am forced to pass settings in every function from top to bottom and also extension methods.

Is there any thing I can do to avoid this?

I am implementing this in C#, .net core.

Possible solutions could be to stick with the static class, and have the class to hold multiple instances of your settings object.

In the following example class SettingsCollection is used to hold multiple instances of your Settings object, places inside System.Collections.Dictionary<TKey,TValue> settings instances are identified by single Int32 but you could also use Strings or what ever suits your application.

De/serializing the System.Collections.Dictionary<int,Settings> to Json and should work just fine, and you will end up with Json that looks something like this:

{
    "1": {
        "Tolerance": 10,
        "SomeOtherValue": 1.15,
        "SomeStringValue": "hello from settings instance 1"
    },
    "2": {
        "Tolerance": 100,
        "SomeOtherValue": 1.20,
        "SomeStringValue": "hello from settings instance 2"
    },
    "3": {
        "Tolerance": 20,
        "SomeOtherValue": 2.30,
        "SomeStringValue": "hello from settings instance 3"
    },
    "4": {
        "Tolerance": 200,
        "SomeOtherValue": 3.1,
        "SomeStringValue": "hello from settings instance 4"
    }
}

Settings are loaded from the file when the application tries to access them for the first time, but this could also be done manually when application is started.

    public class Settings
    {
        /*Your settings object*/
    }

    // using System.Collections.Generic;
    // Class that hold dictionary of your 'Settings' instances.
    // Here settings instances are identified by the `Int32` numeric identifier, but you can of course use anything that suits your needs.
    public class SettingsCollection
    {
        public Dictionary<int, Settings> Settings;
    }

    // Static class that holds the instance of `SettingsCollection`.
    // In this example the `_Instance` is initialized when the it is accessed for the first time, you could also initialize the `_Instance` by your self for example when application is started.
    public static class SettingsContainer
    {
        private SettingsCollection? _Instance = null;

        public SettingsCollection Instance {
            get {
                if (_Instance == null) {
                    _Instance = LoadFromFile();
                }

                return _Instance;
            }
        }

        private SettingsCollection LoadFromFile()
        {
            string jsonString = "";/* load json from the file */
            return JsonConvert.DeserializeObject<SettingsCollection>(jsonString);
        }

        public void Save() => SaveToFile(Instance);

        private void SaveToFile(SettingsCollection settings)
        {
            string jsonString = JsonConvert.SerializeObject(s);
            /* Write json to file */
        }
    }

Usage example:

    public class Program
    {
        public static void Main()
        {
            Settings drawing1Settings = SettingsContainer.Instance.Settings[1];

            // or

            if (!settingsCollection.Settings.TryGetValue(1, out Settings drawing1Settings)) {
                // settings instance not found with key `1`
            } else {
                // settings instance found
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM