Skip to content

HARUTDOKHOYAN/local_data_service_CSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

LocalDataService

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.

🚀 Features

  • 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 JSonData directory

🛠️ Installation

  1. Clone the repository:
git clone https://github.com/yourusername/local_data_service_CSharp.git
cd local_data_service_CSharp
  1. Build the project:
dotnet build
  1. Run the project:
dotnet run --project LocalDataService

📖 Usage

Basic Usage

using 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 Statement (Recommended)

using (var localDataService = new LocalDatasService())
{
    var dataList = localDataService.Get<List<DataB>>();
    dataList.Add(new DataB());
    
    // Data is automatically saved when disposed
}

🏗️ Architecture

Core Components

  1. LocalDatasService - Main service class that manages data access and persistence
  2. DataRepository - Registers and configures available data types
  3. DataModel - Generic wrapper for typed data with persistence capabilities
  4. JsonManager - Handles JSON serialization/deserialization
  5. IDataModel - Interface defining the contract for data models

🔧 Configuration

Adding New Data Types

  1. Define your data class in DatasRepository.cs:
public class MyNewData
{
    public string Name { get; set; }
    public int Value { get; set; }
}
  1. Register it in DataRepasitory.cs:
private void RegisterRepasitory()
{
    // Existing registrations...
    AddData<MyNewData>("MyDataFile");
}
  1. Use it in your application:
var myData = localDataService.Get<MyNewData>();

💾 Data Storage

  • All data is stored in the JSonData directory
  • Files are created automatically if they don't exist
  • Data is saved as .txt files containing JSON
  • Files are human-readable and can be manually edited if needed

🔒 Error Handling

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"

About

A lightweight C# library for local data persistence using JSON files.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages