LinkedProperties.cs 1.0 KB

1234567891011121314151617181920212223242526272829
  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. return _LinkedProperties.Where(x => (x.Type == parent.GetType()) && (CoreUtils.GetPropertyValue(parent,x.Path) == path));
  21. }
  22. }
  23. }