This commit is contained in:
Pilzinsel64
2025-07-31 11:08:13 +02:00
parent 839f90d16a
commit 2c8c307cdd
4 changed files with 18 additions and 10 deletions

View File

@@ -2,5 +2,5 @@
public interface IDataObject
{
public int Id { get; }
public int Id { get; set; }
}

View File

@@ -4,18 +4,24 @@ namespace Pilz.Data.Json;
public abstract class JsonDataContainer
{
public class DataSet<T>
{
public int LastId { get; set; }
public HashSet<T> Data { get; } = [];
}
[JsonProperty("Sets")]
protected readonly HashSet<object> sets = [];
public virtual HashSet<T>? Set<T>(bool allowCreate) where T : class, IDataObject
public virtual DataSet<T>? Set<T>(bool allowCreate) where T : class, IDataObject
{
lock (sets)
{
var list = sets.OfType<HashSet<T>>().FirstOrDefault();
var list = sets.OfType<DataSet<T>>().FirstOrDefault();
if (list == null && allowCreate)
{
list = [];
list = new();
sets.Add(list);
}

View File

@@ -8,22 +8,24 @@ public class JsonDataManager(JsonDataContainer container) : DataManager
{
if (id == 0)
return null;
return container.Set<T>(false)?.FirstOrDefault(n => n.Id == id);
return container.Set<T>(false)?.Data.FirstOrDefault(n => n.Id == id);
}
protected override IQueryable<T> GetEntitySet<T>()
{
return container.Set<T>(true).AsQueryable();
return container.Set<T>(true).Data.AsQueryable();
}
protected override void RemoveEntity<T>(T obj)
{
container.Set<T>(false)?.Remove(obj);
container.Set<T>(false)?.Data.Remove(obj);
}
protected override void UpdateEntity<T>(T obj)
{
container.Set<T>(true).Add(obj);
var set = container.Set<T>(true);
obj.Id = ++set.LastId;
set.Data.Add(obj);
}
protected override void SaveChanges()

View File

@@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>annotations</Nullable>
<Version>2.4.5</Version>
<Version>2.4.6</Version>
</PropertyGroup>
<ItemGroup>