Path: redb/redb.Core/Models/Entities/TreeRedbObject.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/Models/Entities/TreeRedbObject.cs
----
using redb.Core.Models.Contracts;
using redb.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
namespace redb.Core.Models.Entities
{
///
/// Base class for REDB tree objects with hierarchy navigation
/// Supports polymorphic trees - objects of different schemes in one tree
/// Extends RedbObject by adding navigational properties and traversal methods
///
public class TreeRedbObject : RedbObject, ITreeRedbObject
{
///
/// Reference to parent object (filled when loading tree)
///
public ITreeRedbObject? Parent { get; set; }
///
/// Collection of child objects (filled when loading tree)
///
public ICollection Children { get; set; } = new List();
///
/// Checks if node is leaf (has no children)
///
public bool IsLeaf => !Children.Any();
///
/// Gets node level in tree (0 for root)
///
public int Level
{
get
{
int level = 0;
var current = Parent;
while (current != null)
{
level++;
current = current.Parent;
}
return level;
}
}
///
/// Gets all node ancestors (from parent to root)
///
public IEnumerable Ancestors
{
get
{
var current = Parent;
while (current != null)
{
yield return current;
current = current.Parent;
}
}
}
///
/// Gets all node descendants recursively
///
public IEnumerable Descendants
{
get
{
foreach (var child in Children)
{
yield return child;
foreach (var descendant in child.Descendants)
yield return descendant;
}
}
}
///
/// Gets path from root to current node as IDs
///
public IEnumerable GetPathIds()
{
var path = new List();
var current = this;
while (current != null)
{
path.Insert(0, current.Id);
current = current.Parent as TreeRedbObject;
}
return path;
}
///
/// Gets breadcrumbs for navigation
///
/// Separator between elements
/// Whether to include IDs in parentheses
/// String like "Root > Category > Subcategory"
public string GetBreadcrumbs(string separator = " > ", bool includeIds = false)
{
var ancestors = Ancestors.Reverse().ToList();
var path = ancestors.Append(this);
var names = path.Select(node =>
{
var displayName = node.Name ?? $"Object {node.Id}";
return includeIds ? $"{displayName} ({node.Id})" : displayName;
});
return string.Join(separator, names);
}
///
/// Checks if current node is descendant of specified node
///
/// Alleged ancestor
/// true if current is descendant of ancestor
public bool IsDescendantOf(ITreeRedbObject ancestor)
{
var current = Parent;
while (current != null)
{
if (current.Id == ancestor.Id)
return true;
current = current.Parent;
}
return false;
}
///
/// Checks if current node is ancestor of specified node
///
/// Alleged descendant
/// true if current is ancestor of descendant
public bool IsAncestorOf(ITreeRedbObject descendant)
{
return descendant.IsDescendantOf(this);
}
///
/// Gets all subtree nodes (including current) in depth-first order
///
/// Sequence of subtree nodes
public IEnumerable GetSubtree()
{
yield return this;
foreach (var child in Children)
{
foreach (var node in child.GetSubtree())
yield return node;
}
}
///
/// Gets number of nodes in subtree (including current)
///
public int SubtreeSize => GetSubtree().Count();
///
/// Gets maximum depth of subtree from current node
///
public int MaxDepth
{
get
{
if (!Children.Any())
return 0;
return 1 + Children.Max(child => child.MaxDepth);
}
}
///
/// Recompute MD5 hash from object values and store in hash field
///
public override void RecomputeHash()
{
hash = RedbHash.ComputeFor((IRedbObject)this);
}
///
/// Get MD5 hash from object values without modifying hash field
///
public override Guid ComputeHash() => RedbHash.ComputeFor((IRedbObject)this) ?? Guid.Empty;
}
///
/// ARCHITECTURAL FIX: Typed version of REDB tree object
/// NEW INHERITANCE: RedbObject<TProps> instead of TreeRedbObject
/// ADVANTAGES: Direct type casting, Props duplication elimination, no conversion
/// POLYMORPHISM: Implements ITreeRedbObject for untyped operations support
///
/// Object properties type
public class TreeRedbObject : RedbObject, ITreeRedbObject, ITreeRedbObject
where TProps : class, new()
{
// FIX: Props inherited from RedbObject - duplication eliminated!
// === TREE-SPECIFIC PROPERTIES - untyped navigation ===
// Supports polymorphic trees - parents and children can be different types
///
/// Reference to parent object (untyped to support polymorphic trees)
///
public ITreeRedbObject? Parent { get; set; }
///
/// Collection of child objects (untyped to support polymorphic trees)
///
public ICollection Children { get; set; } = new List();
// === TREE NAVIGATION PROPERTIES (moved from base TreeRedbObject) ===
///
/// Checks if node is leaf (has no children)
///
public bool IsLeaf => !Children.Any();
///
/// Gets node level in tree (0 for root)
///
public int Level
{
get
{
int level = 0;
var current = Parent;
while (current != null)
{
level++;
current = current.Parent;
}
return level;
}
}
///
/// Gets all subtree nodes (including current) in depth-first order
///
public IEnumerable GetSubtree()
{
yield return this;
foreach (var child in Children)
{
foreach (var node in child.GetSubtree())
yield return node;
}
}
///
/// Gets all node ancestors (from parent to root)
///
public IEnumerable Ancestors
{
get
{
var current = Parent;
while (current != null)
{
yield return current;
current = current.Parent;
}
}
}
///
/// Gets all node descendants recursively
///
public IEnumerable Descendants
{
get
{
foreach (var child in Children)
{
yield return child;
foreach (var descendant in child.Descendants)
yield return descendant;
}
}
}
// === NAVIGATION METHODS (moved from base TreeRedbObject) ===
///
/// Gets path from root to current node as IDs
///
public IEnumerable GetPathIds()
{
var path = new List();
var current = (ITreeRedbObject?)this;
while (current != null)
{
path.Insert(0, current.Id);
current = current.Parent;
}
return path;
}
///
/// Gets breadcrumbs for navigation
///
public string GetBreadcrumbs(string separator = " > ", bool includeIds = false)
{
var ancestors = Ancestors.Reverse().ToList();
var path = ancestors.Append(this);
var names = path.Select(node =>
{
var displayName = node.Name ?? $"Object {node.Id}";
return includeIds ? $"{displayName} ({node.Id})" : displayName;
});
return string.Join(separator, names);
}
///
/// Checks if current node is descendant of specified node
///
public bool IsDescendantOf(ITreeRedbObject ancestor)
{
var current = Parent;
while (current != null)
{
if (current.Id == ancestor.Id)
return true;
current = current.Parent;
}
return false;
}
///
/// Checks if current node is ancestor of specified node
///
public bool IsAncestorOf(ITreeRedbObject descendant)
{
return descendant.IsDescendantOf(this);
}
///
/// Gets number of nodes in subtree (including current)
///
public int SubtreeSize => GetSubtree().Count();
///
/// Gets maximum depth of subtree from current node
///
public int MaxDepth
{
get
{
if (!Children.Any())
return 0;
return 1 + Children.Max(child => child.MaxDepth);
}
}
// Cache and metadata methods inherited from RedbObject
// Navigation methods now directly implement ITreeRedbObject (untyped)
// Explicit interface implementations no longer needed!
}
}