C#C
C#3y ago
mario12136

❔ Settings model as a singleton in WPF

I have been using Properties.Settings.Default for my application which automatically saves a config file in the AppData\Local folder and I have been wanting to create a settings model to have more control over the file location and other things. Since my requirements were that there be a single instance that is accessible all throughout my app, I have found that this is called the Singleton pattern. The class I will create will read from and write as Json.

My question is is this common and a correct use of the Singleton pattern? Is there something that I need to consider further before creating my Settings class.

Also any relevant resources/guides would be appreciated.

What I have
    internal sealed class SettingsModel
    {
        private static SettingsModel _instance;

        public static SettingsModel Instance
        {
            get
            {
                _instance ??= new SettingsModel();
                return _instance;
            }
        }

        private SettingsModel()
        {
        }

        [JsonPropertyName("FontSize")]
        public int FontSize { get; set; }

        [JsonPropertyName("FontFamily")]
        public FontFamily FontFamily { get; set; }

        }
Was this page helpful?