Skip to content

Commit 8b25ead

Browse files
committed
Merge branch 'main' of https://github.com/igece/SettingsDb
2 parents 2dc1679 + 763d371 commit 8b25ead

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,68 @@
33
[![Build](https://github.com/igece/SettingsDb/actions/workflows/build.yml/badge.svg)](https://github.com/igece/SettingsDb/actions/workflows/build.yml)
44
[![Tests](https://github.com/igece/SettingsDb/actions/workflows/tests.yml/badge.svg)](https://github.com/igece/SettingsDb/actions/workflows/tests.yml)
55

6-
# SettingsDb
7-
Store application settings in a SQLite database.
6+
## SettingsDb
7+
SettingsDb is a .NET Standard library that allows to manage persistence of application settings in a simple, fast and flexible way. The settings are stored in a [SQLite](http://sqlite.org) database located in the same directory as the application.
8+
9+
### How It Works
10+
11+
Settings are stored into a single table in the database, using a pair of "Setting Name" and "Value" values. Internally, all the values are serialized to a JSON string and then inserted into the table, this offers a great level of flexibility in terms of what type of data can be stored.
12+
13+
#### Initial Setup
14+
15+
When a new instance of the `Settings` class is created, it checks for existence of the database file and creates it if necessary. You can pass the name to be used for the database file in the constructor, but if no name is specified (parameterless contructor) the assembly name of the application will be used as the database name.
16+
17+
#### Storing a Value
18+
19+
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:
20+
21+
``` C#
22+
void Store<T>(string settingName, T value)
23+
```
24+
``` C#
25+
var settings = new Settings();
26+
27+
settings.Store("UserName", "John Doe");
28+
settings.Store("ID", 12345);
29+
settings.Store("ShowToolbar", true);
30+
```
31+
32+
Remember that specified values are serialized to JSON strings prior to be stored into the database, so you can even pass in simple objects:
33+
34+
``` C#
35+
class WindowPosition
36+
{
37+
public int X { get; set; }
38+
public int Y { get; set; }
39+
}
40+
41+
var settings = new Settings();
42+
43+
settings.Store("WindowPosition", new WindowPosition { X = 250; Y = 100});
44+
```
45+
46+
#### Reading a Value
47+
48+
To read a value from the database, you use any of the `Read` or `ReadAsync` generic methods, indicating the name of the setting, the expected type to be returned
49+
and, optionally, a default value to be used if the specified setting is not found in the database.
50+
51+
``` C#
52+
public T Read<T>(string settingName, T defaultValue = default)
53+
```
54+
``` C#
55+
var settings = new Settings();
56+
57+
var showToolbar = settings.Read<bool>("ShowToolbar", true);
58+
var windowPosition = settings.Read<WindowPosition>("WindowPosition");
59+
```
60+
61+
If no default value is specified, SettingsDb will use the default value for the requested data type.
62+
63+
#### Other Operations
64+
65+
``` C#
66+
void Clear(string settingName)
67+
```
68+
``` C#
69+
void ClearAll()
70+
```

0 commit comments

Comments
 (0)