update
This commit is contained in:
@@ -2,5 +2,5 @@
|
||||
|
||||
public interface IDataObject
|
||||
{
|
||||
public int Id { get; }
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user