LinkedProperties.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using FluentResults;
  7. namespace InABox.Core
  8. {
  9. public interface ISubObject
  10. {
  11. BaseObject GetLinkedParent();
  12. void SetLinkedParent(BaseObject obj);
  13. string GetLinkedPath();
  14. void SetLinkedPath(string path);
  15. }
  16. // public interface IResult
  17. // {
  18. // object? Value { get; set; }
  19. // bool Cancel { get; set; }
  20. // }
  21. //
  22. // public class Result<TType> : IResult
  23. // {
  24. // public TType Value { get; set; }
  25. //
  26. // object? IResult.Value {
  27. // get => this.Value;
  28. // set => this.Value = (value is TType _type ? _type : default)!;
  29. // }
  30. //
  31. // public bool Cancel { get; set; }
  32. //
  33. // public Result(TType value, bool cancel = false)
  34. // {
  35. // Value = value;
  36. // Cancel = cancel;
  37. // }
  38. // }
  39. public static class LinkedProperties
  40. {
  41. private static readonly Dictionary<Type, List<ILinkedProperty>> _LinkedProperties = new Dictionary<Type, List<ILinkedProperty>>();
  42. public static void Register<TLinkedEntity, TEntityLink, TType>(Expression<Func<TLinkedEntity,TEntityLink>> path, Expression<Func<TEntityLink, TType>> source,
  43. Expression<Func<TLinkedEntity?, TType>> target, Func<TLinkedEntity?,TType, Result<TType>>? func = null)
  44. where TLinkedEntity : class
  45. where TEntityLink : class
  46. {
  47. var map = new LinkedProperty<TLinkedEntity, TEntityLink, TType>(path, source, target, func);
  48. if(!_LinkedProperties.TryGetValue(map.Type, out var props))
  49. {
  50. props = new List<ILinkedProperty>();
  51. _LinkedProperties[map.Type] = props;
  52. }
  53. if(!props.Any(x => x.Equals(map)))
  54. {
  55. props.Add(map);
  56. }
  57. }
  58. /*public static IEnumerable<ILinkedProperty> Find(object parent, object path)
  59. {
  60. var all = _LinkedProperties.Where(x => x.Type == parent.GetType());
  61. var filtered = all.Where(x => CoreUtils.GetPropertyValue(parent,x.Path)?.GetType() == path?.GetType());
  62. return filtered;
  63. }*/
  64. private static BaseObject GetParent(ISubObject link)
  65. {
  66. while (true)
  67. {
  68. var parent = link.GetLinkedParent();
  69. if(parent is IEntityLink parentLink)
  70. {
  71. link = parentLink;
  72. }
  73. else
  74. {
  75. return parent;
  76. }
  77. }
  78. }
  79. private static string GetPath(ISubObject link)
  80. {
  81. var path = link.GetLinkedPath();
  82. while (true)
  83. {
  84. var parent = link.GetLinkedParent();
  85. if (parent is IEntityLink parentLink)
  86. {
  87. link = parentLink;
  88. path = $"{link.GetLinkedPath()}.{path}";
  89. }
  90. else
  91. {
  92. return path;
  93. }
  94. }
  95. }
  96. public static IEnumerable<ILinkedProperty> FindAll(Type parent)
  97. {
  98. if (_LinkedProperties.TryGetValue(parent, out var props))
  99. {
  100. return props;
  101. }
  102. return Enumerable.Empty<ILinkedProperty>();
  103. }
  104. public static IEnumerable<ILinkedProperty> Find(ISubObject subObject, out BaseObject parent)
  105. {
  106. parent = GetParent(subObject);
  107. if (!_LinkedProperties.TryGetValue(parent.GetType(), out var props))
  108. {
  109. return Enumerable.Empty<ILinkedProperty>();
  110. }
  111. var path = GetPath(subObject);
  112. return props.Where(x => x.Path == path);
  113. }
  114. public static bool Find(ISubObject subObject, string name, [NotNullWhen(true)] out ILinkedProperty? property, out BaseObject parent)
  115. {
  116. property = null;
  117. parent = GetParent(subObject);
  118. if (parent == null)
  119. return false;
  120. if(!_LinkedProperties.TryGetValue(parent.GetType(), out var props))
  121. return false;
  122. var path = GetPath(subObject);
  123. property = props.FirstOrDefault(x => String.Equals($"{x.Path}{(String.IsNullOrWhiteSpace(x.Path) ? "" : ".")}{x.Source}", $"{path}{(String.IsNullOrWhiteSpace(path) ? "" : ".")}{name}"));
  124. return property != null;
  125. }
  126. }
  127. }