LinkedProperty.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Linq.Expressions;
  3. using System.Reflection;
  4. namespace InABox.Core
  5. {
  6. public interface ILinkedProperty
  7. {
  8. Type Type { get; }
  9. String Path { get; }
  10. String Source { get; }
  11. String Target { get; }
  12. void Update(object? from, object? to);
  13. bool Equals(object obj);
  14. }
  15. public class LinkedProperty<TLinkedEntity, TEntityLink, TType> : ILinkedProperty
  16. {
  17. public Type Type => typeof(TLinkedEntity);
  18. public String Path { get; }
  19. public String Source { get; }
  20. public String Target { get; }
  21. public LinkedProperty(Expression<Func<TLinkedEntity, TEntityLink>> path,
  22. Expression<Func<TEntityLink, TType>> source,
  23. Expression<Func<TLinkedEntity, TType>> target)
  24. {
  25. Path = CoreUtils.GetFullPropertyName(path, ".");
  26. Source = CoreUtils.GetFullPropertyName(source, ".");
  27. Target = CoreUtils.GetFullPropertyName(target,".");
  28. }
  29. public void Update(object? from, object? to)
  30. {
  31. if (from != null && to != null)
  32. {
  33. var value = CoreUtils.GetPropertyValue(from, Source);
  34. CoreUtils.SetPropertyValue(to, Target, value);
  35. }
  36. }
  37. public override bool Equals(object obj)
  38. {
  39. if (obj is ILinkedProperty src)
  40. {
  41. return
  42. String.Equals(src.Path,Path)
  43. && String.Equals(src.Source, Source)
  44. && String.Equals(src.Target, Target);
  45. }
  46. return false;
  47. }
  48. }
  49. }