57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using Microsoft.Extensions.Options;
|
|
using RobotNet10.CustomConfiguration.Events;
|
|
using RobotNet10.CustomConfiguration.Models;
|
|
using RobotNet10.StorageManager;
|
|
|
|
namespace RobotNet10.CustomConfiguration.Services;
|
|
|
|
/// <summary>
|
|
/// Service implementation cho quản lý configuration files
|
|
/// Sử dụng StorageManager để lưu trữ file JSON
|
|
/// </summary>
|
|
public partial class ConfigService : IConfigService, IDisposable
|
|
{
|
|
private readonly StorageManager.StorageManager _storageManager;
|
|
private const string ConfigPath = "configs"; // Path trong StorageManager
|
|
private const string ConfigFileExtension = ".config.json"; // Extension cho config files
|
|
private const string StorageConfigsKey = "StorageConfigs"; // Named options key cho IOptionsMonitor
|
|
|
|
// ==========================================
|
|
// EVENTS
|
|
// ==========================================
|
|
|
|
public event EventHandler<ConfigChangedEventArgs>? ConfigChanged;
|
|
|
|
public ConfigService(IOptionsMonitor<StorageConfig> optionsSnapshot)
|
|
{
|
|
var config = optionsSnapshot.Get(StorageConfigsKey);
|
|
|
|
ArgumentNullException.ThrowIfNull(config);
|
|
|
|
_storageManager = new StorageManager.StorageManager(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trigger ConfigChanged event
|
|
/// </summary>
|
|
protected virtual void OnConfigChanged(ConfigFile config, ConfigChangeType changeType, string? variableName = null)
|
|
{
|
|
var args = new ConfigChangedEventArgs
|
|
{
|
|
ConfigType = config.ConfigType,
|
|
ConfigId = config.Id,
|
|
ChangeType = changeType,
|
|
VariableName = variableName
|
|
};
|
|
|
|
ConfigChanged?.Invoke(this, args);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_storageManager?.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|
|
|