namespace DataService; public class DBProcessData(Func KeyConverter, char spilter, string keyName) where T_PrimaryKey : unmanaged where T_Output : IDictionary, new() where T_DicValue : IDictionary, new() { public bool ToDictionary(IEnumerable inputs, out T_Output? output) { output = default; if (inputs is null) return false; T_Output cache = []; foreach (dynamic input in inputs) { if (input is null) return false; if ((input as IDictionary)?.TryGetValue(keyName, out object? primaryKeyValue) != true || primaryKeyValue is null) return false; if (KeyConverter?.Invoke(primaryKeyValue) is not T_PrimaryKey primaryKey) return false; (input as IDictionary)?.Remove(keyName); foreach (KeyValuePair rawData in input) ColumAnalizer(cache, rawData.Key.Split(spilter), primaryKey, rawData.Value); } output = cache; return true; } private static bool ColumAnalizer(/*ref*/ T_Output cache, Span seprated, T_PrimaryKey key, object value) { cache = TryGetValueElseCreateNew(cache, seprated[0]); _ = seprated.Length switch { 1 => TryGetValueElseCreateNew(cache, seprated[0]).TryAdd(key, value),//Recursion End _ => ColumAnalizer(cache, seprated[1..], key, value),//Recursion }; return true; } private static T TryGetValueElseCreateNew(T_Output cache, string key) where T : new() { if (!cache.TryGetValue(key, out object? output) || output is not T dic) { dic = new(); cache[key] = dic; } return dic; } } class GeneralHeaderAnalizer { public static DateTime? LongToDateTime(object o) { if (o is not long l) return null; return new(l); } } public class GeneralProcessData : DBProcessData, DateTime, Dictionary> { public GeneralProcessData() : base(GeneralHeaderAnalizer.LongToDateTime, '.', "time") { } }