Path: redb/redb.Core/Utils/TreeObjectConverter.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Utils/TreeObjectConverter.cs ---- using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; namespace redb.Core.Utils; /// /// Utility class for converting between RedbObject and TreeRedbObject types. /// Provides static methods for type conversion and parent chain building. /// public static class TreeObjectConverter { /// /// Convert RedbObject to TreeRedbObject preserving all properties. /// /// Object properties type /// Source RedbObject /// New TreeRedbObject with same data public static TreeRedbObject ToTreeObject(RedbObject source) where TProps : class, new() { return new TreeRedbObject { id = source.id, parent_id = source.parent_id, scheme_id = source.scheme_id, owner_id = source.owner_id, who_change_id = source.who_change_id, date_create = source.date_create, date_modify = source.date_modify, date_begin = source.date_begin, date_complete = source.date_complete, key = source.key, value_long = source.value_long, value_string = source.value_string, value_guid = source.value_guid, value_bool = source.value_bool, value_double = source.value_double, value_numeric = source.value_numeric, value_datetime = source.value_datetime, value_bytes = source.value_bytes, name = source.name, note = source.note, hash = source.hash, Props = source.Props }; } /// /// Convert IRedbObject to TreeRedbObject (creates new instance with Props = new TProps if types don't match). /// /// Target properties type /// Source object /// New TreeRedbObject public static TreeRedbObject ToTreeObject(IRedbObject source) where TProps : class, new() { // If source is already RedbObject, use direct conversion if (source is RedbObject typedSource) { return ToTreeObject(typedSource); } // Otherwise copy base properties var result = new TreeRedbObject { id = source.Id, parent_id = source.ParentId, scheme_id = source.SchemeId, owner_id = source.OwnerId, who_change_id = source.WhoChangeId, date_create = source.DateCreate, date_modify = source.DateModify, date_begin = source.DateBegin, date_complete = source.DateComplete, key = source.Key, value_long = source.ValueLong, value_string = source.ValueString, value_guid = source.ValueGuid, value_bool = source.ValueBool, value_double = source.ValueDouble, value_numeric = source.ValueNumeric, value_datetime = source.ValueDatetime, value_bytes = source.ValueBytes, value_unique = source.ValueUnique, name = source.Name, note = source.Note, hash = source.Hash, // Try to get Props if types match Props = (source as IRedbObject)?.Props ?? new TProps() }; return result; } /// /// Convert IRedbObject to ITreeRedbObject dynamically preserving actual Props type. /// Uses reflection to create correct TreeRedbObject generic type. /// /// Source object /// ITreeRedbObject with correct generic type public static ITreeRedbObject ToTreeObjectDynamic(IRedbObject source) { var sourceType = source.GetType(); // If source is generic RedbObject if (sourceType.IsGenericType && sourceType.GetGenericTypeDefinition() == typeof(RedbObject<>)) { var propsType = sourceType.GetGenericArguments()[0]; var treeType = typeof(TreeRedbObject<>).MakeGenericType(propsType); var treeObj = Activator.CreateInstance(treeType) as ITreeRedbObject; // Copy base properties. TreeRedbObject derives from RedbObject, not from TreeRedbObject: // tested against TreeRedbObject, nothing was copied and every object came back with id 0, so // LoadWithParentsAsync failed on the second object with "same key" while linking parents. if (treeObj is RedbObject treeBase && source is RedbObject redbBase) { CopyBaseProperties(redbBase, treeBase); } // Copy Props via reflection var propsProperty = sourceType.GetProperty("Props"); var targetPropsProperty = treeType.GetProperty("Props"); if (propsProperty != null && targetPropsProperty != null) { targetPropsProperty.SetValue(treeObj, propsProperty.GetValue(source)); } return treeObj!; } // Fallback for non-generic RedbObject return new TreeRedbObjectWrapper(source); } /// /// Build Parent relationships for a collection of tree objects. /// Links each object's Parent property to its parent in the collection. /// Also populates Children collections. /// /// Object properties type /// Collection of tree objects public static void BuildParentRelationships(IEnumerable> objects) where TProps : class, new() { var dict = objects.ToDictionary(o => o.id, o => o); foreach (var obj in objects) { obj.Parent = null; obj.Children.Clear(); if (obj.parent_id.HasValue && dict.TryGetValue(obj.parent_id.Value, out var parent)) { obj.Parent = parent; parent.Children.Add(obj); } } } /// /// Build Parent relationships for a collection of polymorphic tree objects. /// Links each object's Parent property to its parent in the collection. /// Also populates Children collections. /// /// Collection of tree objects public static void BuildParentRelationships(IEnumerable objects) { var dict = objects.ToDictionary(o => o.Id, o => o); foreach (var obj in objects) { obj.Parent = null; obj.Children.Clear(); if (obj.ParentId.HasValue && dict.TryGetValue(obj.ParentId.Value, out var parent)) { obj.Parent = parent; parent.Children.Add(obj); } } } /// /// Copy base properties from RedbObject to TreeRedbObject. /// private static void CopyBaseProperties(RedbObject source, RedbObject target) { target.id = source.id; target.parent_id = source.parent_id; target.scheme_id = source.scheme_id; target.owner_id = source.owner_id; target.who_change_id = source.who_change_id; target.date_create = source.date_create; target.date_modify = source.date_modify; target.date_begin = source.date_begin; target.date_complete = source.date_complete; target.key = source.key; target.value_long = source.value_long; target.value_string = source.value_string; target.value_guid = source.value_guid; target.value_bool = source.value_bool; target.value_double = source.value_double; target.value_numeric = source.value_numeric; target.value_datetime = source.value_datetime; target.value_bytes = source.value_bytes; target.value_unique = source.value_unique; target.name = source.name; target.note = source.note; target.hash = source.hash; } /// /// Simple wrapper for non-generic IRedbObject to ITreeRedbObject. /// Used when source object type is unknown at compile time. /// private class TreeRedbObjectWrapper : TreeRedbObject, ITreeRedbObject { /// /// Original source object. /// public IRedbObject SourceObject { get; } public TreeRedbObjectWrapper(IRedbObject source) { SourceObject = source; id = source.Id; parent_id = source.ParentId; scheme_id = source.SchemeId; owner_id = source.OwnerId; who_change_id = source.WhoChangeId; date_create = source.DateCreate; date_modify = source.DateModify; date_begin = source.DateBegin; date_complete = source.DateComplete; key = source.Key; value_long = source.ValueLong; value_string = source.ValueString; value_guid = source.ValueGuid; value_bool = source.ValueBool; value_double = source.ValueDouble; value_numeric = source.ValueNumeric; value_datetime = source.ValueDatetime; value_bytes = source.ValueBytes; value_unique = source.ValueUnique; name = source.Name; note = source.Note; hash = source.Hash; } } }