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.
- 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.Json
with support for custom converters. - File System Abstraction: Uses
System.IO.Abstractions
for 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 the package via NuGet:
dotnet add package ktsu.AppDataStorage
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
// 12
The 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 value
This functionality is useful for applications where centralized access to app data is required without repeatedly loading or instantiating objects.
Modify 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
// 42
For 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 exceeded
Write 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.Preserve
for circular references.- Converters such as
JsonStringEnumConverter
andToStringJsonConverter
.
Data is stored in a directory unique to the current application domain:
var appDataPath = AppData.Path;
Console.WriteLine($"App Data Path: {appDataPath}");
Backup and temporary files are automatically managed during save operations to ensure data integrity:
- Backup file extension:
.bk
- Temporary file extension:
.tmp
The library uses System.IO.Abstractions
, allowing you to inject a custom file system implementation for testing.
Contributions are welcome! Feel free to open issues or submit pull requests.
This project is licensed under the MIT License.