using System.Collections.Generic; using System.Linq; using MongoDB.Bson.Serialization.Attributes; namespace Base { /// /// 父子层级信息 /// public class LevelComponent : Component where T: Entity { [BsonIgnore] public T Parent { get; private set; } private List children; [BsonElement, BsonIgnoreIfNull] private Dictionary idChildren; [BsonElement, BsonIgnoreIfNull] private Dictionary nameChildren; [BsonIgnore] public int Count { get { return this.idChildren.Count; } } public void Add(T t) { t.GetComponent>().Parent = this.Owner; if (this.idChildren == null) { this.children = new List(); this.idChildren = new Dictionary(); this.nameChildren = new Dictionary(); } this.children.Add(t); this.idChildren.Add(t.Id, t); this.nameChildren.Add(t.Name, t); } public T[] GetChildren() { return this.children.ToArray(); } private void Remove(T t) { this.idChildren.Remove(t.Id); this.nameChildren.Remove(t.Name); if (this.idChildren.Count == 0) { this.children = null; this.idChildren = null; this.nameChildren = null; } t.Dispose(); } public void Remove(long id) { T t; if (!this.idChildren.TryGetValue(id, out t)) { return; } this.Remove(t); } public void Remove(string name) { T t; if (!this.nameChildren.TryGetValue(name, out t)) { return; } this.Remove(t); } public override void Dispose() { if (this.Id == 0) { return; } base.Dispose(); foreach (T t in this.children) { t.Dispose(); } this.Parent?.GetComponent>().Remove(this.Id); } } }