using System; using System.Collections.Generic; namespace NestedSetModelTest { public class EntityBase { public TKey Id { get; set; } public string IsDeleted { get; set; } public override string ToString() { return $"{ GetType().FullName}[{Id}]"; } } public abstract class BaseEntity : EntityBase { public BaseEntity() { this.Id = Guid.NewGuid(); } } public abstract class BaseTreeEntity : BaseEntity where T : BaseTreeEntity { public int DisplayOrder { get; set; } public int Left { get; set; } public int Right { get; set; } public Guid? ParentId { get; set; } public T Parent { get; set; } #pragma warning disable CA2227 // 集合属性应为只读 public List Children { get; set; } = new List(); #pragma warning restore CA2227 // 集合属性应为只读 public List GetPath() { var list = new List(); var item = this as T; while (item != null) { list.Add(item); if (item.Parent == null || item.Parent.Id == this.Id) { break; } item = item.Parent; } list.Reverse(); return list; } } public class Node : BaseTreeEntity { public string Name { get; set; } public string Number { get; set; } } }