Added SZ singleton and services
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
using System;
|
||||
using SzCore;
|
||||
using SzCore.Defaults;
|
||||
|
||||
namespace SzCli;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static readonly ISzFileManager _fileManager = new LocalFileManager();
|
||||
private static readonly ISzLogger _logger = new Logger(_fileManager);
|
||||
private static readonly DefaultLocalFileManager _fileManager = new();
|
||||
private static readonly Logger _logger = new(_fileManager);
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
@@ -24,6 +25,6 @@ public class Program
|
||||
var loadedDataset = parser.LoadDataset(dataset.Id);
|
||||
_logger.Log($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}");
|
||||
|
||||
_logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value);
|
||||
_logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value ?? "Could not get value of test.value");
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,28 @@ public static class Test
|
||||
{
|
||||
public static SzDataset CreateTestDataset()
|
||||
{
|
||||
var dt = new SzDataObjectTemplate(){
|
||||
Name = "SzCore - Basic Item",
|
||||
Id = "szcore-item-basic",
|
||||
DataObjectType = "item",
|
||||
Uuid = Guid.NewGuid()
|
||||
};
|
||||
|
||||
var testTemplateField = new SzTemplateField()
|
||||
{
|
||||
Id = "cost",
|
||||
FieldType = SzFieldType.Number,
|
||||
DefaultValue = "20"
|
||||
};
|
||||
|
||||
dt.TemplateFields.Add(testTemplateField.Id, testTemplateField);
|
||||
|
||||
var ds = new SzDataset()
|
||||
{
|
||||
Name = "TestDataset",
|
||||
Id = "test-dataset",
|
||||
DataObjectTemplateId = "0",
|
||||
DataObjectTemplateId = dt.Id,
|
||||
DataObjectTemplateUuid = dt.Uuid,
|
||||
DataObjectType = "item",
|
||||
Uuid = Guid.NewGuid(),
|
||||
};
|
||||
|
||||
@@ -8,5 +8,6 @@ public class SzDataset
|
||||
|
||||
public required string DataObjectType {get; set;}
|
||||
public required string DataObjectTemplateId {get; set;}
|
||||
public Guid? DataObjectTemplateUuid {get; set;}
|
||||
public Dictionary<string, SzDataObject> DataObjects {get; set;} = [];
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ namespace SzCore.DataObjects;
|
||||
|
||||
public class SzTemplateField
|
||||
{
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public required SzFieldType FieldType { get; set; }
|
||||
public bool IsList {get; set;} = false;
|
||||
public string DefaultValue {get; set;} = "";
|
||||
public bool IsSpecialType {get; set;} = false;
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
using System.Reflection.Metadata;
|
||||
using SzCore;
|
||||
using SzCore.DataObjects;
|
||||
namespace SzCore.Defaults;
|
||||
|
||||
namespace SzCli;
|
||||
|
||||
public class LocalFileManager : ISzFileManager
|
||||
public class DefaultLocalFileManager : ISzFileManager
|
||||
{
|
||||
public string DataPath
|
||||
{
|
||||
@@ -52,4 +48,4 @@ public class LocalFileManager : ISzFileManager
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
107
SzCore/SZ.cs
Normal file
107
SzCore/SZ.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace SzCore;
|
||||
|
||||
public static class SZ
|
||||
{
|
||||
private static ISzFileManager? _fileManager;
|
||||
private static ISzLogger? _logger;
|
||||
|
||||
public static bool IsInitalized { get; private set; } = false;
|
||||
|
||||
public static ISzFileManager LocalFileManager
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckInitialization();
|
||||
if (_fileManager is null) throw new Exception("SZ._fileManager is null");
|
||||
return _fileManager;
|
||||
}
|
||||
}
|
||||
|
||||
public static ISzLogger Logger
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckInitialization();
|
||||
if (_logger is null) throw new Exception("SZ._logger is null");
|
||||
return _logger;
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<Type, object> _services = [];
|
||||
private static List<Type> _protectedServices = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initalizes the SZ singleton in SzCore
|
||||
/// </summary>
|
||||
public static void Init(ISzFileManager fileManager, ISzLogger logger)
|
||||
{
|
||||
if (IsInitalized) throw new Exception("Cannot initalize SZ more than once.");
|
||||
|
||||
_logger = logger;
|
||||
_fileManager = fileManager;
|
||||
|
||||
if (_logger is null)
|
||||
{
|
||||
throw new Exception("SZ.Init failed: Logger was null");
|
||||
}
|
||||
|
||||
if (_fileManager is null)
|
||||
{
|
||||
throw new Exception("SZ.Init failed: LocalFileManager was null");
|
||||
}
|
||||
|
||||
AddService(fileManager, true);
|
||||
AddService(logger, true);
|
||||
|
||||
IsInitalized = true;
|
||||
|
||||
logger.Log(" -- SZ CORE INITALIZED -- ");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to add a service
|
||||
/// </summary>
|
||||
/// <param name="instance">The object instance.</param>
|
||||
/// <param name="isProtected">Sets if the service should be protected (not able to be deleted).</param>
|
||||
/// <returns>True if the service was successfully added. False otherwise.</returns>
|
||||
/// <exception cref="Exception">Throws if SZ.CheckInitialization() fails</exception>
|
||||
public static bool AddService(object instance, bool isProtected = false)
|
||||
{
|
||||
CheckInitialization();
|
||||
|
||||
var result = _services.TryAdd(instance.GetType(), instance);
|
||||
if (!result) return false;
|
||||
|
||||
if (isProtected)
|
||||
{
|
||||
if (_protectedServices.Contains(instance.GetType())) return false;
|
||||
|
||||
_protectedServices.Add(instance.GetType());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static T? GetService<T>()
|
||||
{
|
||||
CheckInitialization();
|
||||
|
||||
if (!_services.ContainsKey(typeof(T))) return default(T);
|
||||
return (T)_services[typeof(T)];
|
||||
}
|
||||
|
||||
public static bool RemoveService(Type type)
|
||||
{
|
||||
CheckInitialization();
|
||||
|
||||
if (_protectedServices.Contains(type)) return false;
|
||||
return _services.Remove(type);
|
||||
}
|
||||
|
||||
private static void CheckInitialization()
|
||||
{
|
||||
if (!IsInitalized) throw new Exception("SZ.Init has not been called.");
|
||||
}
|
||||
}
|
||||
6
SzCore/SzDataHandler.cs
Normal file
6
SzCore/SzDataHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace SzCore;
|
||||
|
||||
public static class SzDataHandler
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user