❔ 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
10 Replies
yes, that's acceptable
you should arguably avoid use of
static
here
but that's not a huge issue unless you wanna go for testing
alterativelyOptions pattern - .NET
Learn the options pattern to represent groups of related settings in .NET apps. The options pattern uses classes to provide strongly-typed access to settings.
Configuration - .NET
Learn how to use the Configuration API to configure .NET applications. Explore various inbuilt configuration providers.
this is Microsoft's current-gen implementation of what you're trying to do
you should definitely consider using these libraries
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.Hello,
thanks for the response. I have tried to read the articles you have attached. If I am not mistaken, the second one addresses how to get the values that I want from different sources and the first addresses how to manage groups of related settings. If my app is simple enough that I will only use a json file to serialize/deserialize and don't have "groups" of related options, is there a reason to still use these libraries.
In addition, a slight problem that I run into when trying to work with the code i have attached in the post is that to deserialize using the class i need a public contrstuctor, but since I am trying to make a singleton it shouldn't have one. Any advice on what to do here?
I thought I'd make my contructor like
and use the Newtonsoft.Json instead of the built-int serializer in .Net. Would appreciate your thoughts on this.
The reason you would want to use those libraries, simple or not, is because what you're doing isnwhat they're made for. If you feel more comfortable rolling your own simple solution, go for it.
There is no reason a singleton shouldn't have a public constructor. Write code based on the requirements of its consumers, which right now consists of your application code (which doesn't perform construction of this class) and the JSON library, which requires a public constructor. Nowhere is there a requirement that you should make the constructor private.
oh I thought I should avoid a public constructor for preventing creating more than one instance
it's not an insane idea, but it doesn't ACTUALLY serve any real purpose, I.E. any actual requirement you have.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.