LinkedProperties.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  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 class SubObjectOriginalValues : IOriginalValues
  17. {
  18. private ISubObject Object { get; set; }
  19. private IOriginalValues? _rootDictionary;
  20. private IOriginalValues RootDictionary
  21. {
  22. get
  23. {
  24. _rootDictionary ??= LinkedProperties.GetParent(Object).OriginalValues;
  25. return _rootDictionary;
  26. }
  27. }
  28. private string? _rootPath;
  29. /// <summary>
  30. /// Path of this sub object according to the root, suffixed with a "."
  31. /// </summary>
  32. private string RootPath
  33. {
  34. get
  35. {
  36. _rootPath ??= $"{LinkedProperties.GetPath(Object)}.";
  37. return _rootPath;
  38. }
  39. }
  40. public SubObjectOriginalValues(ISubObject obj)
  41. {
  42. Object = obj;
  43. }
  44. private string ChangeKey(string key) => $"{RootPath}{key}";
  45. public bool ContainsKey(string key)
  46. {
  47. return RootDictionary.ContainsKey(ChangeKey(key));
  48. }
  49. public void Clear()
  50. {
  51. foreach(var (k, v) in RootDictionary)
  52. {
  53. if (k.StartsWith(RootPath))
  54. {
  55. RootDictionary.Remove(k);
  56. }
  57. }
  58. }
  59. public void Remove(string key)
  60. {
  61. RootDictionary.Remove(ChangeKey(key));
  62. }
  63. public bool TryAdd(string key, object? value)
  64. {
  65. return RootDictionary.TryAdd(ChangeKey(key), value);
  66. }
  67. public bool TryGetValue(string key, out object? value)
  68. {
  69. return RootDictionary.TryGetValue(ChangeKey(key), out value);
  70. }
  71. public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
  72. {
  73. return RootDictionary.Where(x => x.Key.StartsWith(RootPath)).GetEnumerator();
  74. }
  75. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  76. public object? this[string key]
  77. {
  78. get => RootDictionary[ChangeKey(key)];
  79. set => RootDictionary[ChangeKey(key)] = value;
  80. }
  81. }
  82. public class SubObjectLoadedColumns : ILoadedColumns
  83. {
  84. private ISubObject Object { get; set; }
  85. private ILoadedColumns? _rootSet;
  86. private ILoadedColumns RootSet
  87. {
  88. get
  89. {
  90. _rootSet ??= LinkedProperties.GetParent(Object).LoadedColumns;
  91. return _rootSet;
  92. }
  93. }
  94. private string? _rootPath;
  95. /// <summary>
  96. /// Path of this sub object according to the root, suffixed with a "."
  97. /// </summary>
  98. private string RootPath
  99. {
  100. get
  101. {
  102. _rootPath ??= $"{LinkedProperties.GetPath(Object)}.";
  103. return _rootPath;
  104. }
  105. }
  106. public SubObjectLoadedColumns(ISubObject obj)
  107. {
  108. Object = obj;
  109. }
  110. private string ChangeKey(string key) => $"{RootPath}{key}";
  111. public bool Add(string key)
  112. {
  113. return RootSet.Add(ChangeKey(key));
  114. }
  115. public bool Contains(string key)
  116. {
  117. return RootSet.Contains(ChangeKey(key));
  118. }
  119. }
  120. public static class LinkedProperties
  121. {
  122. private static readonly Dictionary<Type, List<ILinkedProperty>> _LinkedProperties = new Dictionary<Type, List<ILinkedProperty>>();
  123. public static void Register<TLinkedEntity, TEntityLink, TType>(Expression<Func<TLinkedEntity,TEntityLink>> path, Expression<Func<TEntityLink, TType>> source,
  124. Expression<Func<TLinkedEntity, TType>> target, Func<TLinkedEntity,TType, Result<TType, string>>? func = null)
  125. where TLinkedEntity : class
  126. where TEntityLink : class
  127. {
  128. var map = new LinkedProperty<TLinkedEntity, TEntityLink, TType>(path, source, target, func);
  129. if(!_LinkedProperties.TryGetValue(map.Type, out var props))
  130. {
  131. props = new List<ILinkedProperty>();
  132. _LinkedProperties[map.Type] = props;
  133. }
  134. if(!props.Any(x => x.Equals(map)))
  135. {
  136. props.Add(map);
  137. }
  138. }
  139. /*public static IEnumerable<ILinkedProperty> Find(object parent, object path)
  140. {
  141. var all = _LinkedProperties.Where(x => x.Type == parent.GetType());
  142. var filtered = all.Where(x => CoreUtils.GetPropertyValue(parent,x.Path)?.GetType() == path?.GetType());
  143. return filtered;
  144. }*/
  145. public static BaseObject GetParent(ISubObject link)
  146. {
  147. while (true)
  148. {
  149. var parent = link.GetLinkedParent();
  150. if(parent is ISubObject parentLink)
  151. {
  152. link = parentLink;
  153. }
  154. else
  155. {
  156. return parent;
  157. }
  158. }
  159. }
  160. public static string GetPath(ISubObject link)
  161. {
  162. var path = link.GetLinkedPath();
  163. while (true)
  164. {
  165. var parent = link.GetLinkedParent();
  166. if (parent is ISubObject parentLink)
  167. {
  168. link = parentLink;
  169. path = $"{link.GetLinkedPath()}.{path}";
  170. }
  171. else
  172. {
  173. return path;
  174. }
  175. }
  176. }
  177. public static IEnumerable<ILinkedProperty> FindAll(Type parent)
  178. {
  179. if (_LinkedProperties.TryGetValue(parent, out var props))
  180. {
  181. return props;
  182. }
  183. return Enumerable.Empty<ILinkedProperty>();
  184. }
  185. public static IEnumerable<ILinkedProperty> Find(ISubObject subObject, out BaseObject parent)
  186. {
  187. parent = GetParent(subObject);
  188. if (!_LinkedProperties.TryGetValue(parent.GetType(), out var props))
  189. {
  190. return Enumerable.Empty<ILinkedProperty>();
  191. }
  192. var path = GetPath(subObject);
  193. return props.Where(x => x.Path == path);
  194. }
  195. public static bool Find(ISubObject subObject, string name, [NotNullWhen(true)] out ILinkedProperty? property, out BaseObject parent)
  196. {
  197. property = null;
  198. parent = GetParent(subObject);
  199. if (parent == null)
  200. return false;
  201. if(!_LinkedProperties.TryGetValue(parent.GetType(), out var props))
  202. return false;
  203. var path = GetPath(subObject);
  204. property = props.FirstOrDefault(x => string.Equals(
  205. $"{x.Path}{(x.Path.IsNullOrWhiteSpace() ? "" : ".")}{x.Source}",
  206. $"{path}{(path.IsNullOrWhiteSpace() ? "" : ".")}{name}"));
  207. return property != null;
  208. }
  209. }
  210. }