LinkedProperties.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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).OriginalValueList;
  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)).Select(x => new KeyValuePair<string, object?>(x.Key[RootPath.Length..], x.Value)).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. public IEnumerator<string> GetEnumerator()
  120. {
  121. return RootSet.Where(x => x.StartsWith(RootPath)).Select(x => x[RootPath.Length..]).GetEnumerator();
  122. }
  123. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  124. }
  125. public static class LinkedProperties
  126. {
  127. private static readonly Dictionary<Type, List<ILinkedProperty>> _LinkedProperties = new Dictionary<Type, List<ILinkedProperty>>();
  128. public static void Register<TLinkedEntity, TEntityLink, TType>(Expression<Func<TLinkedEntity,TEntityLink>> path, Expression<Func<TEntityLink, TType>> source,
  129. Expression<Func<TLinkedEntity, TType>> target, Func<TLinkedEntity,TType, Result<TType, string>>? func = null)
  130. where TLinkedEntity : class
  131. where TEntityLink : class
  132. {
  133. var map = new LinkedProperty<TLinkedEntity, TEntityLink, TType>(path, source, target, func);
  134. if(!_LinkedProperties.TryGetValue(map.Type, out var props))
  135. {
  136. props = new List<ILinkedProperty>();
  137. _LinkedProperties[map.Type] = props;
  138. }
  139. if(!props.Any(x => x.Equals(map)))
  140. {
  141. props.Add(map);
  142. }
  143. }
  144. /*public static IEnumerable<ILinkedProperty> Find(object parent, object path)
  145. {
  146. var all = _LinkedProperties.Where(x => x.Type == parent.GetType());
  147. var filtered = all.Where(x => CoreUtils.GetPropertyValue(parent,x.Path)?.GetType() == path?.GetType());
  148. return filtered;
  149. }*/
  150. public static BaseObject GetParent(ISubObject link)
  151. {
  152. while (true)
  153. {
  154. var parent = link.GetLinkedParent();
  155. if(parent is ISubObject parentLink)
  156. {
  157. link = parentLink;
  158. }
  159. else
  160. {
  161. return parent;
  162. }
  163. }
  164. }
  165. public static string GetPath(ISubObject link)
  166. {
  167. var path = link.GetLinkedPath();
  168. while (true)
  169. {
  170. var parent = link.GetLinkedParent();
  171. if (parent is ISubObject parentLink)
  172. {
  173. link = parentLink;
  174. path = $"{link.GetLinkedPath()}.{path}";
  175. }
  176. else
  177. {
  178. return path;
  179. }
  180. }
  181. }
  182. public static IEnumerable<ILinkedProperty> FindAll(Type parent)
  183. {
  184. if (_LinkedProperties.TryGetValue(parent, out var props))
  185. {
  186. return props;
  187. }
  188. return Enumerable.Empty<ILinkedProperty>();
  189. }
  190. public static IEnumerable<ILinkedProperty> Find(ISubObject subObject, out BaseObject parent)
  191. {
  192. parent = GetParent(subObject);
  193. if (!_LinkedProperties.TryGetValue(parent.GetType(), out var props))
  194. {
  195. return Enumerable.Empty<ILinkedProperty>();
  196. }
  197. var path = GetPath(subObject);
  198. return props.Where(x => x.Path == path);
  199. }
  200. public static bool Find(ISubObject subObject, string name, [NotNullWhen(true)] out ILinkedProperty? property, out BaseObject parent)
  201. {
  202. property = null;
  203. parent = GetParent(subObject);
  204. if (parent == null)
  205. return false;
  206. if(!_LinkedProperties.TryGetValue(parent.GetType(), out var props))
  207. return false;
  208. var path = GetPath(subObject);
  209. property = props.FirstOrDefault(x => string.Equals(
  210. $"{x.Path}{(x.Path.IsNullOrWhiteSpace() ? "" : ".")}{x.Source}",
  211. $"{path}{(path.IsNullOrWhiteSpace() ? "" : ".")}{name}"));
  212. return property != null;
  213. }
  214. }
  215. }