EntityLink.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Expressive;
  2. using System;
  3. using System.Dynamic;
  4. using System.Linq;
  5. namespace InABox.Core
  6. {
  7. public interface IEntityLink : ISubObject
  8. {
  9. Guid ID { get; set; }
  10. Guid Deleted { get; set; }
  11. bool Synchronise(object entity);
  12. bool Clear();
  13. bool IsValid();
  14. }
  15. public interface IEntityLink<T> : IEntityLink
  16. {
  17. }
  18. public abstract class EntityLink<T> : BaseObject, IEntityLink<T> where T : BaseObject, new()
  19. {
  20. /*
  21. private Func<BaseObject>? _linkedentity;
  22. [DoNotSerialize]
  23. protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();*/
  24. private BaseObject _linkedParent;
  25. private string _linkedPath;
  26. public void SetLinkedParent(BaseObject parent)
  27. {
  28. _linkedParent = parent;
  29. }
  30. public void SetLinkedPath(string path)
  31. {
  32. _linkedPath = path;
  33. }
  34. public BaseObject GetLinkedParent() => _linkedParent;
  35. public string GetLinkedPath() => _linkedPath;
  36. /*
  37. [Obsolete("Please supply linked Entity")]
  38. public EntityLink()
  39. {
  40. }
  41. public EntityLink(Func<BaseObject>? entity, string name)
  42. {
  43. _linkedentity = entity;
  44. }*/
  45. [NullEditor]
  46. public abstract Guid ID { get; set; }
  47. [NullEditor]
  48. public Guid Deleted { get; set; }
  49. public virtual bool Synchronise(object Entity)
  50. {
  51. var result = false;
  52. var props = CoreUtils.PropertyList(GetType(), x => (!x.Name.Equals("ID") || Entity == null) && !x.DeclaringType.Equals(typeof(BaseObject)), false);
  53. foreach (var key in props.Keys)
  54. {
  55. if (CoreUtils.HasProperty(Entity.GetType(), key))
  56. {
  57. var prop = CoreUtils.GetProperty(typeof(T), key);
  58. if (prop != null && prop.PropertyType.Equals(props[key]))
  59. {
  60. if (prop.PropertyType.GetInterfaces().Any(x => x == typeof(IEntityLink)))
  61. {
  62. var tgtlink = CoreUtils.GetPropertyValue(this, key) as IEntityLink;
  63. var srclink = CoreUtils.GetPropertyValue(Entity, key) as IEntityLink;
  64. tgtlink.ID = srclink.ID;
  65. tgtlink.Synchronise(srclink);
  66. }
  67. else
  68. {
  69. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  70. var newvalue = CoreUtils.GetPropertyValue(Entity, key);
  71. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  72. {
  73. result = true;
  74. try
  75. {
  76. CoreUtils.SetPropertyValue(this, key, newvalue);
  77. }
  78. catch (Exception e)
  79. {
  80. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. foreach (var link in LinkedProperties.Find(this, out var parent))
  88. {
  89. link.Update(this, parent);
  90. result = true;
  91. }
  92. return result;
  93. }
  94. public bool Clear()
  95. {
  96. var result = false;
  97. var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);
  98. foreach (var key in props.Keys)
  99. {
  100. var prop = CoreUtils.GetProperty(typeof(T), key);
  101. if (prop != null && prop.PropertyType.Equals(props[key]))
  102. {
  103. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  104. var newvalue = prop.PropertyType.Equals(typeof(string)) ? "" : Activator.CreateInstance(prop.PropertyType);
  105. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  106. {
  107. result = true;
  108. CoreUtils.SetPropertyValue(this, key, newvalue);
  109. }
  110. }
  111. }
  112. return result;
  113. }
  114. protected override void DoPropertyChanged(string name, object? before, object? after)
  115. {
  116. if (IsObserving())
  117. {
  118. if(LinkedProperties.Find(this, name, out var link, out var parent))
  119. {
  120. link.Update(this, parent);
  121. }
  122. }
  123. }
  124. public bool IsValid() => !ID.Equals(Guid.Empty) && Deleted.Equals(Guid.Empty);
  125. }
  126. }