2020-09-27 22:16:57 +02:00

33 lines
1.1 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace UserService.Infrastructure.DataModels
{
public abstract class Node : ICloneable, IComparable<Node>
{
public Guid Id { get; set; }
[Required] public string CommonName { get; set; } = null!;
public string? Description { get; set; }
public ISet<Node> Children { get; set; } = new SortedSet<Node>();
public Node? Parent { get; set; } //Parent
public Guid? ParentId { get; set; }
public override string ToString() => CommonName;
public int Level => Parent?.Level + 1 ?? 0;
/// <inheritdoc />
public virtual object Clone() => MemberwiseClone();
public int CompareTo(Node? other)
{
if (ReferenceEquals(this, other)) return 0;
if (ReferenceEquals(null, other)) return 1;
var commonNameComparison = string.Compare(CommonName, other.CommonName, StringComparison.Ordinal);
if (commonNameComparison != 0) return commonNameComparison;
return Id.CompareTo(other.Id);
}
}
}