EntityLink.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 : Entity, 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. [Obsolete]
  49. public Guid Deleted { get; set; }
  50. /// <summary>
  51. /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
  52. /// </summary>
  53. /// <param name="other">The link to copy data from.</param>
  54. public void CopyFrom(IEntityLink<T> other)
  55. {
  56. ID = other.ID;
  57. Synchronise(other);
  58. }
  59. /// <summary>
  60. /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
  61. /// </summary>
  62. /// <param name="other">The link to copy data from.</param>
  63. public void CopyFrom(T other)
  64. {
  65. ID = other.ID;
  66. Synchronise(other);
  67. }
  68. public virtual bool Synchronise(object Entity)
  69. {
  70. var result = false;
  71. var props = CoreUtils.PropertyList(GetType(), x => (!x.Name.Equals("ID") || Entity == null) && !x.DeclaringType.Equals(typeof(BaseObject)), false);
  72. foreach (var key in props.Keys)
  73. {
  74. if (CoreUtils.HasProperty(Entity.GetType(), key))
  75. {
  76. var prop = CoreUtils.GetProperty(typeof(T), key);
  77. if (prop != null && prop.PropertyType.Equals(props[key]))
  78. {
  79. if (prop.PropertyType.GetInterfaces().Any(x => x == typeof(IEntityLink)))
  80. {
  81. var tgtlink = CoreUtils.GetPropertyValue(this, key) as IEntityLink;
  82. var srclink = CoreUtils.GetPropertyValue(Entity, key) as IEntityLink;
  83. tgtlink.ID = srclink.ID;
  84. tgtlink.Synchronise(srclink);
  85. }
  86. else
  87. {
  88. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  89. var newvalue = CoreUtils.GetPropertyValue(Entity, key);
  90. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  91. {
  92. result = true;
  93. try
  94. {
  95. CoreUtils.SetPropertyValue(this, key, newvalue);
  96. }
  97. catch (Exception e)
  98. {
  99. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. foreach (var link in LinkedProperties.Find(this, out var parent))
  107. {
  108. link.Update(this, parent);
  109. result = true;
  110. }
  111. return result;
  112. }
  113. public bool Clear()
  114. {
  115. var result = false;
  116. var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);
  117. foreach (var key in props.Keys)
  118. {
  119. var prop = CoreUtils.GetProperty(typeof(T), key);
  120. if (prop != null && prop.PropertyType.Equals(props[key]))
  121. {
  122. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  123. var newvalue = prop.PropertyType.Equals(typeof(string)) ? "" : Activator.CreateInstance(prop.PropertyType);
  124. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  125. {
  126. result = true;
  127. CoreUtils.SetPropertyValue(this, key, newvalue);
  128. }
  129. }
  130. }
  131. return result;
  132. }
  133. protected override void DoPropertyChanged(string name, object? before, object? after)
  134. {
  135. if (IsObserving())
  136. {
  137. if(LinkedProperties.Find(this, name, out var link, out var parent))
  138. {
  139. link.Update(this, parent);
  140. }
  141. }
  142. }
  143. public bool IsValid() => !ID.Equals(Guid.Empty) && Deleted.Equals(Guid.Empty);
  144. }
  145. }