Click here to Skip to main content
13,767,570 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.8K views
71 downloads
9 bookmarked
Posted 14 Oct 2018
Licenced CPOL

Introducing SimpleSamSettings

, 17 Oct 2018
Rate this:
Please Sign up or sign in to vote.
A simple (duh!), Cross-Platform, and Scalable Settings Framework

Introduction

SimpleSamSettings is a framework based on the idea that many distinct settings classes are better than one huge one. Persistence is configurable (Save after every change? Turn off persistence for in memory only?), as is the location.

Every application requires user-, application-, and other defined settings.  The maintenance of these settings can be horrendous over time if not property architectured and maintained.

SimpleSamSettings aims to ease the burden of creating, managing, saving, and restoring settings for any application in .NET Standard.

Available on nuget here and github here.

Background

This architecture has been in my production code for many years now, and I've recently re-written it for the open source community. Tried and true, hopefully it can handle anything you throw at it!

Using the Code

public class MySettings : OverwriteSettingsProfile<MySettings>
{
   public string SomeProp
   {
      get => Get<string>();
      set => Set(value);
   }
}

Globals.SettingsFileBasePath = Path.Combine(Environment.GetFolderPath
                               (Environment.SpecialFolder.ApplicationData), "MyAppName");

That's it! Your settings will be persisted to %appdata%/MyAppName/MySettings.settings in JSON format. Prefer binary? No problem! Put the [Serializable] tag on it and it will auto-serialize as binary.

By default, each change updates the file on disk. To turn off this behavior, simply set AutoSave to false, like this:

var settings = new MySettings() { AutoSave = false };

...then at any time, you can save it (say, on application exit):

settings.SaveInstance() OR MySettings.Save(); // OverwriteSettingsProfile<> 
                                        // ensures a singleton instance, so it is a static helper method

Undoing Settings Changes - The Easy Way!

Say you have a dialog open for the user to change settings. The settings changes are live, ideally, but if the user clicks 'Cancel' everything they've done during the lifetime of that dialog should be undone. Easy peasy!

Simply set the 'MakeUndoable' flag in the base to true.

settings.MakeUndoable = true;

... do your stuff in the dialog ...

To cancel or undo everything:

settings.RevertChanges();

Then set MakeUndoable back to false so it doesn't save off changes:

settings.MakeUndoable = false;

Usage Advice

Encapsulate your settings into small classes - many, properly encapsulated classes is preferred over one or a few very large ones! This makes it easier to maintain, easier to use and reuse, and speeds up disk I/O.

NOTE

If a collection or object inside the settings class changes, the file will not be autosaved. It is recommended that the settings class listens for changes on those objects and auto-saves them, as appropriate, but this is in no way enforced and left entirely up to the consumer (you). Happy settings coding!

History

  • 15th October, 2018: Initial public release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

outbred
Software Developer (Senior)
United States United States
Senior Software Developer/Lead/Architect for all things .NET/C#. Passionate about the SDLC and architecture.

You may also be interested in...

Comments and Discussions

 
QuestionVery very nice Pin
Reader Man San18-Oct-18 7:54
professionalReader Man San18-Oct-18 7:54 
AnswerRe: Very very nice Pin
outbred18-Oct-18 14:53
professionaloutbred18-Oct-18 14:53 
Questionhave you consider to post this as a tip? Pin
Nelek16-Oct-18 10:09
protectorNelek16-Oct-18 10:09 
Question?? 9k ?? Pin
mag1315-Oct-18 16:57
membermag1315-Oct-18 16:57 
AnswerRe: ?? 9k ?? Pin
outbred16-Oct-18 9:34
professionaloutbred16-Oct-18 9:34 
QuestionSource File Pin
Member 1402062915-Oct-18 9:19
memberMember 1402062915-Oct-18 9:19 
AnswerRe: Source File Pin
Alen Toma15-Oct-18 17:39
memberAlen Toma15-Oct-18 17:39 
GeneralRe: Source File Pin
outbred16-Oct-18 9:34
professionaloutbred16-Oct-18 9:34 
QuestionGood job Pin
Alen Toma15-Oct-18 1:50
memberAlen Toma15-Oct-18 1:50 
AnswerRe: Good job Pin
outbred16-Oct-18 9:36
professionaloutbred16-Oct-18 9:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05-2016 | 2.8.181116.1 | Last Updated 17 Oct 2018
Article Copyright 2018 by outbred
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid