Fixed issue where singleton configuration cache was blocking creation of new components

This commit is contained in:
TheXamlGuy
2024-10-07 23:12:22 +01:00
parent 1c28659eac
commit f47e3deee9
6 changed files with 51 additions and 53 deletions
+21 -38
View File
@@ -9,6 +9,7 @@ namespace Toolkit.Foundation;
public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfiguration> configurationFile,
string section,
IConfigurationCache cache,
JsonSerializerOptions? serializerOptions = null) :
IConfigurationSource<TConfiguration>
where TConfiguration :
@@ -58,13 +59,13 @@ public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfigurati
using Stream stream2 = new FileStream(fileInfo.PhysicalPath!, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
JsonSerializer.Serialize(stream2, rootNode, serializerOptions ?? defaultSerializerOptions());
ConfigurationCache.Set(section, value);
cache.Set(section, value);
}
}
public bool TryGet(out TConfiguration? value)
{
if (ConfigurationCache.TryGet(section, out value))
if (cache.TryGet(section, out value))
{
return true;
}
@@ -102,7 +103,7 @@ public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfigurati
if (currentNode != null && currentNode[segments[lastIndex]] is JsonNode sectionNode)
{
value = JsonSerializer.Deserialize<TConfiguration>(sectionNode, serializerOptions ?? defaultSerializerOptions());
ConfigurationCache.Set(section, value);
cache.Set(section, value);
return true;
}
@@ -136,16 +137,9 @@ public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfigurati
if (currentNode is not null)
{
string lastKey = segments[lastIndex];
if (currentNode is JsonArray array && int.TryParse(lastKey, out int index))
if (valueNode is JsonArray)
{
if (array.Count <= index)
{
array.Add(valueNode);
}
else
{
array[index] = MergeNodes(array[index], valueNode);
}
currentNode[lastKey] = valueNode;
}
else
{
@@ -154,6 +148,20 @@ public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfigurati
}
}
private JsonNode? MergeNodes(JsonNode? existingNode, JsonNode? newNode)
{
if (existingNode is JsonObject existingObject && newNode is JsonObject newObject)
{
foreach (KeyValuePair<string, JsonNode?> property in newObject)
{
existingObject[property.Key] = MergeNodes(existingObject[property.Key], CloneNode(property.Value));
}
return existingObject;
}
return newNode;
}
private JsonNode? CloneNode(JsonNode? node)
{
if (node is null)
@@ -165,6 +173,7 @@ public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfigurati
return JsonNode.Parse(serialized);
}
private void EnsureFileExists(string? filePath)
{
if (filePath == null || File.Exists(filePath))
@@ -180,30 +189,4 @@ public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfigurati
File.WriteAllText(filePath, "{}");
}
private JsonNode? MergeNodes(JsonNode? existingNode, JsonNode? newNode)
{
if (existingNode is JsonObject existingObject && newNode is JsonObject newObject)
{
foreach (KeyValuePair<string, JsonNode?> property in newObject)
{
existingObject[property.Key] = MergeNodes(existingObject[property.Key], CloneNode(property.Value));
}
return existingObject;
}
else if (existingNode is JsonArray existingArray && newNode is JsonArray newArray)
{
foreach (JsonNode? item in newArray)
{
existingArray.Add(CloneNode(item));
}
return existingArray;
}
else
{
return newNode;
}
}
}