using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; namespace Mirea.Api.Endpoint.Sync.Common; internal class DataRepository where T : class { private readonly ConcurrentBag _data = []; private readonly object _lock = new(); public IEnumerable GetAll() => _data.ToList(); public DataRepository(List data) { foreach (var d in data) _data.Add(d); } public T? Get(Func predicate) { var entity = _data.FirstOrDefault(predicate); return entity; } public T Create(Func createEntity) { var entity = createEntity(); _data.Add(entity); return entity; } public T GetOrCreate(Func predicate, Func createEntity) { lock (_lock) { var entity = Get(predicate); return entity ?? Create(createEntity); } } }