LinkedProperties.cs 3.8 KB

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