A .NET library for simple application data management with JSON serialization.
ktsu.AppDataStorage is a .NET library designed to simplify the process of managing application data. It facilitates saving and loading configuration or state data to the application's data folder, leveraging JSON serialization. The library handles file operations with safety mechanisms like automatic backups and provides an intuitive API for developers.
- Easy-to-use API: Intuitive methods for saving and loading data.
- Automatic Backup: Backs up original files before overwriting to ensure data safety.
- Custom Serialization Options: Uses
System.Text.Jsonwith support for custom converters. - File System Abstraction: Uses
System.IO.Abstractionsfor easy unit testing and mocking. - Debounced Saves: Prevents frequent file writes to improve performance.
- Support for Multiple Applications: Organizes data by application domain for isolation.
- Static Instance Access: Provides easy access to a singleton-like instance for centralized data management.
Install-Package ktsu.AppDataStoragedotnet add package ktsu.AppDataStorage<PackageReference Include="ktsu.AppDataStorage" Version="x.y.z" />Create a class that inherits from AppData<T>, where T is your custom data type.
public class MyAppData : AppData<MyAppData>
{
public string Setting1 { get; set; } = "hello";
public int Setting2 { get; set; } = 12;
}Load existing data or create a new instance if no data file exists using LoadOrCreate.
var data = MyAppData.LoadOrCreate();
Console.WriteLine(data.Setting1);
Console.WriteLine(data.Setting2);
// Output:
// hello
// 12The AppData<T> class provides a static instance through the Get method, which ensures a single, easily accessible instance is available throughout your application:
var data = MyAppData.Get();
Console.WriteLine(data.Setting1);The static instance is initialized automatically and matches the instance returned by LoadOrCreate. Changes to the static instance are persistent once saved:
var data = MyAppData.Get();
data.Setting1 = "new value";
data.Save();
var sameData = MyAppData.Get();
Console.WriteLine(sameData.Setting1);
// Output:
// new valueModify properties and save the data using the Save method.
var data = MyAppData.Get();
data.Setting1 = "goodbye";
data.Setting2 = 42;
data.Save();
var reloadedData = MyAppData.Get();
Console.WriteLine(reloadedData.Setting1);
Console.WriteLine(reloadedData.Setting2);
// Output:
// goodbye
// 42For scenarios with frequent updates, you can queue save operations using QueueSave, which automatically debounces writes to avoid frequent file system operations.
MyAppData.QueueSave(); // Schedules a save
MyAppData.SaveIfRequired(); // Performs the save if the debounce threshold is exceededWrite and read arbitrary files in the application's data folder using the static AppData class.
AppData.WriteText("example.txt".As<FileName>(), "Hello, AppData!");string content = AppData.ReadText("example.txt".As<FileName>());
Console.WriteLine(content);
// Output:
// Hello, AppData!Serialization behavior can be customized using JsonSerializerOptions. By default, the library uses:
- Indented JSON for readability.
ReferenceHandler.Preservefor circular references.- Converters such as
JsonStringEnumConverterandToStringJsonConverter.
Data is stored in a directory unique to the current application domain:
var appDataPath = AppData.Path;
Console.WriteLine($"App Data Path: {appDataPath}");The primary static class for working with application data storage.
| Name | Type | Description |
|---|---|---|
Path |
AbsoluteDirectoryPath |
The path where persistent data is stored for this application |
| Name | Return Type | Description |
|---|---|---|
WriteText<T>(T appData, string text) |
void |
Writes text to an app data file with backup safety |
ReadText<T>(T appData) |
string |
Reads text from an app data file |
QueueSave<T>(this T appData) |
void |
Queues a save operation for the app data |
SaveIfRequired<T>(this T appData) |
void |
Saves the app data if required based on debounce settings |
Base class for app data storage implementations.
| Name | Type | Description |
|---|---|---|
FilePath |
AbsoluteFilePath |
The file path for the app data file |
| Name | Return Type | Description |
|---|---|---|
Get() |
T |
Gets the current instance of the app data |
LoadOrCreate() |
T |
Loads app data from file or creates a new instance |
Save() |
void |
Saves the app data to the file system |
QueueSave() |
void |
Queues a save operation for the current app data instance |
SaveIfRequired() |
void |
Saves the app data if required based on debounce settings |
Contributions are welcome! Feel free to open issues or submit pull requests.
This project is licensed under the MIT License. See the LICENSE.md file for details.