Settings.Saver 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Settings.Saver --version 1.0.5
                    
NuGet\Install-Package Settings.Saver -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Settings.Saver" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Settings.Saver" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Settings.Saver" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Settings.Saver --version 1.0.5
                    
#r "nuget: Settings.Saver, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Settings.Saver@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Settings.Saver&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Settings.Saver&version=1.0.5
                    
Install as a Cake Tool

Example

The settings class must have a default constructor that SettingsBase can call, but this constructor should not be called directly. Instead, a constructor accepting an ISettingsSaver as a parameter should be used.

internal class SettingsClass : SettingsBase<SettingsClass>
{
    public int Id { get; set; } = 1;

    public string User { get; set; } = "jdoe";

    public int Age { get; set; } = 33;

	//Settings class must have default constructor that is called from SettingsBase.
	//Exception will be thrown if default constructor is called directly.
    [Obsolete("Do not use", true)]
    public SettingsClass() { }

    public SettingsClass(ISettingsSaver settingsSaver) : base(settingsSaver)
    {

    }
}

The Settings.Saver library comes with two implementations of ISettingsSaver:

  1. FileSettingsSaver
    • Saves files to a file.
  2. InMemorySettingsSaver
    • Does not save to disk, stores saved settings in memory. Used primarily for testing.
SettingsClass settings = new SettingsClass(new FileSettingsSaver(@"C:\settings.config"));

Equality Checking

Calling Save() will call CheckIsDirty() and only performs a save operation if one or more of the properties have changed.

CheckIsDirty evaluates changes by value-type, not reference type. If the settings class contains an object, it will check whether the value-types within that object have changed. It also supports objects within objects for property evaluation.

internal class Address
{
	public string State { get; set; }

	public string City { get; set; }

	//A default constructor is required
	public Address()
	{
	
	}
}

internal class User
{
	public int Id { get; set; }

	public string User { get; set; }

	public int Age { get; set; }	

	public Address Address{ get; set; } = new Address();

	//A default constructor is required
	public User()
	{
	
	}
}


internal class SettingsClass : SettingsBase<SettingsClass>
{
	public User TheUser { get; set; } = new User();

	[Obsolete("Do not use", true)]
	public SettingsClass() { }

	public SettingsClass(ISettingsSaver settingsSaver) : base(settingsSaver)
	{

	}
}


SettingsClass settings = new (new InMemorySettingsSaver());
settings.TheUser = new User()
{
	Id = 1,
	Name = "John Doe",
	Age = 54,
	Address = new Address()
	{
		State = "IL",
		City = "Chicago"
	}
}
//Save settings
settings.Save();

// Create a new Address object, but set the properties to the same values as the Address that was saved.
settings.TheUser.Address = new Address()
{
	State = "IL",
	City = "Chicago"
}


bool hasChanged = settings.CheckIsDirty(); //hasChanged is false

Disclaimer

SettingsSaver does not support properties that are arrays, lists, or collections.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 118 2/8/2025
2.0.2 152 4/17/2024
2.0.1 140 3/26/2024
2.0.0 133 3/26/2024
1.0.5 128 3/17/2024
1.0.4 122 3/17/2024
1.0.3 141 3/17/2024
1.0.2 171 1/5/2024
1.0.1 132 1/4/2024
1.0.0 146 1/4/2024