I made a settings class for my own C# application. My main goal was to reach this class from every other class (this is why I choose the Singleton pattern). I also wanted to distinguish the different type of settings, so I added GeneralSettings
and made it public. Do you know any other ways which I can tell apart the different type of settings? I thought about namespaces, but those are not allowed in classes.
I also wanted to make sure that I can restore its state if the user don't want to save the changes made in the settings dialog. What is your opinion about my implementation? Now I need to call SetRestorePoint
and if I want to discard changes Restore.
public class Settings : ISerializable
{
private Settings ()
{
}
static private Settings _instance = null;
static private Settings _restoreInstance = null;
static public Settings Get //Singleton
{
get
{
if (_instance == null)
_instance = new Settings ();
return _instance;
}
private set { }
}
public void SetRestorePoint ()
{
_restoreInstance = DeepClone (_instance);
}
public Settings Restore ()
{
_instance = _restoreInstance;
return _instance;
}
public GeneralSettings General = new GeneralSettings ();
[Serializable]
public class GeneralSettings
{
public bool isAutomaticSave = false;
}
private static T DeepClone<T>(T obj)
{
using (var ms = new System.IO.MemoryStream ())
{
var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ();
formatter.Serialize (ms, obj);
ms.Position = 0;
return (T) formatter.Deserialize (ms);
}
}
//serialization...
}
How have people implemented Settings in bigger software?
ISerializable
implementation? \$\endgroup\$