Path: redb/redb.Core/Extensions/RedbObjectExtensions.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/Extensions/RedbObjectExtensions.cs ---- using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Core.Providers; using redb.Core.Utils; namespace redb.Core.Extensions { /// /// Extension methods for IRedbObject for convenient work with trees and objects /// public static class RedbObjectExtensions { // ===== SOFT DELETE ===== /// /// Checks whether the object has been soft-deleted (scheme changed to @@__deleted). /// public static bool IsSoftDeleted(this IRedbObject obj) => obj.SchemeId == RedbTypeIds.DeletedScheme; // ===== TREE OPERATIONS ===== /// /// OPTIMIZED version: Checks if object is descendant of specified parent /// Uses already loaded hierarchy from GetPathToRootAsync, but adds cycle protection /// /// Object to check /// Potential ancestor /// Provider for tree operations public static async Task IsDescendantOfAsync( this IRedbObject obj, IRedbObject potentialAncestor, ITreeProvider treeProvider) where T : class, new() { if (obj.Id == potentialAncestor.Id) return false; // Object cannot be descendant of itself try { // Use existing method, but with cycle protection var pathToRoot = await treeProvider.GetPathToRootAsync(obj); // OPTIMIZATION: Use HashSet for O(1) search instead of linear scan var ancestorIds = new HashSet(pathToRoot.Select(ancestor => ancestor.Id)); return ancestorIds.Contains(potentialAncestor.Id); } catch { return false; // In case of error return false } } /// /// Checks if object is an ancestor of the specified descendant /// /// Object to check /// Potential descendant /// Provider for tree operations public static async Task IsAncestorOfAsync( this IRedbObject obj, IRedbObject potentialDescendant, ITreeProvider treeProvider) where T : class, new() { return await potentialDescendant.IsDescendantOfAsync(obj, treeProvider); } /// /// OPTIMIZED version: Gets object level in tree (root = 0) /// Uses cycle protection and more efficient approach /// /// Object /// Provider for tree operations public static async Task GetTreeLevelAsync( this IRedbObject obj, ITreeProvider treeProvider) where T : class, new() { try { var pathToRoot = await treeProvider.GetPathToRootAsync(obj); var pathCount = pathToRoot.Count(); // PROTECTION: If path is too long, there may be a cycle if (pathCount > 1000) { return -1; // Suspiciously deep tree - possible cycle } return Math.Max(0, pathCount - 1); // -1 because path includes the object itself } catch { return -1; // Error determining level } } /// /// Checks if object is a tree leaf (without children) /// /// Object /// Provider for tree operations public static async Task IsLeafAsync( this IRedbObject obj, ITreeProvider treeProvider) where T : class, new() { try { var children = await treeProvider.GetChildrenAsync(obj); return !children.Any(); } catch { return true; // In case of error consider as leaf } } /// /// Gets count of object children /// /// Object /// Provider for tree operations public static async Task GetChildrenCountAsync( this IRedbObject obj, ITreeProvider treeProvider) where T : class, new() { try { var children = await treeProvider.GetChildrenAsync(obj); return children.Count(); } catch { return 0; } } /// /// Gets count of all object descendants /// /// Object /// Provider for tree operations /// Maximum search depth public static async Task GetDescendantsCountAsync( this IRedbObject obj, ITreeProvider treeProvider, int? maxDepth = null) where T : class, new() { try { var descendants = await treeProvider.GetDescendantsAsync(obj, maxDepth); return descendants.Count(); } catch { return 0; } } // ===== STATE CHECKS ===== /// /// Checks if object is active by timestamps /// /// Object /// Date to check (default - current) public static bool IsActiveAt(this IRedbObject obj, DateTime? checkDate = null) { var date = checkDate ?? DateTime.Now; // Check begin date if (obj.DateBegin.HasValue && date < obj.DateBegin.Value) return false; // Check end date if (obj.DateComplete.HasValue && date > obj.DateComplete.Value) return false; return true; } /// /// Checks if object validity has expired /// /// Object /// Date to check (default - current) public static bool IsExpired(this IRedbObject obj, DateTime? checkDate = null) { var date = checkDate ?? DateTime.Now; return obj.DateComplete.HasValue && date > obj.DateComplete.Value; } /// /// Checks if object has started its validity /// /// Object /// Date to check (default - current) public static bool HasStarted(this IRedbObject obj, DateTime? checkDate = null) { var date = checkDate ?? DateTime.Now; return !obj.DateBegin.HasValue || date >= obj.DateBegin.Value; } /// /// Gets object age (time since creation) /// /// Object /// Reference date (default - current) public static TimeSpan GetAge(this IRedbObject obj, DateTime? referenceDate = null) { var date = referenceDate ?? DateTime.Now; return date - obj.DateCreate; } /// /// Gets time since last object modification /// /// Object /// Reference date (default - current) public static TimeSpan GetTimeSinceLastModification(this IRedbObject obj, DateTime? referenceDate = null) { var date = referenceDate ?? DateTime.Now; return date - obj.DateModify; } // ===== UTILITIES ===== /// /// Gets object display name with fallback logic /// /// Object /// Whether to include ID in display name public static string GetDisplayName(this IRedbObject obj, bool includeId = true) { var displayName = obj.Name; // Fallback to values if (string.IsNullOrWhiteSpace(displayName)) { if (!string.IsNullOrWhiteSpace(obj.ValueString)) displayName = obj.ValueString; else if (obj.ValueLong.HasValue) displayName = $"Value_{obj.ValueLong.Value}"; else if (obj.ValueGuid.HasValue) displayName = obj.ValueGuid.Value.ToString("D").Substring(0, 8); else if (obj.Key.HasValue) displayName = $"Key_{obj.Key.Value}"; else displayName = $"Object_{obj.Id}"; } return includeId ? $"{displayName} (#{obj.Id})" : displayName; } /// /// Gets brief object information for debugging /// /// Object public static string GetDebugInfo(this IRedbObject obj) { return $"Object[Id={obj.Id}, Scheme={obj.SchemeId}, Name='{obj.Name}', Parent={obj.ParentId}, Owner={obj.OwnerId}]"; } /// /// Creates hierarchical path string for object /// /// Path objects from root to object /// Separator (default "/") public static string CreateHierarchicalPath(this IEnumerable pathObjects, string separator = "/") { return string.Join(separator, pathObjects.Select(obj => obj.Name ?? $"#{obj.Id}")); } } }