C
C#2y 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; }

}
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; }

}
10 Replies
JakenVeina
JakenVeina2y ago
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 alteratively
JakenVeina
JakenVeina2y ago
Options 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.
JakenVeina
JakenVeina2y ago
Configuration - .NET
Learn how to use the Configuration API to configure .NET applications. Explore various inbuilt configuration providers.
JakenVeina
JakenVeina2y ago
this is Microsoft's current-gen implementation of what you're trying to do you should definitely consider using these libraries
Accord
Accord2y ago
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.
mario12136
mario12136OP17mo ago
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
private SettingsModel()
{
string jsonString = File.ReadAllText(Path.GetFullPath("./Data/settings.json"));
JsonConvert.PopulateObject(jsonString, this);
}
private SettingsModel()
{
string jsonString = File.ReadAllText(Path.GetFullPath("./Data/settings.json"));
JsonConvert.PopulateObject(jsonString, this);
}
and use the Newtonsoft.Json instead of the built-int serializer in .Net. Would appreciate your thoughts on this.
JakenVeina
JakenVeina17mo ago
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.
mario12136
mario12136OP17mo ago
oh I thought I should avoid a public constructor for preventing creating more than one instance
JakenVeina
JakenVeina17mo ago
it's not an insane idea, but it doesn't ACTUALLY serve any real purpose, I.E. any actual requirement you have.
Accord
Accord17mo ago
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.
Want results from more Discord servers?
Add your server