SettingsDb 1.2.1

dotnet add package SettingsDb --version 1.2.1
NuGet\Install-Package SettingsDb -Version 1.2.1
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="SettingsDb" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SettingsDb --version 1.2.1
#r "nuget: SettingsDb, 1.2.1"
#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.
// Install SettingsDb as a Cake Addin
#addin nuget:?package=SettingsDb&version=1.2.1

// Install SettingsDb as a Cake Tool
#tool nuget:?package=SettingsDb&version=1.2.1

nuget nuget Build Tests

SettingsDb

SettingsDb is a .NET Standard library that allows to manage persistence of application settings in a simple and fast way. Settings values are serialized as JSON objects and stored in a database. This offers a great level of flexibility in terms of what type of data can be stored.

Any type of database can be used, as long as there is an specialization of DbConnection for it.

Installation

Using NuGet package manager console:

Install-Package SettingsDb

Using .NET CLI:

dotnet add package SettingsDb

Usage

Just create an instance of the SettingsDb class specifying the type of connection, the connection string and (optionally) the database table to use. If no table name is specified, "Settings" will be used.

// Use a SQL Server database to store the settings.
var settings = new DbSettings<SqlConnection>("Database=myDataBase");
// Use a SQL Server database to store the settings in the "AppSettings" table.
var settings = new DbSettings<SqlConnection>("Database=myDataBase", "AppSettings");

When using a SQLite database, Settings class can be used instead. Settings acts as a shortcut for DbSettings<SqliteConnection> and provides backwards compatibility with previous versions of the library.

// Use SQLite to store the settings.
var settings = new Settings();

// This is equivalent to use:
var settings = new DbSettings<SqliteConnection>($"Data Source={Assembly.GetEntryAssembly().GetName().Name}.Settings.db");
// Use SQLite to store the settings in the "AppSettings" table.
var settings = new Settings("AppSettings");

// This is equivalent to use:
var settings = new DbSettings<SqliteConnection>($"Data Source={Assembly.GetEntryAssembly().GetName().Name}.Settings.db", "AppSettings");
// Use SQLite to store the settings in the "AppSettings" table, using "Settings.db" as the database file name.
var settings = new Settings("Settings", "AppSettings");

// This is equivalent to use:
var settings = new DbSettings<SqliteConnection>("Data Source=Settings.db", "AppSettings");

When a new instance of the Settings class is created, it checks for existence of the database file and creates it if necessary. By default, the assembly name of the application will be used as the database name, but you can specify any other name in the constructor.

Storing a Value

To store a value, just call the Store method (or its async version StoreAsync) passing the name to be used for the setting and its value:

public void Store<T>(string settingName, T value);
public async Task StoreAsync<T>(string settingName, T value);
var settings = new Settings();

settings.Store("UserName", "John Doe");
settings.Store("ID", 12345);
await settings.StoreAsync("ShowToolbar", true);

If a setting with that name already exists, its value is replaced.

Remember that specified values are serialized to JSON strings prior to be stored into the database, so you can even pass in simple objects:

class WindowPosition
{
    public int X { get; set; }
    public int Y { get; set; }
}

var settings = new Settings();

settings.Store("WindowPosition", new WindowPosition { X = 250; Y = 100});

Reading a Value

To read a value from the database, you use any of the Read or ReadAsync generic methods, passing the name of the setting, the expected type to be returned and, optionally, a default value to be used if the specified setting is not found in the database.

public T Read<T>(string settingName, T defaultValue = default);
public async Task<T> ReadAsync<T>(string settingName, T defaultValue = default);
var settings = new Settings();

var showToolbar = settings.Read<bool>("ShowToolbar", true);
var windowPosition = await settings.ReadAsync<WindowPosition>("WindowPosition");

If no default value is specified and the setting name is not found, SettingsDb will return the default value for the requested data type.

Other Operations

public void Clear(string settingName);
public Task ClearAsync(string settingName);
public void ClearAll();
public Task ClearAllAsync();
public long Count();
public long CountAsync();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.2.1 693 5/8/2022
1.2.0 370 5/8/2022
1.1.0 400 3/18/2022
1.0.0 391 3/15/2022

SettingsDb class now implements a ISettingsDb interface (useful when using Dependency Injection)