NestedSetModelTest

Former-commit-id: 362b27276319e216a7208f0f20360ff1b3b7ae69
Former-commit-id: 822df39a062b8deac5a26336d7b836edbf0a8967
1.0
wanggang 5 years ago
parent ed0ac377ff
commit c59273edec

@ -0,0 +1,16 @@
*.bak
*.suo
*.db
*.user
.vs
obj
Obj
bin
Bin
debug
Debug
release
Release
Logs
logs
node_modules

@ -0,0 +1,59 @@
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; }
}
}

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30717.126
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NestedSetModelTest", "NestedSetModelTest.csproj", "{37D9722F-9B16-481C-AEC0-35553CD13575}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{37D9722F-9B16-481C-AEC0-35553CD13575}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{37D9722F-9B16-481C-AEC0-35553CD13575}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37D9722F-9B16-481C-AEC0-35553CD13575}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37D9722F-9B16-481C-AEC0-35553CD13575}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9C3B8285-8771-4F47-89D3-A4B9BD41B469}
EndGlobalSection
EndGlobal

@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace NestedSetModelTest
{
internal class Program
{
private static List<Node> list = new List<Node>();
private static void Main(string[] args)
{
var root = CreateTree();
Add(root);
var i = 1;
foreach (var item in list)
{
item.Left = i++;
}
list.Reverse();
foreach (var item in list)
{
item.Right = i++;
}
list.Reverse();
foreach (var item in list)
{
Console.WriteLine($"{item.Name}:{item.Number},{item.Left}-{item.Right}");
}
}
private static void Add(Node node)
{
list.Add(node);
if (node.Children.Any())
{
foreach (var item in node.Children.OrderBy(o => o.DisplayOrder))
{
Add(item);
}
}
}
private static Node CreateTree()
{
var root = new Node
{
Name = "root",
Number = "1-18",
Children = new List<Node> {
new Node{
Name="Fruit",
Number="2-11",
Children=new List<Node>{
new Node{
Name="Red",
Number="3-6",
Children=new List<Node>
{
new Node{
Name="Cherry",
Number="4-5"
}
}
},
new Node{
Name="Yellow",
Number="7-10",
DisplayOrder=1,
Children=new List<Node>
{
new Node{
Name="Banana",
Number="8-9"
}
}
}
}
},
new Node{
Name="Meat",
Number="12-17",
DisplayOrder=1,
Children=new List<Node>{
new Node{
Name="Beef",
Number="13-14"
},
new Node{
Name="Port",
Number="15-16",
DisplayOrder=1,
}
}
}
}
};
return root;
}
}
}
Loading…
Cancel
Save