using System.Collections.Concurrent; using AyCode.Core.Extensions; using AyCode.Interfaces.Entities; namespace FruitBank.Common.Databases; public abstract class DatabaseLocalBase { //private ObservableCollection a = new ObservableCollection(); private readonly ConcurrentDictionary _database = new(); protected void AddTable(IList table)where TEntity: class, IEntityInt { if (GetTableObject() == null) { if (!_database.TryAdd(typeof(TEntity), table)) return; } return; } protected IList? GetTableObject() where T : class, IEntityInt { return _database.GetValueOrDefault(typeof(T)) as IList; } public IEnumerable? GetRows() where T : class, IEntityInt { return GetTableObject(); } public T? GetRow(int id) where T : class, IEntityInt { return GetTableObject()?.FirstOrDefault(x => x.Id == id) as T; } public T AddRow(T entity) where T : class, IEntityInt => AddRow(GetTableObject()!, entity); protected T AddRow(in IList table, T entity) where T : class, IEntityInt { table.Add(entity); return entity; } public void AddRows(IEnumerable entities) where T : class, IEntityInt { var table = GetTableObject()!; foreach (var entity in entities) { AddRow(table, entity); } } public T? DeleteRow(int id) where T : class, IEntityInt { var table = GetTableObject(); if (table == null) return null; var index = table.FindIndex(x => x.Id == id); if (index < 0) return null; var entity = table[index]; table.RemoveAt(index); return entity as T; } }