A lightweight C# library for local data persistence using JSON files. This service provides a simple and efficient way to store and retrieve typed data locally with automatic serialization/deserialization.
- Type-safe data access - Generic methods ensure compile-time type safety
- Automatic JSON serialization - Uses Newtonsoft.Json for reliable data persistence
- In-memory caching - Fast data access with automatic file synchronization
- File-based storage - Data stored as human-readable JSON files in
JSonDatadirectory
- Clone the repository:
git clone https://github.com/yourusername/local_data_service_CSharp.git
cd local_data_service_CSharp- Build the project:
dotnet build- Run the project:
dotnet run --project LocalDataServiceusing LocalDataService;
// Create an instance of the service
var localDataService = new LocalDatasService();
// Get data of a specific type
var dataList = localDataService.Get<List<DataB>>();
// Modify the data
dataList.Add(new DataB());
// Dispose to save changes automatically
localDataService.Dispose();using (var localDataService = new LocalDatasService())
{
var dataList = localDataService.Get<List<DataB>>();
dataList.Add(new DataB());
// Data is automatically saved when disposed
}- LocalDatasService - Main service class that manages data access and persistence
- DataRepository - Registers and configures available data types
- DataModel - Generic wrapper for typed data with persistence capabilities
- JsonManager - Handles JSON serialization/deserialization
- IDataModel - Interface defining the contract for data models
- Define your data class in
DatasRepository.cs:
public class MyNewData
{
public string Name { get; set; }
public int Value { get; set; }
}- Register it in
DataRepasitory.cs:
private void RegisterRepasitory()
{
// Existing registrations...
AddData<MyNewData>("MyDataFile");
}- Use it in your application:
var myData = localDataService.Get<MyNewData>();- All data is stored in the
JSonDatadirectory - Files are created automatically if they don't exist
- Data is saved as
.txtfiles containing JSON - Files are human-readable and can be manually edited if needed
The service throws an exception if you try to access a data type that hasn't been registered:
// This will throw an exception if UnregisteredType is not in the repository
var data = localDataService.Get<UnregisteredType>();
// Exception: "There is no such data in the local storage"