LinkedProperties.cs 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. namespace InABox.Core
  6. {
  7. public static class LinkedProperties
  8. {
  9. private static readonly List<ILinkedProperty> _LinkedProperties = new List<ILinkedProperty>();
  10. public static IEnumerable<ILinkedProperty> All => _LinkedProperties;
  11. public static void Register<TLinkedEntity, TEntityLink, TType>(Expression<Func<TLinkedEntity,TEntityLink>> path, Expression<Func<TEntityLink, TType>> source,
  12. Expression<Func<TLinkedEntity, TType>> target)
  13. {
  14. var map = new LinkedProperty<TLinkedEntity, TEntityLink, TType>(path, source, target);
  15. if (!_LinkedProperties.Any(x => x.Equals(map)))
  16. _LinkedProperties.Add(map);
  17. }
  18. public static IEnumerable<ILinkedProperty> Find(object parent, object path)
  19. {
  20. var all = _LinkedProperties.Where(x => x.Type == parent.GetType());
  21. var filtered = all.Where(x => CoreUtils.GetPropertyValue(parent,x.Path)?.GetType() == path?.GetType());
  22. return filtered;
  23. }
  24. }
  25. }