// // PropertyBag.cs // // Author: // beckermj <> // // Copyright (c) 2023 ${CopyrightHolder} // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; using System.Collections.Generic; namespace MBS.Framework { public class PropertyBag { private Dictionary _settings = new Dictionary(); private Dictionary _names = new Dictionary(); public string GetName(Guid id) { if (_names.ContainsKey(id)) return _names[id]; return null; } public void SetName(Guid id, string name) { _names[id] = name; } private Dictionary _PropertyValues = new Dictionary(); public bool Contains(Guid id) { return _PropertyValues.ContainsKey(id); } public bool SetValue(Guid id, T value) { bool changed = false; lock (_PropertyValues) { object oldValue = null; if (!_PropertyValues.ContainsKey(id) || (!( (_PropertyValues[id] == null && (value as object) == null) || (_PropertyValues[id] != null && _PropertyValues[id].Equals(value))))) { changed = true; } if (_PropertyValues.ContainsKey(id)) { oldValue = _PropertyValues[id]; } if (changed) { PropertyValueChangingEventArgs e = new PropertyValueChangingEventArgs(id, oldValue, value); OnPropertyValueChanging(e); if (e.Cancel) { return false; } } _PropertyValues[id] = value; if (changed) { OnPropertyValueChanged(new PropertyValueChangedEventArgs(id, oldValue, value)); } } return changed; } public event EventHandler PropertyValueChanging; protected virtual void OnPropertyValueChanging(PropertyValueChangingEventArgs e) { PropertyValueChanging?.Invoke(this, e); } public event EventHandler PropertyValueChanged; protected virtual void OnPropertyValueChanged(PropertyValueChangedEventArgs e) { PropertyValueChanged?.Invoke(this, e); } public T GetValue(Guid id, T defaultValue = default(T)) { lock (_PropertyValues) { if (_PropertyValues.ContainsKey(id)) { if (_PropertyValues[id] is T val) { return val; } } else { PropertyValueRequestedEventArgs e = new PropertyValueRequestedEventArgs(id, defaultValue); OnPropertyValueRequested(e); if (e.Handled) { if (e.Cache) { _PropertyValues[id] = e.Value; } return e.Value; } } } return defaultValue; } public event EventHandler PropertyValueRequested; protected virtual void OnPropertyValueRequested(PropertyValueRequestedEventArgs e) { PropertyValueRequested?.Invoke(this, e); } public IEnumerable> GetAll() { return _PropertyValues; } public bool Initialized { get; private set; } = false; protected virtual void OnInitialized(EventArgs e) { } public void Initialize() { if (Initialized) return; OnInitialized(EventArgs.Empty); Initialized = true; } } }