You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.5 KiB
59 lines
1.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace NestedSetModelTest
|
|
{
|
|
public class EntityBase<TKey>
|
|
{
|
|
public TKey Id { get; set; }
|
|
public string IsDeleted { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{ GetType().FullName}[{Id}]";
|
|
}
|
|
}
|
|
|
|
public abstract class BaseEntity : EntityBase<Guid>
|
|
{
|
|
public BaseEntity()
|
|
{
|
|
this.Id = Guid.NewGuid();
|
|
}
|
|
}
|
|
|
|
public abstract class BaseTreeEntity<T> : BaseEntity where T : BaseTreeEntity<T>
|
|
{
|
|
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<T> Children { get; set; } = new List<T>();
|
|
#pragma warning restore CA2227 // 集合属性应为只读
|
|
|
|
public List<T> GetPath()
|
|
{
|
|
var list = new List<T>();
|
|
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<Node>
|
|
{
|
|
public string Name { get; set; }
|
|
public string Number { get; set; }
|
|
}
|
|
} |